Build an AI Research Agent0% done
← PrevNext →
Lesson 3 of 6~2 min read

Lesson 2 — Setting Up Your Tools

You need two things: a way for the AI to search the web, and a way for it to read web pages.

Lesson 2 — Setting Up Your Tools

You need two things: a way for the AI to search the web, and a way for it to read web pages.

Install dependencies:

pip install anthropic tavily-python requests beautifulsoup4

Get your API keys:

  • Anthropic: console.anthropic.com (you may already have this)
  • Tavily: tavily.com (free tier gives 1,000 searches/month — plenty)
  • Create a `.env` file:

    ANTHROPIC_API_KEY=your_key_here

    TAVILY_API_KEY=your_key_here

    Define your tools in Python:

    import os

    import anthropic

    from tavily import TavilyClient

    import requests

    from bs4 import BeautifulSoup

    anthropic_client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

    tavily_client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])

    def search_web(query: str, max_results: int = 5) -> list:

    """Search the web and return results with titles, URLs, and snippets."""
    results = tavily_client.search(query, max_results=max_results)
    return results["results"]

    def read_page(url: str) -> str:

    """Fetch a webpage and return its text content."""
    try:
    resp = requests.get(url, timeout=10, headers={"User-Agent": "Mozilla/5.0"})
    soup = BeautifulSoup(resp.text, "html.parser")
    # Remove nav, footer, scripts
    for tag in soup(["script", "style", "nav", "footer", "header"]):
    tag.decompose()
    text = soup.get_text(separator="\n", strip=True)
    # Limit to first 3000 chars to stay within context
    return text[:3000]
    except Exception as e:
    return f"Error reading page: {e}"

    These two functions are your agent's eyes and hands. The AI will call them as it sees fit.

    ← PREVIOUSLesson 1 — What Makes Something an AgentNEXT →Lesson 3 — Building the Agent Loop