Lesson 10 Building Agents
Theme:

Homework

Three tasks extending the agent with persistent memory, robustness features, and a real-world application.

Complete all three lab exercises before starting the homework. These tasks build on the ReAct agent from the lab.

📥 Reference Solution

⬇ lab_answer.pdf

Task 1 — Persistent JSON Memory

Objective

Add persistent memory that survives across agent runs using a JSON file.

Instructions

  1. Modify save_note to also persist notes to agent_memory.json
  2. Modify get_notes to load from the JSON file on first call
  3. Run the agent twice with related tasks:
    • Run 1: "Research Python decorators and save key points"
    • Run 2: "What did I learn about decorators? Build on that knowledge and research advanced decorator patterns"
  4. Verify that Run 2 can access notes saved in Run 1

Deliverable

A notebook showing both runs, the JSON memory file contents, and evidence that the second run used the first run's notes.

Task 2 — Max Steps Guard with Graceful Fallback

Objective

Make the agent robust against infinite loops and context overflow by implementing a graceful fallback.

Instructions

  1. Enhance run_agent to detect when it hits max_steps
  2. On hitting the limit, instead of returning a generic error:
    • Summarize what the agent has accomplished so far
    • Return any notes that were saved during the run
    • Indicate what steps remain incomplete
  3. Test with a deliberately vague task and max_steps=3 to trigger the fallback
  4. Add duplicate-action detection: if the agent calls the same tool with the same arguments twice in a row, inject a message saying "You already tried this. Try a different approach."

Deliverable

A notebook showing the enhanced agent handling both max-steps and duplicate-action scenarios gracefully.

Task 3 — Codebase Analysis Agent

Objective

Build an agent that can analyse a codebase using file read and search tools.

Instructions

  1. Define two new tools:
    • read_file(path) — reads and returns the content of a file (use real file I/O)
    • list_files(directory, pattern) — lists files matching a glob pattern
  2. Point the agent at any Python project on your machine (or use the lesson files in this course)
  3. Test with tasks like:
    • "Analyse the project structure and summarize what each module does"
    • "Find all functions that take more than 3 parameters"
    • "Identify potential issues or code smells in the codebase"
  4. Document the agent's step-by-step process and evaluate its analysis quality

Deliverable

A notebook with the codebase agent, 2-3 test tasks, agent traces, and a brief evaluation of the quality of its analysis.

Security note: Be careful with the read_file tool. Restrict it to a specific directory to prevent the agent from reading sensitive files. Never give an agent write access to the filesystem without explicit safeguards.

Submission Checklist