The goal: an agent that summarizes any topic

Over the last nine lessons, you've learned system prompts, structured outputs, multi-turn chat, and tool use. Now you'll build a production-grade ReAct agent that combines all of it.

The agent does this: given a topic or URL, it searches the web, picks the most promising link, fetches the HTML, strips the content, and returns a structured summary with key facts and source citations.

$ python summarizer_agent.py "M-Pesa Ethiopia expansion latest"

Searching for: M-Pesa Ethiopia expansion latest
Found: https://techcrunch.com/2026/03/m-pesa-ethiopia...
Fetching and analyzing...

PageSummary:
  Title: M-Pesa Eyes 5M Users in Ethiopia by 2027
  Summary: Safaricom's M-Pesa service launched formal
  operations in Ethiopia in March 2026, targeting 5M
  active users within 18 months through partnerships
  with local banks.
  Key Facts:
    1. Launch date: March 2026 (formal ops)
    2. Target: 5M users by Q2 2027
    3. Partner: Commercial Bank of Ethiopia (CBE)
  Sources:
    - https://techcrunch.com/2026/03/m-pesa-ethiopia...
    - https://reuters.com/tech/safaricom-ethiopia-2026...
    - https://bbc.com/news/business/m-pesa-africa-2026...

It handles real-world complexity: websites that block bots, broken HTML, slow pages, and network timeouts. The agent gracefully fails and reports what it couldn't find.

Architecture: ReAct loop with 2 tools

This agent follows the ReAct (Reasoning + Acting) pattern you learned in Lesson 09. It has a system prompt that decides: Should I search for this topic, or fetch that URL? It has two tools.

User query
β†’
LLM reasons: search or fetch?
β†’
Call tool 1 or 2
β†’
Parse JSON into Pydantic
β†’
Return PageSummary
πŸ“°
Real scenario
SouthBank India's research assistant (Mumbai)

SouthBank India's compliance team needs daily summaries of RBI announcements and fintech news. They don't want to manually browse 10 websites. You'll build an agent that takes "RBI repo rate decision February 2026" and returns a summary with key facts and official source links. The output is JSON so it can be fed into their reporting dashboard.

Tool 1: fetch_page(url) β€” scrape and clean HTML

The first tool fetches a URL and extracts plain text. It uses `requests` (for HTTP) and `beautifulsoup4` (for HTML parsing). The key trick: strip out navigation, ads, and scripts; keep only the main article text.

fetch_page_tool.py Fetch and clean webpage content for summarization
Why graceful failure?
Real websites block bots (Cloudflare), have timeouts, or serve broken HTML. The agent must never crash. If a fetch fails, it returns None and the LLM tries a different source.

Tool 2: search_web(query, k=3) β€” web search without API keys

The second tool searches the web using DDGS (DuckDuckGo Search), which has no API key requirement. It returns the top 3 URLs and snippets. The LLM reads the snippets and picks the best source to fetch.

search_tool.py Search web and return top results with snippets
Why DDGS?
It's free, no API key, and fast. Google and Bing require keys or rate-limiting. For production, you'd use a real API (Bing, Google Custom Search), but DDGS is perfect for prototypes.

Define the output: PageSummary Pydantic model

Before the LLM runs, define exactly what you want back. The PageSummary model enforces structure and validates the LLM's JSON output.

output_model.py Structured output definition

The system prompt: ReAct orchestration

The system prompt tells the LLM: "You are an agent. You have two tools. Decide which to use. When you fetch a page, extract key facts. Return only valid JSON."

agent_system_prompt.py ReAct system prompt for multi-tool agent

Putting it all together: the complete agent

Here's the full, runnable agent that ties together search, fetch, LLM reasoning, and structured output. It handles real-world edge cases: timeouts, network errors, bot detection.

summarizer_agent.py Complete ReAct summarizer agent with tool routing
$ python summarizer_agent.py "RELIANCE Q1 earnings 2026"

Summarizing: RELIANCE Q1 earnings 2026

Title: Reliance Q1 2026: Net profit β‚Ή15,000 cr, beats
Summary: Reliance Industries reported Q1 net profit of β‚Ή15,000 crore,
beating analyst expectations. Revenue rose 8% YoY to β‚Ή2.2 lakh crore.
Oil refining margins improved 2.3% on strong global prices.

Key Facts:
  β€’ Net profit Q1 2026: β‚Ή15,000 crore
  β€’ YoY revenue growth: 8%
  β€’ Refining margin improvement: 2.3%

Sources:
  - https://reuters.com/business/reliance-q1-2026...
  - https://economictimes.indiatimes.com/reliance...
  - https://thehinduinbusiness.com/reliance-q1...

Robustness: handling real-world failures

Real websites are messy. Cloudflare blocks bots. HTML is broken. Pages load slowly. Your agent must survive all of it.

Timeouts and retries

Set a strict timeout (8–10 seconds) on HTTP requests. If a site is slow, skip it and try the next search result. Ollama timeouts are built in; if the LLM hangs, return a default fallback.

Graceful degradation

If all fetches fail, your function returns None and you inform the user. Never crash. Never return hallucinated facts. If you can't fetch, admit it.

Ethics: respect robots.txt and User-Agent

Always identify your bot in the User-Agent header. Check `robots.txt` before scraping (for production). Don't hammer a site with 100 requests per second. For this tutorial, we're scraping public news and blog postsβ€”low risk. But in production, respect terms of service.

Ethical scraping checklist:

1. Set a realistic User-Agent (not "Mozilla only")
2. Respect robots.txt
3. Add delays between requests to large datasets
4. Never scrape personal data (emails, phone numbers)
5. Check the site's ToS before automation

Worked examples: two real queries

Example 1: Search + summarize

User: "What's happening with M-Pesa in Ethiopia?"

Agent flow: search_web("M-Pesa Ethiopia") β†’ get 3 results β†’ fetch the top one β†’ LLM analyzes text β†’ output PageSummary with facts and sources.

Example 2: Direct URL + summarize

User: "https://www.bbc.com/news/business-68573849" (a direct link)

Agent flow: detect URL β†’ fetch directly β†’ no search needed β†’ LLM analyzes β†’ output PageSummary.

How production agents work

This summarizer is a tiny agent. Production agents scale to 10+ tools (APIs for payment, email, calendar, databases), persistent memory, multi-step reasoning, and error recovery. But the pattern is identical: LLM reasons β†’ picks tool β†’ tool runs β†’ LLM reads result β†’ repeat. Every search engine, customer service bot, and financial trading system uses this exact architecture. You've built the real thing.

ReAct loops Multi-tool routing Web scraping Graceful failure Structured outputs
🎯 Challenge: Build a stock news agent

Task: Modify the summarizer to accept a stock ticker (e.g., "RELIANCE.NS") and search for recent news, then return a summary with 3 key facts.

Hints:

1. Detect if input is a ticker (ends with ".NS" or ".NSE")
2. If ticker, search for "[TICKER] news 2026"
3. Fetch the top 2 results and merge their facts
4. Return a PageSummary with all sources

Hint: Add a function `is_ticker(s)` that checks if the string matches a stock ticker pattern. Then in the main agent, branch: if ticker, search "[ticker] news"; else use the normal search flow. Fetch and summarize as usual. Test with "TCS.NS news".

Test your knowledge

Question 1 of 5
What are the two core tools in this summarizer agent?
Question 2 of 5
Why limit fetched page text to ~3000 characters?
Question 3 of 5
What happens if fetch_page fails with a timeout or bot detection?
Question 4 of 5
How does the agent decide between search_web and fetch_page?
Question 5 of 5
Why use Pydantic for the PageSummary output?
0
/5
Excellent! You've mastered multi-tool agents.
Africa & India's Best AI Academy
πŸ€– AI-first πŸŽ“ Placement Assistance

You've built a web agent.
Now ship production systems.

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 β†’