Chapter Two · The Model Call

The Model Call

The loop from Chapter 1 makes exactly one kind of request, and everything the agent does or fails to do passes through it. Six topics open that request up: the message array and the four roles inside it, the token arithmetic that explains the bill, why the same ticket gets two different answers, the one response field the loop branches on, how to get JSON that parses, and a single quarantined page where vendor names are allowed to live.

6 topics

Vera's forty-line agent works, in the sense that it answers the cracked-shelving ticket without crashing. What she cannot yet say is why it costs what it costs, why the same ticket produced a different sequence of tool calls the second time she ran it, or why one run ended with the sentence "Let me look that order up" delivered to the buyer as a resolution. All three answers are inside one HTTP request and the response it gets back, and none of them are visible through the SDK's typed objects.

So this chapter takes the SDK away. The call is a list of messages with roles, a list of tool definitions, and a handful of sampling settings; the response is one more message, a token count, and a stop reason. That is the entire interface a language model exposes, and it is the same interface underneath every framework in Chapter 14. Everything the rest of the book builds — memory, retrieval, planning, budgets, approval gates — is code that decides what goes into that array and what to do with what comes back.

Two of these topics carry numbers the rest of the book leans on. Topic 08 works the Sundry arithmetic in full and shows that a ticket whose context tops out at 4,880 tokens bills roughly 12,000 input tokens, because turn N re-sends turns 1 through N-1; that number is the reason Chapter 5 exists. Topic 10 is the stop reason, the field a correct loop branches on and the field a broken loop ignores. The chapter closes on the vendor surface, deliberately isolated on one page so that when a provider ships something next quarter, exactly one file in this book goes stale.

One request opened up, and rebuilt from scratch every turn
One HTTP request
everything the model will see this turn
system
who the agent is, and what it may not do
the stable prefix a cache can pay for
tools[]
nine schemas · about 680 tokens
identical every turn, billed every turn
messages[]
user, assistant and tool results, in order
grows by one exchange per turn
One message back
content blocks · token counts · stop reason
the loop branches on the stop reason, never on the text

Topics in This Chapter

Topic 07
Messages, Roles, and the Wire Format
System, user, assistant and tool as one array your code rebuilds from scratch every turn, with content as a list of blocks rather than a string. One Sundry turn shown as raw JSON, including the nine tool schemas that ride along on every single request.
Wire Format
Topic 08
Tokens, Windows, and the Bill
Why a four-call ticket with a 4,880-token context bills 12,000 input tokens, and why a twelve-turn one bills 88,000. Where a Sundry context actually goes, measured: tool results dominate, and the system prompt everyone edits is the smallest fixed cost on the page.
Cost
Topic 09
Sampling and Non-Determinism
The same context sent twice gives two answers, because the next token is sampled rather than read off. What temperature and top-p actually do, why setting temperature to zero does not buy determinism, and how to test a component that is legitimately stochastic.
Variance
Topic 10
Stop Reasons
The loop does not branch on the text, it branches on why the model stopped: finished, wants a tool, hit the output limit, or was blocked. A table from stop reason to loop action, including the turn that asks for two tools at once.
Control Flow
Topic 11
Structured Output
Three levels of enforcement — asking in the prompt, a schema the API validates against, and constrained decoding — and what each one does and does not remove. Plus the check none of them replace: a schema-valid refund for an order that does not exist is still a bad refund.
Schemas
Topic 12
The Vendor Surface, Quarantined
The one page in the first thirteen chapters that names providers: where the system prompt goes, how tools are declared, what the stop reason is called, and how tool results are attached across the major APIs as of 2026. Written to be thrown away and rewritten without touching anything else.
Vendor Surface