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.pdfTask 1 — Persistent JSON Memory
Objective
Add persistent memory that survives across agent runs using a JSON file.
Instructions
- Modify
save_noteto also persist notes toagent_memory.json - Modify
get_notesto load from the JSON file on first call - 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"
- 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
- Enhance
run_agentto detect when it hitsmax_steps - 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
- Test with a deliberately vague task and
max_steps=3to trigger the fallback - 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
- 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
- Point the agent at any Python project on your machine (or use the lesson files in this course)
- 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"
- 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
- Task 1: Persistent JSON memory; two sequential runs; evidence of cross-run memory
- Task 2: Graceful fallback on max_steps; duplicate-action detection; test cases
- Task 3: Codebase agent with read_file and list_files; 2-3 test tasks; evaluation
- All code runs without error after kernel restart + run all