Free guide · PDF · 14 pages

Beyond the LLM Call

Designing the Python system around an AI agent.

Calling an LLM takes three lines of Python. The hard part is the code around the call: what happens when the answer is wrong, how you test it without spending tokens, and how you stop an agent from looping forever.

This guide takes one small agent, an expense classifier, from a 20-line demo to a system you can change and test. Each chapter starts with a way the demo breaks and shows the design that fixes it.

Cover of the guide: Beyond the LLM Call

What's inside

  1. An agent is plumbing. Model, instructions, memory, tools and a loop. Only one of those is the LLM.
  2. The 20-line classifier. The version most tutorials end with, and why it returns a sentence you can't use.
  3. Structured output. Treat the model's reply as untrusted input: Pydantic and a StrEnum at the boundary.
  4. The LLM behind a Protocol. Switch providers and test without an API key.
  5. One service, many interfaces. A service layer and a repository, so the CLI, bot and API share one copy of the logic.
  6. The model proposes, you decide. Send low-confidence answers to a human instead of into your database.
  7. The loop and its guards. A 15-line agent loop, and the limits that stop it from running up your bill.

Every chapter has a diagram, a short code snippet and one question to think through for your own app. It's for Python developers who have called an LLM API and want to know what comes next.

Bob Belderbos Juan José Expósito González Written by Bob Belderbos & Juanjo Expósito