Skip to content

Your First Agent

Create a stock analysis agent with LangChain and register it on Zynd.

Complete example

Here's a fully functional agent that analyzes stocks using tools:

python
from zyndai_agent.agent import AgentConfig, ZyndAIAgent
from langchain_openai import ChatOpenAI
from langchain_classic.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.tools import tool
from langchain_community.tools.tavily_search import TavilySearchResults
from dotenv import load_dotenv
import os

load_dotenv()

# 1. Define tools for the agent
@tool
def compare_stocks(stock_symbols: str) -> str:
    """Compare multiple stocks by their key metrics."""
    symbols = [s.strip().upper() for s in stock_symbols.split(",")]
    return f"Comparison data for {', '.join(symbols)}..."

@tool
def get_stock_info(symbol: str) -> str:
    """Get detailed information about a specific stock."""
    return f"Stock info for {symbol.upper()}..."

# 2. Create LangChain executor
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
search = TavilySearchResults(max_results=5)
tools = [compare_stocks, get_stock_info, search]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a stock analysis agent. Use tools to find data."),
    MessagesPlaceholder(variable_name="chat_history"),
    ("human", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor.from_agent_and_tools(
    agent=agent,
    tools=tools,
    verbose=True
)

# 3. Configure Zynd agent
config = AgentConfig(
    name="Stock Analyzer",
    description="Real-time stock comparison and analysis",
    category="finance",
    tags=["stocks", "analysis", "trading"],
    webhook_port=5003,
    registry_url=os.environ.get(
        "ZYND_REGISTRY_URL",
        "https://zns01.zynd.ai"
    ),
    use_ngrok=True,
    entity_pricing={
        "model": "per_request",
        "base_price_usd": 0.0001,
        "currency": "USDC",
        "payment_methods": ["x402"],
        "rates": {"default": 0.0001},
    },
)

# 4. Create and configure
zynd_agent = ZyndAIAgent(agent_config=config)
zynd_agent.set_langchain_agent(executor)

# 5. Test invoke
if __name__ == "__main__":
    result = zynd_agent.invoke("Compare AAPL and GOOGL")
    print(result)

Breaking it down

Section 1: Define tools

Tools give your agent capabilities. The @tool decorator converts functions into LangChain tools with automatic docstring parsing. Your agent learns what each tool does from the docstring.

Section 2: Create executor

The executor combines the LLM, tools, and prompt. LangChain's create_tool_calling_agent uses function-calling models to decide which tool to use. The prompt tells the agent its role and provides message history slots for multi-turn conversations.

Section 3: Configure

AgentConfig sets your agent's metadata: name, description, category, tags, port, registry URL, and pricing. The use_ngrok=True flag creates a public tunnel for webhook callbacks during development.

Section 4: Connect to Zynd

Create a ZyndAIAgent and call set_langchain_agent() to connect your LangChain executor. The SDK now owns the agent lifecycle.

Section 5: Test locally

Call agent.invoke() to test before registration. This runs the LLM without starting the webhook server.

Before you register

WARNING

Make sure OPENAI_API_KEY is set in your .env file. The agent cannot run without it.

TIP

Use use_ngrok=True for local development. This creates a public tunnel so registries can reach your local webhook server. Remove it before deploying to production with a real domain.

Run it

bash
zynd agent run --port 5003

zynd agent run handles everything — it starts the Flask server, generates and signs the Agent Card, registers on zns01.zynd.ai, and begins the heartbeat. The CLI prints your FQAN (e.g. zns01.zynd.ai/alice/stock-analyzer).

Making it public

For local development, leave use_ngrok=True — the SDK opens a tunnel and writes the public URL into your Agent Card.

For production, drop the ngrok flag and pick one:

  • Deploy to deployer.zynd.ai — zip + keypair, get https://<slug>.deployer.zynd.ai.
  • Run your own container — on any VPS or Docker host. Set ZYND_ENTITY_URL env var to your public URL.

Next steps

Released under the MIT License.