An autonomous AI agent that answers finance questions by deciding which tools to call — get_stock_price or get_ratios — based on the user's question. It maintains conversation memory across turns.
| Chatbot | This Agent |
|---|---|
| Single-turn, no tools | Multi-turn with tool calling |
| Pre-defined responses | Decides which tool(s) to call based on question |
| No memory | Full conversation history carried forward |
| Can't combine data | Calls multiple tools in one turn if needed |
| Tool | Description | When It's Called |
|---|---|---|
get_stock_price(ticker) |
Returns current market price only | User asks "What's AAPL trading at?" |
get_ratios(ticker) |
Returns P/E, P/B, ROE, ROA, NPM | User asks "Is AAPL cheap?" or "Show me AAPL ratios" |
You: What is the price of AAPL?
→ Agent calls get_stock_price("AAPL") → Returns $321.45
You: What are the key financial ratios for HDFCBANK.NS?
→ Agent calls get_ratios("HDFCBANK.NS") → Returns P/E, P/B, ROE, ROA, NPM
You: Give me price and ratios for AAPL
→ Agent calls get_stock_price("AAPL") AND get_ratios("AAPL")
You: Compare HDFC Bank and Axis Bank valuation — which looks cheaper?
→ Agent calls get_ratios("HDFCBANK.NS") AND get_ratios("AXISBANK.NS")
→ Compares P/E, P/B, ROE, ROA, NPM → "Axis Bank is cheaper on P/E (13.77x vs 16.68x)"
You: What is the P/E ratio of HDFCBANK.NS?
→ Agent calls get_ratios("HDFCBANK.NS") → P/E = 16.68x
You: What about Axis Bank?
→ Agent calls get_ratios("AXISBANK.NS") → P/E = 13.77x (remembers context)
You: Which one is cheaper based on P/E?
→ Agent answers from history: "Axis Bank (13.77x vs 16.68x)" — NO new tool calls
User Question
│
▼
┌─────────────────────────────────────┐
│ System Prompt + Conversation History │
│ (system + user1 + assistant1 + ...) │
└─────────────────────────────────────┘
│
▼
┌─────────────────────────────────────┐
│ OpenRouter LLM (openrouter/free) │
│ Sees: messages + tool schemas │
│ Decides: respond OR call tool(s) │
└─────────────────────────────────────┘
│
├─→ No tool_calls → Final answer
│
└─→ tool_calls → Execute tool(s)
│
▼
Append tool results as role="tool" messages
│
▼
Loop back to LLM with updated history
pip install yfinance openai
export OPENROUTER_API_KEY="sk-or-..."
python agent.pypython agent.pyThen ask questions interactively. Type exit to quit.
The global messages list accumulates across turns:
systemprompt (once)- Each
userquestion - Each
assistantresponse (includingtool_calls) - Each
toolresult (withtool_call_id)
This means follow-ups like "What about X?" or "Which is cheaper?" work naturally — the agent sees the full context.
- Python 3.10+
yfinancefor market dataopenaiclient (works with OpenRouter's OpenAI-compatible API)- OpenRouter API key (free tier: 50 requests/day)
Default: openrouter/free — auto-routes to an available free model that supports tool calling (e.g., Nemotron 3 Ultra).
Change in agent.py:
MODEL = "openrouter/free" # or specific model like "nvidia/nemotron-3-ultra-550b-a55b:free"