agentic-ai-course

Module 13 — Extended Thinking

For most of this course, Claude answers directly from a single forward pass. For genuinely hard multi-step problems — a tricky logic puzzle, a multi-constraint plan, a subtle bug — giving the model explicit room to reason before committing to an answer improves correctness, at the cost of extra latency and tokens.

What you’ll learn

The core idea

response = client.messages.create(
    model=MODEL,
    max_tokens=budget_tokens + 1024,
    thinking={"type": "enabled", "budget_tokens": budget_tokens},
    messages=[{"role": "user", "content": prompt}],
)

for block in response.content:
    if block.type == "thinking":
        thinking_text = block.thinking
    elif block.type == "text":
        answer_text = block.text

response.content now contains a thinking block ahead of the usual text block. The thinking content is genuinely how the model worked through the problem — useful for debugging why an answer came out a certain way, or for showing your own users a “reasoning trace” — but it’s not meant to be re-parsed as instructions or trusted as a citation; treat it as a window into the process, not a second answer.

One constraint worth knowing: when thinking is enabled, temperature is fixed at 1 — the API will reject a different value, since thinking mode manages its own sampling. And in multi-turn tool-use conversations, thinking blocks generally need to be passed back unmodified as part of the assistant’s message history (not stripped or edited) for later turns to remain valid — check the current API reference before relying on this in a tool-using agent, as the exact requirement can shift between model versions.

Run it

pip install -r requirements.txt
export ANTHROPIC_API_KEY=your_key_here
python exercise.py   # after filling in the TODOs
python solution.py   # to compare against a working version

Try lowering budget_tokens on a hard prompt and see the reasoning get visibly more rushed.

Next

Module 14 — Deploying an Agent as a Service — wrapping an agent in an HTTP API instead of a CLI chat loop.