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.

Install yfinance Terminal command

Once installed, you can fetch stock data with three lines of Python:

stock_price_simple.py Your first tool function
Expected output (example)
HDFC Bank: ₹1847.50 (+2.15%)
Why yfinance is useful:
No API keys, no rate limits, just fetch. Real stock symbols work: HDFCBANK.NS (India), SAFCOM.NR (Kenya), TSLA (US). For your agent, wrap this in a tool that the LLM can call.

Tool #2: yfinance news—pull headlines for a ticker

Same yfinance library gives you news headlines. Pass a ticker, get the last N articles:

news_tool.py Fetch company news headlines
Expected output (example)
HDFC Bank Q4 earnings beat estimates Source: Reuters RBI holds rates steady; HDFC Bank rallies Source: Mint

Tool #3: DuckDuckGo search—no API key web search

DuckDuckGo doesn't require API keys. Install the duckduckgo-search library (formerly ddgs):

Install DuckDuckGo search Terminal command

Now write a web search tool:

web_search_tool.py Search the web without an API key
Rate limiting:
DuckDuckGo throttles aggressive requests. Wrap your searches in try/except. If you hit rate limits, add a small sleep between calls: 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.

agent_with_tools.py Complete agent with three real tools
Expected output
{ "ticker": "HDFCBANK.NS", "price": 1847.50, "change_pct": 2.15 }

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.

📊
India Scenario
Arjun monitors HDFC Bank stock from Hyderabad

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:

Agent summary output
HDFCBANK.NS: ₹1847.50 (+2.15%) Latest news: - HDFC Bank Q4 earnings beat estimates (Reuters) - RBI holds rates steady; HDFC Bank rallies (Mint) Analyst ratings: - "HDFC Bank remains a strong buy" (Goldman Sachs) - "Price target ₹2100" (CLSA)

Kenya example: Safaricom (SAFCOM.NR)

Safaricom is listed on the Nairobi Securities Exchange. Use the same agent pattern:

safaricom_agent.py Agent for East African telecom stock
Example output
SAFCOM.NR: KSh 34.50 (-1.20%) News: Safaricom 5G rollout on track (Business Daily) Web: Safaricom partners with AWS for cloud services

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:

robust_tools.py Tool functions with error recovery
Agent resilience:
When a tool fails, the agent should still be able to respond. If stock price fails but news works, tell the user "I got news but price data is down right now." Better than crashing.

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.

Dangerous (NEVER DO THIS)
Open-ended code execution

LLM generates Python code. Agent runs eval(). Attacker asks it to delete files.

Safe (THIS IS GOOD)
Whitelisted tools only

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.

Mini Challenge: Add a tool for currency conversion

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

Question 1 of 5
Why does yfinance not require an API key?
Question 2 of 5
What is DuckDuckGo-search's main advantage as a tool for agents?
Question 3 of 5
If your agent calls get_stock_price('FAKETTICKER') and yfinance fails silently, what should happen?
Question 4 of 5
What does time.sleep(0.5) do before a DuckDuckGo search?
Question 5 of 5
Why should you never let your agent execute arbitrary Python code?
0
/5
Excellent! You understand real tool integration and agent security.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →