Week 4 · Build, debug, iterate
You've got the parts. Today it's a hackathon: you build your own agent for real. But real agents break — so first you'll learn to debug them (spot the three failure patterns fast), then build in structured phases with check-ins so nobody stays stuck.
What you'll walk away with
Part 1 · Theory (20 min)
An agent has more moving parts than a chatbot — the model, the tools, and the prompts wiring them together — so there are more places for things to go wrong. The good news: almost every bug falls into one of three patterns. Learn to name them and you'll fix them fast.
The agent invents a fact or an answer instead of using a tool to find out. Sounds confident, but it's made up.
A tool crashes, returns junk, or gets called with the wrong arguments — e.g. a date passed where a number belongs.
Vague instructions → wrong behavior. "Summarize this" with no length or focus gives a rambling, off-target answer.
When something's off, ask: did it make it up, did a tool break, or was the prompt unclear? That names your fix.
A crash is obvious — you see the red error. A hallucination is sneaky: the agent answers smoothly and looks right, so it slips past you. That's exactly why the next two habits matter — they force the agent to prove its answer instead of just asserting it.
Before you believe an agent's answer, check it. Three cheap habits catch most problems:
An agent loops silently inside your code. If you can't see what it did, you can't fix it. So log every step: print or record each Thought → Action → Observation. Then when the answer is wrong, you scroll back and spot the exact step where it went off the rails.
# with logging, the failure is obvious: Thought → "I need the average age from the dataset." Action → mean_age("Age") Observation → 320.0 # 👈 impossible — a bug lives here Thought → "That looks wrong. Re-check the column."
Every agent bug is a hallucination, a tool error, or a prompt issue. Log the loop so you can see it, validate the output so you can trust it.
Part 2 · Practical (100 min)
Now you build your own agent. It's a hackathon, but a structured one — timed phases with class check-ins so momentum never dies and nobody stays stuck alone. Start from the reusable scaffold in the Week 4 notebook (agent template with a tool registry, logging, and try/except error handling built in).
Get the smallest thing working end to end: the core agent loop, your first working tool, and your primary data source. Don't polish — just get one full Thought → Action → Observation cycle to run and log.
Pause the build. 2–3 students share their screen and their logs. As a class, spot and solve one common integration challenge (loading data, calling a tool, reading a key) — whatever most teams are hitting. One fix, everyone benefits.
Now make it real. Add a second tool or data source, wire in basic error handling (so one bad tool call doesn't kill the run), and get the full end-to-end workflow answering your project's question.
def run_tool(name, args): log(f"Action → {name}({args})") # log every call try: result = TOOLS[name](**args) # tool registry lookup except Exception as e: result = f"TOOL_ERROR: {e}" # don't crash the loop log(f"Observation → {result}") return result
Everyone submits their top blocker. The class votes on 2, and we solve them together on the big screen — reading logs, naming the failure type, and fixing it live. You'll leave with a template for how to debug the next one yourself.
Checkpoint
Your agent confidently reports "the average passenger age was 320." Nothing crashed. What kind of bug is this, and what catches it?
No exception fired, so try/except won't help here — the tool "succeeded" but returned nonsense (or the agent hallucinated it). This is why validation matters: a quick sanity check ("is an age of 320 even possible?") plus a logged trace lets you catch and locate an impossible answer that looks confident.
Right before the build, spend 12–15 minutes hunting bugs — it primes everyone to spot the exact failures they're about to hit in their own code.
Run it · 12–15 min · teams of 3–4
Each team gets a short buggy agent transcript with planted problems: a hallucinated fact, a tool called with a wrong argument, a vague prompt, and an unhandled error. Teams race to find each bug, classify it (hallucination / tool error / prompt issue), and propose a one-line fix. Round 1 is an easy transcript; Round 2 is trickier. The debrief maps each bug type to a fix habit — verify, validate tool inputs, tighten the prompt, add try/except + logging.
Coach: open the printable card → Bug Hunt (green energizer sheet, ready to print).
Recap. Agents fail in three ways — hallucinations, tool errors, prompt issues. You beat them by logging the loop (so you can see it) and validating outputs (so you can trust it). Then you ran a structured hackathon — Foundation, check-in, Expansion, debugging clinic — to build an MVP with at least two tools. Next week: polish it and present.