Lesson 10 Building Agents
Theme:

Lab Guide — Building Agents

Three exercises building a research agent with mock tools, a ReAct loop, and multi-task testing.

Setup: Open lab.pdf in Jupyter. Run the setup cell — it loads your API key and defines mock data. The notebook provides MOCK_SEARCH_DB and MOCK_URL_DB dictionaries so the agent runs without real web access.

📥 Lab Notebooks

⬇ lab.pdf ⬇ lab_answer.pdf

Exercise 1 — Build Mock Tools

EX 1

Implement 4 tools: search, read_url, save_note, get_notes

Build the tool execution functions and define their tool schemas for Claude's tool-use API.

Tool specifications

ToolParametersBehaviour
searchquery: strLook up query in MOCK_SEARCH_DB; return matching results or "No results found"
read_urlurl: strLook up URL in MOCK_URL_DB; return content or "URL not found"
save_notekey: str, content: strStore content in a notes dict under key; return confirmation
get_notes(none)Return all saved notes as a formatted string

Test your implementation

print(execute_tool("search", {"query": "python async"}))
# Should return mock search results

print(execute_tool("save_note", {"key": "test", "content": "hello"}))
# Should confirm note saved

print(execute_tool("get_notes", {}))
# Should show the saved note

Checkpoint: All four tools work correctly with mock data.

Exercise 2 — The ReAct Agent Loop

EX 2

Implement run_agent(task, tools, max_steps=10)

Build a full ReAct agent loop that:

  1. Sends the task to Claude with the tool definitions
  2. Uses a system prompt that instructs Claude to think step by step
  3. Handles tool_use stop reason: execute tools, feed results back
  4. Handles end_turn: extract and return the final text
  5. Enforces max_steps guard
  6. Prints each step (thought, action, observation) to the console

System prompt suggestion

SYSTEM = """You are a research agent. You have access to tools for searching,
reading URLs, and saving notes. Follow this process:
1. Think about what information you need
2. Use tools to gather information
3. Save important findings as notes
4. When you have enough information, provide your final answer

Be systematic and thorough."""

Test your implementation

result = run_agent(
    "Research Python async programming. What are the key concepts? Save your findings.",
    TOOLS
)
print("FINAL:", result)

Checkpoint: Agent completes the task in multiple steps, using search and save_note tools.

Exercise 3 — Multi-Task Testing

EX 3

Run 3 tasks and analyse step count

Test your agent on three different task types and compare how many steps each requires.

Test tasks

tasks = [
    # Research task: requires search + read + save
    "Research Python type hints. Find key concepts and save a summary.",

    # Comparison task: requires multiple searches + analysis
    "Compare Python lists vs tuples. Search for information about both and explain the differences.",

    # Planning task: requires gathering info + synthesis
    "Create a study plan for learning Python web development. Research the key frameworks and tools needed.",
]

What to record

Checkpoint: You have a comparison table showing step count, tools used, and quality rating for all 3 tasks.

Tip: If the agent gets stuck in a loop, check your tool result formatting. Make sure tool results are clear and informative — vague results like "OK" do not give the agent enough signal to decide its next step.