Week 1 · Deep dive · ~2 hours

From Chatbot to Agent — every line, explained

This is the Week 1 agent, taken apart one line at a time for someone who has never built an agent. Tap any line of code to see what it does in plain English; tap go deeper ▾ for the nerdy detail. You don't have to memorise it — you need to be able to point at a line and say what it's for.

👆 Tap a line → plain English 🔎 go deeper → extra detail # dim italics = a comment (Python ignores it)
Goal
what the agent is trying to do
Loop
reason → act → observe → repeat
Tool
a function the agent can call
Memory
the notes it carries between steps
Run it right here. Every cell has a ▶ Run code button that runs the Python in your browser — no install, no API key, no account. The cells share memory, so run them in order, 1 → 4. The first Run downloads Python one time (~10–15 MB), then it's cached.
Why a “mock” LLM? A real agent's brain is a live API call to Claude or GPT — which needs a secret key and the internet, so it can't safely run inside a web page. To let you see the whole loop with zero setup, we swap the brain for a tiny hand-written stand-in that returns the same shape of answer. The loop, the tools, and the memory are 100% real — only the brain is faked. The one-line swap to a real API is at the bottom of the page.
🔌 The real thing (for Colab). To make this a genuine agent, delete mock_llm and point the loop at a live model. The loop, tools, and memory don't change at all — that's the whole point of Law #1:
from anthropic import Anthropic
client = Anthropic()   # reads your ANTHROPIC_API_KEY

def real_llm(question, scratchpad):
    prompt = SYSTEM + "\nQuestion: " + question + "\n" + scratchpad
    msg = client.messages.create(
        model="claude-sonnet-4-6", max_tokens=300,
        messages=[{"role": "user", "content": prompt}])
    return msg.content[0].text

# then just: reply = real_llm(question, notes)
In the NB1 Colab notebook you'll do exactly this — same run_agent, real brain.
↩ Back to Week 1 Week 2 deep dive →