Chapter 20. The Agent Loop by Hand: Claude Messages and OpenAI Responses
In this section
- Chapter 20. The Agent Loop by Hand: Claude Messages and OpenAI ResponsesOpen the agent with nothing hidden: a model only asks for a tool, and your code runs it, keeps the whole conversation and decides when to stop.
- 20.1 What an agent is, in codeAn agent in code is a loop around a model API: call, keep, check, run and return, with three decisions that belong only to your code.
- 20.2 A tool is a name, a description and a schemaA tool definition's name, description and JSON Schema, what strict tool use guarantees and what it never can, and how free the model is to choose a tool.
- 20.3 One turn: request, response and stop reasonOne turn of the loop: what a request carries, what a response holds, and how its stop reason decides what happens next.
- 20.4 Running the tools and sending back the resultsRunning each tool the model asked for, and returning one result per call, matched by id, all together and first, failures included.
- 20.5 The conversation is an array you ownWhy a stateless API leaves the whole conversation in your program, and why everything the model produced goes back unchanged.
- 20.6 The same loop, different shapesThe same five steps on a second API, kept stateless: items instead of blocks, arguments as a string to parse, and four differences in how a tool call travels.
- 20.7 What a stateful mode hidesWhat a stateful mode keeps on the provider's side, what it saves and does not save, and what you can no longer see.
- 20.8 The stops you add yourselfA turn limit, a cost cap and a time limit checked before every model call, a deadline on each request, and the tools a worker may run.
- 20.9 When the loop goes wrongSorting every failure by whose fault it is: fix your own, retry the service's with backoff, show the answer when the model declines, and record each request id.
- 20.10 Reading a traceA trace with a span for every model call and tool call: what it proves about a run, and what it never can.
- 20.11 Streaming (advanced path)A response streamed in pieces: show text as it arrives, and join a tool call's input and parse it only when its block ends.
- 20.12 Tokens, caching and what a call costs (advanced path)Reading what each call costs in tokens, and how prompt caching makes the repeated start of every request cheaper.
- 20.13 Versions, models and keysPinning the API version, the SDK, the model, the prompt and the prices, and keeping one key per project with a spending limit.
- 20.14 How today's tools realize itHow the Claude Messages API, OpenAI's Responses API and an open model behind a local server realize the loop today.
- Field guide: the two APIs and an open model, step by stepThe repository and its secrets, accounts and keys, the connector by either route, the two APIs' exact shapes, stateful modes, prices and caching, and Ollama with an open model.
- Lab 20: the agent loop by handWith coding agents, explain, run, stop, debug and trace the AP worker's loop on two APIs and an open model, in TypeScript and Python, and ship it.
- Check yourselfRecall and practice for the whole chapter: the flashcards, twenty-one questions with outline answers, and a final quiz round.