Recap: from function calling to real tools
In Lesson 07, you built a function-calling loop. The LLM decided which function to call and passed typed parameters. But those functions were imaginary: they logged text or returned mock data.
Now we wire up real tools. Your agent will fetch live stock prices from Yahoo Finance, pull news headlines, and search the web. By the end of this lesson, you'll have an agent that answers questions like "What's going on with HDFCBANK.NS today?" by combining three real data sources.
Tool #1: yfinance—stock data without API keys
yfinance is a Python library that scrapes Yahoo Finance. No API key, no authentication. Just pip install and go.
Once installed, you can fetch stock data with three lines of Python:
Tool #2: yfinance news—pull headlines for a ticker
Same yfinance library gives you news headlines. Pass a ticker, get the last N articles:
Tool #3: DuckDuckGo search—no API key web search
DuckDuckGo doesn't require API keys. Install the duckduckgo-search library (formerly ddgs):
Now write a web search tool:
time.sleep(0.5)
Wire all three tools into your agent
Now the payoff. You have three tool functions. Wire them into the function-calling loop from Lesson 07. The LLM decides which tool to use and with what parameters.
Worked example: "What's going on with HDFCBANK.NS today?"
Here's how the agent handles a multi-part question that needs all three tools in sequence.
Arjun invests in HDFC Bank. He asks his agent: "What's going on with HDFCBANK.NS today?" The agent needs to:
- Fetch the stock price and today's change
- Pull the latest 2 news headlines
- Search for any recent analyst ratings
- Summarize all three into a single answer
The agent calls three tools, parses three responses, and gives Arjun a summary in seconds.
Real agent output for this query would be:
Kenya example: Safaricom (SAFCOM.NR)
Safaricom is listed on the Nairobi Securities Exchange. Use the same agent pattern:
Error handling: timeouts, rate limits, and silent failures
yfinance and DuckDuckGo can fail silently. A ticker might not exist. DuckDuckGo might throttle your requests. Always wrap tool calls in try/except:
Security: tools should be controlled, not open-ended
Here's a critical rule: never let the LLM pick arbitrary Python code to execute. Limit your agent's tools to a whitelist of functions you write and test.
LLM generates Python code. Agent runs eval(). Attacker asks it to delete files.
Agent can call only your predefined functions: get_stock_price(), search_web(). LLM chooses which one, but can't execute arbitrary code.
Your agent's tools in this lesson are all safe: they read data from public APIs. They can't delete, modify, or access credentials. That's the goal.
Task: Write a fourth tool function convert_currency(amount: float, from_currency: str, to_currency: str) -> dict that converts currencies. Use the yfinance approach: fetch the exchange rate. Then add it to your agent's tool list so it can convert ₹ to KSh or vice versa.
Hint 1: Fetch the exchange rate using yfinance for a pair like 'INRKSH=X'.
Hint 2: Multiply your amount by the rate.
Hint 3: Add the function to tools_schema in your agent so the LLM knows it exists.
Test your knowledge
You've wired your first tools.
Now build agents that work in production.
Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Agentic Engineer →