When an agent's reply goes wrong, it goes wrong in a few recognisable ways. It stops mid-sentence. It takes a long time to show the first word. It quotes a ticket number with two digits swapped. It shows odd marks inside a name. Each gets filed as "the model did it", and several are not the model at all. Between the request your code sends and the words that stream back, one call passes through seven steps, and each of those complaints shows up at one of them. Knowing which turns a vague complaint into a fix that someone owns.

Thesis: Inside one call, the model never sees your messages, never copies an id and never learns that its reply was cut off. A template turns the messages into one document that stops where the reply begins, every id in the reply is drawn again piece by piece, and in a stream the stop reason arrives after the last word. Each of those facts points a familiar symptom at a step you can check.


The Example: One Call at a Service Desk

The call comes from an IT service-desk agent with one tool, search_tickets. A user asks "Which of last week's P1 incidents are still open?" On its first call the model asks for the search; the harness, the code around the model, runs it and sends a second call with the result (The Tool Call Is a Round Trip). This page opens that second call, because its reply repeats the ticket id from the context window (the window: everything the call sends the model). The loop of calls and the whole request have their own pages (Anatomy of a Turn, One Request, End to End); this one stays inside one call.

Step 1: Render the Call Into One Document

A chat template turns the list of messages your code sends into one sequence, with marker tokens where each role starts and ends (How the Model Reads the Window explains why roles reach the model only as markers). Here is the service-desk call in the harmony format that OpenAI documents for its open-weights gpt-oss models (harmony guide); the line breaks are only for reading.

<|start|>user<|message|>Which of last week's P1 incidents are still open?<|end|>
<|start|>assistant<|channel|>commentary to=functions.search_tickets <|constrain|>json<|message|>{"priority":"P1","status":"open"}<|call|>
<|start|>functions.search_tickets to=assistant<|channel|>commentary<|message|>INC-4471 | VPN gateway drops sessions | owner: Network Ops<|end|>
<|start|>assistant<|channel|>final<|message|>

Three things matter. The <|…|> markers are tokens your code never typed. The roles are plain words after <|start|>, and the tool speaks as functions.search_tickets. And the last line is open: the document stops just after the header of the assistant's reply. The model is not asked a question; it is handed a document that stops where a reply would start, and continues it. Hugging Face's chat templating guide warns that without that open ending a model may do "something strange, like continuing the user's message instead of replying to it".

The example is abridged (no system header, tool definitions or reasoning) and filled in at one place: the documented prompt ends at a bare <|start|>assistant, after which the model writes its own header, naming a channel such as analysis for its chain of thought. The final-answer header here (<|channel|>final<|message|>) lets the walk start at the answer.

On a hosted API this step runs on the provider's servers, out of sight. When you serve a model yourself, the template is a file you deploy, and a mismatched one is your bug.

Step 2: Cut It Into Numbered Pieces

A tokenizer cuts the document into tokens, each an id in a fixed vocabulary. The counts here come from OpenAI's open-source library tiktoken, using the o200k_harmony encoding the format above belongs to. The question alone is 11 tokens: Which, of, last, week's, P, 1, incidents, are, still, open, ?. The ticket id INC-4471 is four: INC (58311), - (12), 447 (39596) and 1 (16). The whole abridged call is 73 tokens, and 15 of them are markers that fill the window though you never typed them (ending at the documented bare <|start|>assistant instead, it is 70 and 13). Tokens Are the Unit of Account covers how counts vary and what they cost.

Step 3: Read Everything in One Pass

The model reads all 73 positions in one parallel pass, which serving guides call prefill, and keeps each position's keys and values for the rest of the call in the KV cache. What matters here is the clock: prefill is part of the wait before the first word, and it grows with the window. NVIDIA's guide to LLM benchmarking says time to first token "generally includes both request queuing time, prefill time, and network latency". A provider's prompt cache lets a later call skip a start it has already read (What a Prompt Cache Reuses).

Steps 4 to 6: Score, Draw, Stream, One Token at a Time

From here the call repeats one short cycle for each token of the reply.

Score. The model takes the last position and gives every token in its vocabulary a score, called a logit. After the first token, scoring computes one new position against the cache; the model does not read the window again (prefill and decode). Two of the seven steps run the model, the read and this one, and this is the only one whose output is the trained model's choice.

Draw. The sampling code, the provider's or yours, turns the scores into probabilities and draws one token. Temperature and top-p reshape that draw. From Logits to Response has a lab for them, and explains why the newest models from Anthropic, OpenAI and Google no longer let you set them (OpenAI's when reasoning is on; as of November 2026) and why even temperature 0 was never fully deterministic.

Stream. A decoder turns the drawn id back into text and sends it at once, so the first words reach the screen while later ones are still being drawn. (Here "decoder" means only id to text, not the token-by-token phase serving guides call decode.)

One stream detail causes real trouble: a token can be part of a character. The tokenizer works on bytes, and the German capital "ẞ" comes out of o200k_harmony as two tokens, neither valid text alone. Decoded one at a time they give two replacement marks (�); decoded together, the letter. tiktoken's own decode function warns that "decoded bytes are not guaranteed to be valid UTF-8" (source). On a hosted API the provider's code buffers the bytes; if you serve a model yourself, yours must.

The Id Is Drawn Again

Say the reply is "INC-4471 is still open. Owner: Network Ops." That is 13 tokens, and the first four are the same four pieces as in the window. They are not carried across: each is a separate score and a separate draw, like the word "open" after them. The id in the window is what can make those pieces likely; no code copies the string. Set beside a near miss, they part at one draw:

Draw INC-4471 (the tool's result) INC-4417 (a near miss)
1 INC (58311) INC (58311)
2 - (12) - (12)
3 447 (39596) 441 (34196)
4 1 (16) 7 (22)

Token ids from tiktoken's o200k_harmony encoding.

If the reply names INC-4417, the third draw went wrong one of two ways: the scores put 441 on top, or the sampler drew it from lower down. Log probabilities tell which, where the server returns them: vLLM, an open-source serving engine, does (sampling parameters), and some hosted models no longer do (From Logits to Response lists one). Either way the fix is a check in code that every id in the reply appears in the tool's data, before the reply lands anywhere.

Step 7: Stop

The end of a reply is drawn too (From Logits to Response): in the format above, <|return|> (id 200002) ends a finished answer and <|call|> (id 200012) a tool request. Or code ends it, and the model chose nothing: a stop sequence your code gave the server appears in the text, or the reply reaches the output limit your code set (max_tokens on Anthropic's API; the name differs by provider). The server reports which happened as the stop reason.

In a stream, the stop reason arrives last. In Anthropic's streaming guide, the first event of a streamed message carries "stop_reason": null, and the real value comes in a later message_delta event, after the text. A screen that shows each word as it arrives has shown a cut-off reply before it can know. The model never learns either; it was simply not asked for another token. What the harness should do with each value is in Anatomy of a Turn and Anthropic's stop-reason guide.

Try It: Follow the Call

Without JavaScript, the table follows the same call (token ids from tiktoken's o200k_harmony encoding; the reply was written for this example, and no model was run).

Step What happens to the service-desk call Who runs it
1. Render Three messages become one document with 15 markers, ending at an open assistant header The template (the provider's, or yours)
2. Tokenize 73 token ids; INC-4471 is INC - 447 1 The tokenizer
3. Read All 73 positions in one pass, into the KV cache The model
4. Score Every possible next token scored; after the first, one new position against the cache The model
5. Draw One token drawn, weighted by the scores The sampling code
6. Stream The token turned back into text and sent The decoder
7. Stop A stop token, stop sequence or length limit; the stop reason arrives last The serving code

Score and draw run 14 times here, the 14th drawing the stop token; 13 tokens stream. With the stop token counted against the length limit, a limit of 6 gives "INC-4471 is still", and a limit of 13 delivers every word but stops before the 14th draw, so the stop reason says the limit was reached. Both look like a finished stream until the stop reason arrives.

Which Step Owns the Symptom

Each symptom points to a step to look at first. These show at the grain of tokens; failures elsewhere, such as a fact never in the window, are on One Request, End to End's card.

Symptom Look first at Where to look
The reply stops mid-sentence Stop: the length limit The stop reason, read before the reply is used
A long wait before the first word Read (after queueing and the network) Window length, prompt-cache hits, time to first token
The words come slowly once started Score (each new token) The time between tokens; a long reply multiplies it
A ticket number with two digits swapped Score or draw, with the right id in the window Log probabilities, if returned; a code check of every id against the tool result
A different answer to the same call Draw; even at temperature 0, where a model still allows it, server batching shifts the scores slightly The logged replies over several runs (From Logits to Response)
Replacement marks inside names Stream: decoding part of a character The decoder's byte buffering
The reply carries on the user's message, or ignores roles Render: wrong or incomplete template The rendered document against the model's published template

When the Steps Blur

The fair objection is that seven steps are partly a teaching split. Read and score are one forward pass, which One Request, End to End counts as one stage; sampler, decoder and stream can sit in one loop of serving code; and a reasoning model or a strict output schema changes what gets drawn and streamed. On a hosted API you can open none of these steps, so "look at the step" can sound like advice for people who run their own servers.

The split holds because the evidence a hosted API returns falls along the same lines. Usage counts show the tokenizer's work, time to first token the read, log probabilities (where offered) the scores against the draw, stream events the decoder's output, and the stop reason the stop. A symptom that matches one of those records belongs to its step even when you cannot see inside it. Some stays out of reach: the rendered document, hidden sampler settings, log probabilities a model does not return.

For a prototype whose replies go nowhere that matters, none of this is worth the time. It pays the first time a reply is written to a ticket, an email or a record, and even then two things stay yours on any API: reading the stop reason, and checking the reply before it lands.

An Example: An Insurer Serves Its Own Model

An insurer keeps claim data on its own servers, so it serves an open-weights model there (the insurer and its numbers are invented for this example). Its claims assistant streams answers into the case screen and writes a summary note to each claim. In the first month four complaints arrive, all filed as "the model".

Notes that end mid-sentence. The integration saved each note when the stream closed, never reading the stop reason, so summaries cut at the length limit were saved as finished. The stop step: save a note only on a finished stop reason, and flag the rest.

A wrong claim number. A note cited CLM-20817; the record in the window said CLM-20871. The two share every token but the last (208, then 17 against 71), so they parted at one draw, through the scores or the sampler. The fix does not wait to learn which: code checks every claim number in the note against the tool's data and holds the note on a miss.

Broken characters in policyholders' names. The team's streaming code decoded each token alone, so characters split across two tokens showed replacement marks. The stream step: buffer bytes until they form whole characters.

Replies that carried on the user's sentence. After a model upgrade, the old model's chat template stayed in place, and the new model read markers it was never trained on. The render step: version the template with the model, and rerun the evaluation set after any swap.

Three of the four were code from the start. The fourth came from the scores or the draw, and its fix is code too.


What to Do Next

  • Read the stop reason before you use a reply. Treat a stream as unfinished until it arrives, and never save a length-limited reply as complete.
  • Check every id a reply repeats against the data in the window before it lands. Ticket, claim and account numbers are drawn piece by piece.
  • Measure two latencies. Time to first token points at the read and the window's size; the time between tokens, times the reply's length, is the rest.
  • Keep log probabilities where you can get them. Logged with the reply, they show whether a wrong token came from the scores or the draw.
  • If you serve a model yourself, own the code around it. Version the chat template with the model, log the rendered document, buffer bytes in the decoder.

Related reading: One Request, End to End maps the whole request around this call. Tokens Are the Unit of Account, How the Model Reads the Window and From Logits to Response open the tokenize, read and draw steps; What an LLM Actually Is gives the plain picture of next-token prediction. Terms are in the glossary.