promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_

Wire the real model — swap fake_llm for the Anthropic SDK shape — step 9 of 9

Checkpoint

One last thing before we move on. pass this to mark the lesson done, or skip and keep moving. hop to the next when you're ready.

Final drill. Build the full agent loop using the real-SDK shape. Write run_agent(question, max_iters) that:

  • Loads the API key via os.getenv("ANTHROPIC_API_KEY") (mocked to a known value for the demo). If missing, return {"ok": False, "error": "missing key"} immediately.
  • Otherwise, builds messages = [{"role": "user", "content": question}] and loops up to max_iters times calling fake_create(messages) (which returns a MockMessage).
  • On response.stop_reason == "end_turn": collect all text blocks from response.content, join them with " ", return {"ok": True, "answer": <joined text>, "iters": <iteration count>, "tokens": <input + output>}.
  • On response.stop_reason == "tool_use": for each tool_use block, dispatch through TOOLS[block.name](**block.input) to get a result string. Append the assistant turn (passing response.content directly through) AND a user turn with tool_result blocks. Continue.
  • On cap exhaustion: return {"ok": False, "error": "capped", "iters": max_iters}.

Token tracking: sum response.usage.input_tokens + response.usage.output_tokens across every call. Include the running total in the success return.

Expected output:

ok=True iters=2 tokens=120 answer=Found Tokyo's best ramen, 2026 guide.

full-screen editor opens — close anytime to keep reading.