Sub topic 1
What a Model Call Actually Does
Prerequisites
This chapter assumes you can read basic Python and JSON, and that you know what an HTTP API request and response look like. You should have a general idea of what a large language model (LLM) is. You do not need to have built an agent before. Everything else, including tokens, statelessness, and the role of the harness, is explained here.
Why start here
A coding agent can seem like a program that "remembers" your project, "decides" to open files, and "knows" what it did five minutes ago. None of that happens inside the model. The model is a function: text goes in, text comes out. Everything else is done by a surrounding program called the harness. This chapter pins down exactly what one model call does, because every later idea in this tutorial (tools, the agent loop, the context window) follows from it.
A model call is a function call over HTTP
When an application uses a hosted model, it sends an HTTP request and gets an HTTP response. The request carries the input text (plus settings). The response carries the output text (plus bookkeeping such as token counts).
A simplified request looks like this. Field names differ between providers, but the shape is common.
{
"model": "example-model",
"max_tokens": 500,
"temperature": 0,
"messages": [
{"role": "user", "content": "Write a Python one-liner that reverses a string s."}
]
}A simplified response looks like this:
{
"content": "s[::-1]",
"stop_reason": "end_turn",
"usage": {"input_tokens": 18, "output_tokens": 6}
}The model weights do not change during this exchange. The model reads the input, produces output, and the request is finished. Nothing is stored on the model's side for your next request.
Tokens: the unit the model reads and writes
A model does not read characters or words. It reads tokens, which are chunks of text from a fixed vocabulary. A token might be a whole common word (the), part of a word (Pyth + on), a single symbol ({), or a run of whitespace. Code often uses more tokens per character than prose, because of punctuation, indentation, and unusual identifiers.
Tokens matter for three practical reasons:
- Limits. Each model accepts a maximum number of tokens per call. This is the context window, covered in a later chapter.
- Cost. Hosted models usually charge separately for input tokens and output tokens.
- Latency. Output is produced one token at a time, so longer answers take longer.
For rough planning, English text averages about 4 characters per token. Treat that as an estimate only. Exact counts require the provider's tokenizer, and different models tokenize differently.
How the model produces output
The model generates its answer one token at a time. At each step it computes a probability for every token in its vocabulary, picks one, appends it to the text so far, and repeats. It stops when it emits a special end token or reaches the max_tokens limit you set.
The diagram below shows this loop inside a single call.
Sampling settings
How the next token is picked is controlled by sampling settings:
| Setting | What it does | Typical effect |
|---|---|---|
| `temperature` | Scales how spread out the probabilities are | Low values favor the most likely token. High values give more varied output. |
| `top_p` | Limits choices to the smallest set of tokens whose probabilities add up to p | Lower values cut off unlikely tokens. |
| `max_tokens` | Caps the length of the output | Output may be cut off mid-sentence if the cap is hit. |
Even at temperature 0, outputs are not always identical across calls. Hosted systems can differ slightly in floating-point arithmetic and batching. Design your harness so that it works with varied outputs rather than relying on exact repeats.
Statelessness: the model remembers nothing
This is the central idea of the chapter. A model call is stateless. The model keeps no memory between calls. If you send a second request, the model has no access to the first one unless you include it again.
The following code makes this concrete. It uses a placeholder call_model function in place of a real provider API, and a stub so you can run it.
def call_model(messages):
# Stub standing in for a provider API. It can only "know" what is in `messages`.
text = " ".join(m["content"] for m in messages)
if "Priya" in text:
return {"text": "Your name is Priya."}
return {"text": "I don't know your name."}
# Call 1: tell the model something.
first = call_model([{"role": "user", "content": "My name is Priya."}])
print(first["text"]) # Your name is Priya.
# Call 2: a fresh request that does not include call 1.
second = call_model([{"role": "user", "content": "What is my name?"}])
print(second["text"]) # I don't know your name.
# Call 3: the harness resends the earlier turn along with the new question.
history = [
{"role": "user", "content": "My name is Priya."},
{"role": "assistant", "content": "Nice to meet you, Priya."},
{"role": "user", "content": "What is my name?"},
]
third = call_model(history)
print(third["text"]) # Your name is Priya.A real model behaves the same way. The "memory" in call 3 exists only because the harness sent the earlier turns again. Chat applications feel continuous because they resend the whole conversation on every turn.
What the harness is responsible for
Because the model holds no state, something else must hold it. That something is the harness. In a coding agent, the harness:
- Stores the conversation so far, as a list of messages.
- Builds each request from that list, plus instructions and tool definitions.
- Sends the request and reads the response.
- Decides what to do with the response (show it, run a tool, stop).
- Appends the new information to the list, ready for the next call.
Consequences you will see in practice
Statelessness has direct effects on how agents behave.
- Input grows every turn. Each call resends everything so far, so the input gets longer as work continues. Total tokens processed across a long session grow faster than the number of turns. With 10 turns of about 1,000 new tokens each, call 10 sends about 10,000 input tokens, and the session as a whole sends about 55,000.
- Cost and latency grow with history. Providers may offer prompt caching, which reduces the cost of repeating an unchanged beginning of the input. It helps, but it is a provider feature with its own rules. It does not give the model memory.
- Anything not in the input does not exist. If a file was read 50 steps ago and then dropped from the message list, the model cannot recall it. It will guess, or read the file again.
- Edits to history are possible. Since the harness owns the list, it can shorten, summarize, or reorder it. This is the basis of context management, covered later.
You can compute the growth in the example above directly:
def total_input_tokens(turns, tokens_per_turn):
# Call k sends all k turns' worth of tokens.
return sum(k * tokens_per_turn for k in range(1, turns + 1))
print(total_input_tokens(10, 1000)) # 55000Common errors and misconceptions
- "The model learned from my earlier chat." Calls do not train or update the model. Only the text you send is used.
- "It forgot what I said." Usually the harness dropped it, truncated it, or never sent it. Check the request, not the model.
- "Temperature 0 makes it deterministic." It makes output more consistent, not guaranteed identical.
- "The model ran my code." A plain model call only returns text. Running anything is the harness's job. The next chapters show how a model can request an action and how the harness carries it out.
- Ignoring
stop_reason. If the reason is a length cutoff rather than a natural end, the output is incomplete. Treating it as a finished answer is a bug.
Key points
- A model call takes text in and returns text out. It changes nothing on the model side.
- Text is measured in tokens, which determine limits, cost, and latency.
- Output is generated one token at a time until an end token or the
max_tokenscap. - Calls are stateless. The harness stores the conversation and resends it on every call.
- Everything an agent "knows" at a given step is whatever the harness put into that step's input.
Exercise
A harness sends call 1 with the message "The test file is tests/test_api.py." and gets a reply. It then sends call 2 containing only the message "Run that file." The model asks which file. Why, and what should the harness have done?
Show answer
Calls are stateless, so the model only sees what is in the input of call 2, which does not include call 1. The harness should have stored the earlier messages and the reply, then sent them again together with the new message in call 2.
Exercise
A session sends 2,000 new tokens of content on each turn, and each call resends all earlier content. How many input tokens does call 5 send, and how many have been sent across all 5 calls in total?
Show answer
Call 5 sends 5 x 2,000 = 10,000 input tokens. The total across calls 1 to 5 is 2,000 x (1+2+3+4+5) = 30,000 input tokens.