What happens in one model call
Priya is the engineer who owns the cooking assistant in Cookvale, a recipe app. The assistant answers questions while a person cooks one recipe: what to substitute, how long to wait, whether something is done. The answer streams into a chat panel, a few words at a time.
The assistant goes live. The first real user stops cooking and types eight words:
Can I use butter instead of oil?
The answer box stays empty for almost a second. Then the words arrive quickly, one small piece after another. Priya's usage log for that call says:
{ "input_tokens": 12060, "output_tokens": 300 }On the bill line for the call, the input part is eight times the output part. But on the price list, every output token costs five times as much as an input token.
That gives three questions. This lesson answers the first; the other two follow in the next one:
- The user typed eight words. Where did twelve thousand tokens of input come from?
- Why was there a pause, and then a rush of words?
- Why is output priced higher per token, and why is input still the bigger part of the bill?
A token is one piece of text that the model reads or writes, often a short word or part of a word. The model stores each token as a number, its token ID. Tokens: what you pay for shows how text is cut into tokens. For now, a token is the unit the provider counts and charges for.
Running a trained model to get output is called inference, the word in this course's title. In the lessons we say "a model call", because that is what you write in your code.
What the Provider Actually Receives
Priya's code does not send eight words. It sends a JSON request with four kinds of content: tool definitions, a system prompt, the recipe the user is cooking, and the conversation so far. On turn 1, the conversation is only the new question.
Here is the request, trimmed. Long parts are replaced by a description in square brackets. The shape follows the Anthropic Messages API; other providers use other field names for the same parts.
{
"model": "mid-size-model",
"max_tokens": 1024,
"stream": true,
"tools": [
{
"name": "convert_units",
"description": "Convert an amount between units, for example cups to grams.",
"input_schema": {
"type": "object",
"properties": {
"amount": { "type": "number" },
"from_unit": { "type": "string" },
"to_unit": { "type": "string" }
},
"required": ["amount", "from_unit", "to_unit"]
}
}
],
"system": "You are the cooking assistant in Cookvale. [voice and house rules, food safety rules, unit conventions, six example exchanges]",
"messages": [
{
"role": "user",
"content": "[the recipe being cooked, plus three technique notes]\n\nCan I use butter instead of oil?"
}
]
}model picks the model. max_tokens is the most output tokens the call may produce. stream: true asks for the answer piece by piece. tools lists functions the model may ask Priya's code to run; the real list has eight (find_recipes, get_recipe, scale_recipe, convert_units, substitute_ingredient, set_timer, add_to_shopping_list, get_pantry), and one is shown. system is the system prompt: instructions that are the same for every user. messages is the conversation.
The recipe sits at the start of the first user message. A retrieved recipe means text the app looks up and pastes into the prompt: Cookvale finds the recipe being cooked, adds three short technique notes, and puts them in front of the question.
The Model Reads One String
A model has no idea what a "system prompt" or a "tool" is. It was trained on one long stream of tokens. So before it runs, the provider turns the JSON into one flat string. The program that does this is the chat template: a text format that wraps each message in special marker tokens.
Closed providers do this on their servers and do not publish their templates. Open models publish theirs. Qwen3 is an open model family whose template is a small program in the file tokenizer_config.json on Hugging Face. Here is the shape of the string it builds for Cookvale's request, using the template's own text, with the long parts cut:
<|im_start|>system
You are the cooking assistant in Cookvale. [... rules and examples ...]
# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{"type": "function", "function": {"name": "convert_units", ...}}
[... seven more tools ...]
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call><|im_end|>
<|im_start|>user
[... the recipe and three technique notes ...]
Can I use butter instead of oil?<|im_end|>
<|im_start|>assistantFour things to see in it.
<|im_start|>and<|im_end|>are marker tokens. Each is one token with its own ID, marking where a message starts and ends. The word after<|im_start|>says whose message it is. That is all a "role" is: a word inside the string.- The tools are text. The template writes every tool definition into the system message as JSON, with a paragraph telling the model how to ask for a call. The model reads those tokens on every call, and the provider bills them as input on every call. Qwen puts the tool text after the system text; other providers put tools first. The order varies. That tools become text does not.
- The string ends with
<|im_start|>assistantand a new line: an open message with no text yet. The model's job is to continue the string from there. - The model also has a way to stop. When it writes
<|im_end|>(token ID 151645 in Qwen3), the server stops and returns the text. The other way a call stops is reachingmax_tokens.
How much does a template add? The script below renders Cookvale's own two messages, a one-line system prompt and the user's question: 24 tokens of message text become 41 tokens after the Qwen3 template. Templates can add more than markers. The Llama 3.1 8B template on Hugging Face writes "Cutting Knowledge Date: December 2023" and "Today Date: 26 Jul 2024" into the system message unless the caller passes a date. On a two-message example of about the same size, that header was 20 of 51 tokens.
You can render a template yourself. This needs no GPU and no API key, and downloads only Qwen3-0.6B's tokenizer files:
pip install "transformers==5.20.1" "tokenizers==0.23.3" "jinja2==3.1.6"from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
messages = [
{"role": "system", "content": "You are Cookvale's cooking assistant. Answer in one or two short sentences."},
{"role": "user", "content": "Can I use butter instead of oil?"},
]
text = tok.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False,
)
print(text)
ids = tok(text)["input_ids"]
print(len(ids), "tokens with the template")
print(sum(len(tok(m["content"])["input_ids"]) for m in messages), "tokens of text alone")from_pretrained downloads the tokenizer and its chat template from the model's Hugging Face page. messages is the same list shape you send to a provider. apply_chat_template runs the template; tokenize=False returns the string so you can read it; add_generation_prompt=True adds the open <|im_start|>assistant line; enable_thinking=False is a Qwen3 option that adds an empty thinking block so the model answers directly. The next line cuts the rendered string into token IDs and counts them. The last line counts each message's text without any template and adds the counts.
Run on an Apple M1 Max laptop:
<|im_start|>system
You are Cookvale's cooking assistant. Answer in one or two short sentences.<|im_end|>
<|im_start|>user
Can I use butter instead of oil?<|im_end|>
<|im_start|>assistant
<think>
</think>
41 tokens with the template
24 tokens of text aloneThe template added 17 tokens to two short messages. Thirteen of them are the markers, the role words and the line breaks around them. The other four are the empty thinking block, <think>, a blank line, </think> and another blank line, which enable_thinking=False writes so the model skips straight to the answer.
Every Turn Sends Everything Again
The user keeps cooking and asks a second question. What does the provider receive now?
You might expect only the new question, because the provider "remembers" the chat. It does not. Nothing kept between calls changes what the model reads. Priya's code builds the whole request again: the same tools, system prompt and recipe, the first question, the first answer, and the new question. The template renders all of it, and the model reads all of it. Anthropic's documentation describes the input of a turn the same way:
"Input phase: Contains all previous conversation history plus the current user message"
So "conversation history" is a longer prompt, nothing more. In this course, the prompt is everything the model reads for one call: tools, system prompt, recipe, history and question.
Each later Cookvale turn adds the previous answer (300 tokens) and the new question (60 tokens):
each new turn adds 300 (previous answer) + 60 (new question) = 360 tokens
turn 1 input 2,000 + 6,000 + 4,000 + 60 = 12,060 tokens
turn 2 input 12,060 + 360 = 12,420 tokens
turn 3 input 12,420 + 360 = 12,780 tokens
turn 4 input 12,780 + 360 = 13,140 tokensOn turn 4, the model reads 12,780 tokens it already read on turn 3, all over again. Prompt caching shows how a provider can skip some of that work, and what it charges when it does.
Where the Twelve Thousand Came From
Now the first prediction. The user typed eight words. How many tokens will the provider bill as input?
Take a moment before you read on.
All of them, the whole prompt:
tool definitions (8 tools) 2,000 tokens
system prompt 6,000 tokens
retrieved recipe and technique notes 4,000 tokens
user question 60 tokens
------
turn 1 input 12,060 tokensThe user's words are the smallest part. Two thirds of the prompt, the tools and the system prompt (8,000 tokens), is the same for every user. The question band is a planning number: 60 tokens is the average question across the five languages Cookvale's users write in, and these eight English words are fewer. Tokens: what you pay for counts real questions and the marker tokens, which Cookvale's planning numbers count inside the bands above.
Anthropic's API reference warns that its counts will not match the text you can see, because of this rendering step:
"Under the hood, the API transforms requests into a format suitable for the model. The model's output then goes through a parsing stage before becoming an API response. As a result, the token counts in
usagewill not match one-to-one with the exact visible content of an API request or response."
Recap. The provider receives JSON, but the model reads one long string made by a chat template. That string holds every tool, the system prompt, the recipe and the whole conversation, and it is sent again in full on every turn.
What the Model Does With That String
First the tokenizer, a program that cuts text into tokens, turns the string into token IDs. For Cookvale's turn 1, that is a list of 12,060 integers, the only form the model ever sees.
Then the model runs. A model is a large file of numbers. Each number is a parameter, and together they are the model's weights. Running the model means a fixed series of multiplications and additions between the tokens' numbers and those weights. One run through the whole model is a pass. Inside the forward pass opens the model up. For now, treat the pass as a box.
What comes out of one pass is not an answer. It is a list of scores, one for every token the model could write next. The full list of tokens a tokenizer knows is its vocabulary; Qwen3-0.6B's has 151,936 entries, so each pass ends with 151,936 scores. The scores are turned into probabilities that add up to one. Then a small piece of ordinary code, the sampler, picks one token from those probabilities, usually a likely one but not always the most likely. Where the next token is chosen shows how it picks.
Prediction: the model has the whole prompt. How many tokens does one pass produce?
One. A pass over 12,060 tokens ends with one chosen token, perhaps "Yes".
The Loop
What happens to that one token? It is added to the end of the string, and the model runs again on the string with one more token. That pass ends with scores for the next token, the sampler picks one, it is added, and the model runs again.
pass 1 reads [12,060 prompt tokens] -> writes "Yes"
pass 2 reads [12,060 prompt tokens] "Yes" -> writes ","
pass 3 reads [12,060 prompt tokens] "Yes" "," -> writes " you"
pass 4 reads [12,060 prompt tokens] "Yes" "," " you" -> writes " can"
...
last the model writes <|im_end|>, or the call reaches max_tokensThe words are only an illustration. The shape is the point: every pass ends with exactly one new token, and that token becomes part of what the next pass reads.
Prediction: the answer is 300 tokens. How many passes?
Three hundred, one per output token.
The Model Does Not Write the Whole Answer at Once
You might expect the model to produce the full answer and then send it in pieces. That is wrong, and the loop shows why. Token 2 cannot be computed before token 1 exists, because token 1 is part of what pass 2 reads. Token 300 depends on all 299 before it. The paper that introduced speculative decoding (Leviathan, Kalman and Matias, ICML 2023) puts the rule in one line:
"decoding K tokens takes K serial runs of the model."
"Serial" means one after another. Speculative decoding shows ways to get more than one token from a pass, but they guess and check, and the loop is still underneath. When words appear one at a time in a chat app, that is not an animation. You are watching the loop run.
Recap. One pass reads the whole string and ends with scores for the next token; the sampler picks one, it is added, and the model runs again. One pass, one new token.
The Puzzle, Answered
Where did twelve thousand tokens come from? From the whole prompt, not the question: tools 2,000, system prompt 6,000, recipe 4,000 and question 60, rendered into one string of 12,060 tokens. Every later turn reads all of it again.
The other two questions, the pause before the rush and the price of output, are the next lesson's.
The Takeaway
- The provider receives JSON; the model reads one flat string. A chat template wraps each message in marker tokens and writes the tools in as text.
- Nothing is kept between calls. Every turn sends the whole conversation again: 12,060 input tokens on turn 1, 360 more on each later turn.
- One pass ends with a score for every token in the vocabulary; the sampler picks one, it is added, and the model runs again. A 300-token answer is 300 passes.
The one sentence to keep: your call reaches the model as one long string, sent again in full on every turn, and each pass over that string adds one token.
An open question to carry forward: Priya's answer box stayed empty for almost a second, and then the words arrived quickly. Why a pause, and then a rush? That is the next step, the pause, the rush and the bill.
References
Model files and templates (Hugging Face, checked 2026-10-18):
- Qwen/Qwen3-0.6B: the tokenizer and chat template used in the code above;
vocab_size151,936 andeos_token_id151645 inconfig.json - Qwen/Qwen3-8B
tokenizer_config.json: the Qwen3 chat template whose text the rendered string above follows, including the# Toolsblock and the empty thinking block forenable_thinking=False - Llama 3.1 8B Instruct, ungated mirror: the chat template with the "Cutting Knowledge Date" and "Today Date: 26 Jul 2024" header, and
config.json(8,030,261,248 parameters, 16.06 GB in bf16). The official repository, meta-llama/Llama-3.1-8B-Instruct, is gated. - The Llama 3.1 20 of 51 token measurement: rendered with
jinja2and cross-checked withtransformersapply_chat_templateon an Apple M1 Max, 2026-10-18 (transformers 5.20.1, tokenizers 0.23.3)
Provider documentation (checked 2026-10-18):
- Messages API reference,
usagefield (Anthropic): "the token counts inusagewill not match one-to-one with the exact visible content of an API request or response" - Context windows (Anthropic): "Input phase: Contains all previous conversation history plus the current user message"
Papers and engineering writing:
- Leviathan, Kalman, Matias, "Fast Inference from Transformers via Speculative Decoding", ICML 2023, arXiv:2211.17192: "decoding K tokens takes K serial runs of the model."
Lab runs: the chat template script with Python 3.12, transformers 5.20.1 and tokenizers 0.23.3, using Qwen/Qwen3-0.6B on an Apple M1 Max.

