Sub topic 3
The parts of a context
Every model call receives one block of input, and that block is assembled from distinct parts. This chapter names those parts and describes what each one costs and how it changes during a session. It also explains who controls each part and where each one tends to cause trouble. It assumes you have called a model through a chat or API interface, know what a system prompt is, and can read short Python. Token counting and budgeting are covered in the previous chapter. Choosing what to include is covered in the next one.
Why the parts matter
The model does not see "a prompt". It sees a sequence of tokens. Your application builds that sequence from several sources, and each source has different properties:
- how large it is
- how often it changes
- who wrote it
- how much you can trust it
If you treat the context as one undifferentiated string, you cannot say which part is responsible when a call is slow, expensive, or wrong. If you treat it as a set of parts, you can measure each one and change it on purpose.
A typical agent or chat application has five parts.
| Part | Written by | Changes | Typical size |
|---|---|---|---|
| System prompt | Developer | Rarely | Hundreds to a few thousand tokens |
| Tool definitions | Developer | Rarely | Hundreds of tokens per tool |
| Conversation history | User and model | Every turn | Grows without limit |
| Retrieved data | Your retrieval code and tools | Every call | Often the largest single block |
| Current user input | User | Every call | Usually small |
The arrow from output back to history matters. The model's own replies become future input, so mistakes and verbose answers stay in the context and cost tokens on every later call.
System prompt
The system prompt holds the standing instructions: the role, the rules, the output format, and the boundaries. It is written by the developer and does not depend on the user's current request.
Because it is present on every call, each token in it is paid for on every call. A 2,000-token system prompt in a session of 50 calls costs 100,000 input tokens in total. For that reason it should hold only what is true for the whole session.
Common errors:
- Putting task-specific details in the system prompt, such as one customer's account notes. They belong in retrieved data for that call.
- Letting the prompt grow by accretion. Each bug fix adds a sentence, and nobody removes old ones. Over time, rules start to contradict each other.
- Writing rules the model cannot act on, such as "be accurate". A rule should change behavior in a way you can check.
Tool definitions
If your application uses tool calling, each tool is described to the model with a name, a description, and a parameter schema. These descriptions are part of the input on every call, whether or not the model uses the tool.
Tool definitions are easy to underestimate. A tool with ten parameters and a careful description can take several hundred tokens. An agent with 40 tools can spend thousands of tokens before the user has said anything. Large tool sets also make it harder for the model to choose the right tool, because several descriptions overlap.
Tool definitions are stable in the same way the system prompt is. Tool results are different. They arrive during a call, are appended to the context, and belong to the retrieved-data category in practice. A later chapter covers both in detail.
Conversation history
History is the list of earlier user messages, model replies, and (in agent loops) tool calls and tool results. It is the only way the model knows what happened earlier, because the model keeps no state between calls. The application must send the history again each time.
This makes history the part that grows. After turn n, the context contains all n earlier exchanges. Cost per call grows with each turn, and total cost for a session grows faster than linearly.
History is also the part where old errors persist. If the model made a wrong claim in turn 3, that claim is still in the input at turn 30 unless you remove it. Later chapters cover trimming and compaction. For now, remember that history is not neutral storage. It is content the model will read and may rely on.
Retrieved data
Retrieved data is any information your application fetches to help with this specific request. Examples:
- document passages from a search index
- rows from a database
- the contents of a file the agent opened
- the output of a tool such as a shell command or web fetch
This part is usually the most variable and often the largest. One file read can add 10,000 tokens. It is also the part with the weakest trust guarantees. The text comes from outside your control, so it can be outdated, irrelevant, or wrong. It may even contain text that looks like instructions.
Treat retrieved data as evidence to be read, not as instructions to be followed. It helps to mark it clearly, for example by wrapping each item in labeled tags that name its source. Labels let the model refer to its sources and let you debug which item caused a bad answer.
Current user input
The current user input is the message the model must respond to now. It is usually the smallest part, but it defines the task for this call. Everything else in the context exists to help the model answer it.
Two practical points apply here. First, user input can be long if the user pastes a document or log file. Do not assume it is small. Second, the user's message may refer back to earlier turns ("do the same for the second one"). The model can only resolve that reference if the relevant history is still in the context.
Measuring each part
You cannot manage what you have not measured. The following script prints the share of the context that each part uses. It uses the rough heuristic of four characters per token. Replace it with your provider's tokenizer when you need exact counts.
def estimate_tokens(text):
# Rough heuristic: about 4 characters per token for English text.
return max(1, len(text) // 4)
def breakdown(parts):
# parts maps a part name to its text.
counts = {name: estimate_tokens(text) for name, text in parts.items()}
total = sum(counts.values())
for name, tokens in counts.items():
share = 100 * tokens / total
print(f"{name:<10}{tokens:>7}{share:>7.1f}%")
print(f"{'total':<10}{total:>7}{100.0:>7.1f}%")
return counts
parts = {
"system": "a" * 2400,
"tools": "b" * 6000,
"history": "c" * 12000,
"retrieved": "d" * 8000,
"user": "e" * 400,
}
breakdown(parts)Running it prints:
system 600 8.3%
tools 1500 20.8%
history 3000 41.7%
retrieved 2000 27.8%
user 100 1.4%
total 7200 100.0%In this example the user's message is 1.4% of the input. Tool definitions and the system prompt together take about 29%, and they are the same on every call. History is the largest part and will keep growing. This kind of table tells you where to look first.
Stable parts and changing parts
The diagram above groups the parts by how often they change. That grouping has two consequences.
- Cost. Stable parts are paid for on every call but never carry new information. They are the best candidates for shortening and for caching.
- Order. Providers that support prompt caching reuse work for an unchanged prefix of the input. If you put stable parts first and changing parts last, the prefix stays identical between calls. A later chapter covers this. For now, note that assembly order is a design decision and not an accident of how you concatenate strings.
Common errors
- Counting only the visible text. Tool schemas, role markers, and formatting add tokens that do not appear in your chat transcript. Measure what you actually send.
- Forgetting that output is reused. Long model replies are appended to history and become input on the next call.
- Mixing instructions and data. When retrieved text sits in the same block as rules, the model cannot tell which is which, and neither can you when debugging.
- Treating every part as equally important. The current request and its direct evidence matter most for this call. Older history and unused tools matter least.
- Never looking at the assembled context. Log or print the final input for a sample of real calls. Many context problems become obvious once you read what the model actually received.
Putting it into practice
When you build or review an application, go through the parts in order:
- Print the full assembled input for one real call.
- Label each section by part.
- Estimate the tokens in each.
- For each part, ask whether it is needed for this call, whether it could be shorter, and whether it is placed with parts that change at a similar rate.
The next chapters use this breakdown to decide what to select, what to remove, and what to move outside the window.
Exercise
An agent has a 1,800-token system prompt and 25 tool definitions totaling 6,000 tokens. A session runs 40 model calls. Ignoring all other parts, how many input tokens go to the system prompt and tool definitions across the session, and why is this a reason to treat these two parts as candidates for shortening and caching?
Show answer
Each call sends 1,800 + 6,000 = 7,800 tokens of stable content. Over 40 calls that is 7,800 × 40 = 312,000 input tokens. These parts are identical on every call and carry no new information, so shortening them or caching them as a stable prefix reduces cost on every call without changing what the model learns about the current request.
Exercise
A support assistant answers incorrectly in turn 20 of a conversation. The assembled input contains the system prompt, tool definitions, 19 earlier turns, three retrieved help-center articles, and the user's new question. Name two parts of the context that could be the source of the error and say how you would check each.
Show answer
Possible sources include the conversation history and the retrieved data. For history, read the earlier turns for a wrong claim or outdated statement the model may be relying on, and test by removing or correcting that turn. For retrieved data, check whether any of the three articles is outdated, irrelevant, or contradicts another, and test by removing each article in turn and rerunning the call. (The system prompt, if it contains conflicting rules, is also an acceptable answer.)