LLM Kitchen
Explanations

Wrap-up

Ch 43. From message to reply

One message

The recap showed what the pieces are. This chapter shows the order they run in, for one real message, โ€œFinish this: The cat sat on theโ€, sent to a small assistant with a few tools. Inside the model, the steps are shown with GPT-2 small, which runs them in the same order.

The recap showed what the pieces are. This chapter shows the order they run in, for one real message: โ€œFinish this: The cat sat on theโ€, sent to a small assistant with a few tools. The harness, the prompt, the tool call, and the reply come from that recorded run. Inside the model, each step is shown with GPT-2 smallโ€™s recorded run on the same last words: the stages run in the same order in both models, though the details differ.

Step through the stages in order, or jump to any box. Each one links to the chapters that explain it.

Step through

One message, start to finish

Recorded ยท Qwen2.5-1.5B-Instruct and GPT-2 small

Agent loop (Ch 29)

The harness

The model: a token loop (Ch 11), once per token until end-of-turn

Next token: back to box 4

The harness again

After a tool runs: back to box 2

Other kinds of models swap one piece:

1. You type a message

The app shows a chat, but nothing has reached the model yet. Your message is just text in the app.

  1. YouHi! Can you help me with a few things?
  2. AssistantOf course! What can I help you with today?
  3. YouFinish this: The cat sat on the

Read more: Ch 23: The model is not the product

Step 1 of 13 or use โ† โ†’ keys

Stage 1 of 13: You type a message

Two loops

Two loops run at once. Inside, the model writes one token at a time until itโ€™s done. Outside, the harness checks what the model wrote, runs any tool it asked for, adds the result, and calls the model again. Here the harness called the model twice: once to search for a nursery rhyme, and once to answer.

Two loops run at once. The inner one is the modelโ€™s: run once, pick one token, add it, and run again, until it writes an end-of-turn token. The outer one is the harnessโ€™s: read what the model wrote, and if it asked for a tool, run it, add the result, and call the model again. Here the outer loop ran twice: once to ask for a nursery rhyme search, once to answer with what came back.

Everything in both loops is plain text in one prompt: your message, the instructions, and the toolโ€™s result. Thatโ€™s why tool results canโ€™t be trusted blindly.

Everything that passes through both loops is plain text. Your message, the system prompt, the toolโ€™s result, and the modelโ€™s scratch work all end up as tokens in one prompt. That is why reusing the unchanged start saves so much work, and why a tool result has to be treated as untrusted.

Counting the runs

The first call read the 400-token prompt in one pass, then wrote its 21-token tool call one token per run until the harness stopped it at </tool_call>. The second callโ€™s prompt began with all 400 tokens of the first, so with prompt caching only the 93 new ones needed reading: 493 tokens of work instead of 893 (Ch 26). It then wrote its 59-token reply and an end-of-turn token, again one run each. Thatโ€™s about 80 runs of the model for one short message, and thanks to the KV cache each run worked through just one new token.

Recipe card

Recap

  • Sending a message runs the harness first, then the model once per token, then the harness again.
  • The modelโ€™s loop adds one token per run; the harnessโ€™s loop adds one tool result per call.
  • Everything the model sees, including tool results, is one stream of tokens in a single prompt.
  • Other kinds of models swap one stage and keep the rest of the order.
  • Two calls, about 80 model runs: with caching, the second call read only the 93 new tokens.

Next question: that is how one model works, end to end. When is it the right tool for a job, and when is something else better?