Lesson 10 Building Agents
Theme:
Lesson 10 · Slide 1

Building Agents

Agents are LLMs that can perceive, plan, and act autonomously using tools. This lesson covers the ReAct pattern, memory architectures, planning strategies, and failure modes.

Learning Objectives

Lesson 10 · Slide 2

What Is an Agent?

An agent is an LLM-powered system that autonomously decides which actions to take to accomplish a goal. The core loop:

  1. Perceive — observe the current state (user input, tool results, memory)
  2. Plan — decide what to do next (which tool to call, what to reason about)
  3. Act — execute the chosen action (call a tool, generate output)

This distinguishes agents from simple chatbots: a chatbot responds to one message. An agent pursues a goal across multiple steps, making autonomous decisions about which tools to use and when to stop.

Key insight: The LLM is the "brain" — it does the planning and reasoning. Tools are the "hands" — they let the agent interact with the world. The loop connecting them is the agent framework.

Lesson 10 · Slide 3

The ReAct Pattern

ReAct (Reasoning + Acting) interleaves chain-of-thought reasoning with tool actions. Each step has three parts:

Thought → Action → Observation

  • Thought: The LLM reasons about what it knows and what it needs to find out
  • Action: The LLM calls a tool to gather information or perform an operation
  • Observation: The tool result is fed back to the LLM as new context
Thought: I need to find information about Python pathlib.
         Let me search for it.
Action:  search("Python pathlib module")
Observation: "The pathlib module provides object-oriented
             filesystem path handling..."

Thought: Now I have the information. Let me save a summary.
Action:  save_note("pathlib", "pathlib provides OOP file paths...")
Observation: Note saved successfully.

Thought: I have the information and saved it. I can answer now.
Answer:  Python's pathlib module provides...
Lesson 10 · Slide 4

Memory Types

Agents need memory to maintain state across steps and sessions.

TypeWhereLifetimeUse Case
In-contextConversation messagesCurrent sessionTool results, intermediate reasoning
External KVDictionary / databasePersistentNotes, facts, user preferences
EpisodicVector storePersistentPast conversations, learned patterns

In-Context Memory

The simplest form: the full conversation history (messages list) serves as working memory. The agent can "remember" tool results from earlier steps because they are still in the context window.

Limitation: Context windows are finite. Long agent runs can exceed the limit, requiring summarization or truncation strategies.

Lesson 10 · Slide 5

Planning

Complex tasks require the agent to decompose the problem before acting.

Task Decomposition

Ask the LLM to break a complex task into sub-tasks before executing any of them. This reduces errors by making each step simpler and more focused.

Task: "Compare Python and Rust for web development"

Plan:
1. Search for Python web frameworks
2. Search for Rust web frameworks
3. Compare performance characteristics
4. Compare developer experience
5. Write comparison summary

Replanning

When a step fails or produces unexpected results, the agent should be able to revise its plan. This requires the LLM to assess whether the current approach is working and adapt if needed.

Practical tip: Include a system prompt that says "After each tool result, briefly assess whether you are making progress toward the goal. If not, reconsider your approach."

Lesson 10 · Slide 6

Failure Modes

Agents fail in predictable ways. Knowing these patterns lets you build defenses.

Common Failures

  • Infinite loops: Agent calls the same tool repeatedly with the same input
  • Context overflow: Too many steps exhaust the context window
  • Wrong tool selection: Agent picks an inappropriate tool for the task
  • Hallucinated tool calls: Agent invents tool names or parameters that do not exist

Mitigations

  • Max steps guard: Hard limit on loop iterations (e.g., 10-20)
  • Context budget: Summarize or truncate when approaching limit
  • Tool validation: Check tool name and parameters before execution
  • Graceful fallback: Return partial results if the agent gets stuck
Lesson 10 · Slide 7

Demo

Live demonstration: a research agent with search, read, and note-taking tools.

Demo Steps

  1. Define 4 mock tools: search, read_url, save_note, get_notes
  2. Give the agent a task: "Research Python async programming and save key points"
  3. Watch the ReAct loop: thought → action → observation
  4. Show the agent saving notes and using them in its final answer
  5. Trigger a max-steps guard by giving a vague, open-ended task
  6. Show graceful fallback when the agent cannot complete the task
Notes: Use mock data so the demo is deterministic and fast. Show the full message history at the end to illustrate how context grows with each step.
Lesson 10 · Slide 8

Lab & Homework

Lab Exercises

  1. Mock Tools — build 4 tools with mock data: search, read_url, save_note, get_notes
  2. ReAct Loop — implement run_agent(task, tools, max_steps=10)
  3. Multi-Task Test — run 3 tasks and analyse step count per task

Homework Tasks

  1. Persistent JSON memory across agent runs
  2. Max steps guard + graceful fallback
  3. Codebase analysis agent with file read/search tools
1 / ?