Sub topic 3
The parts of a context
This chapter assumes you have called a model through a chat or API interface, know what a system prompt is, and can read short Python. The previous chapter covered tokens and budgets. Here you split a single call into its parts, so you can see what each part costs and which ones you can control.
One call, five parts
The model sees one sequence of tokens on every call. That sequence is usually assembled from five parts:
- System prompt. The standing rules: role, tone, constraints, output format.
- Tool definitions. The names, descriptions, and parameter schemas of the functions the model may call.
- Conversation history. Earlier user messages, model replies, and tool results from this session.
- Retrieved data. Documents, search hits, or file contents your code fetched for this step.
- User input. The current question or instruction.
How the parts differ
The parts differ in who writes them, how often they change, and how they grow. These differences decide which technique you use to manage each one.
| Part | Written by | Changes | Growth pattern |
|---|---|---|---|
| System prompt | You | Rarely | Fixed, but tends to creep upward as rules are added |
| Tool definitions | You | Rarely | Grows with every tool you register |
| Conversation history | User and model | Every turn | Grows without bound |
| Retrieved data | Your retrieval code | Every call | Varies with how much you fetch |
| User input | The user | Every turn | Usually small, occasionally huge (a pasted log) |
The model's own output also uses the window. A budget that fills the window with input leaves no room to answer, which is why the earlier budgeting example reserves output tokens first.
Measuring each part
You cannot manage what you have not measured. This script reports the approximate cost of each part. It uses the rough four-characters-per-token heuristic, so use your provider's tokenizer when you need exact numbers.
def estimate_tokens(text):
# Rough heuristic: about 4 characters per token for English text.
return max(1, len(text) // 4)
def report(parts, window):
total = 0
for name, text in parts.items():
cost = estimate_tokens(text)
total += cost
print(f"{name:<14}{cost:>7} tokens")
print(f"{'total':<14}{total:>7} tokens ({total / window:.0%} of window)")
parts = {
"system": "You are a support assistant. Answer briefly. " * 20,
"tools": '{"name": "search_orders", "parameters": {"id": "string"}}' * 15,
"history": "User: where is my order?\nAssistant: Checking now.\n" * 60,
"retrieved": "Order 1042 shipped on Monday via ground freight. " * 80,
"user_input": "Why has order 1042 not arrived?",
}
report(parts, window=8000)Run it and look at the proportions. In real applications the surprise is rarely the user input. It is usually tool definitions that you stopped noticing, or history that has quietly become the largest part.
Why the split matters
Each part has a different failure and a different fix.
- A bloated system prompt gets pruned or rewritten.
- Too many tool definitions get filtered so only relevant tools appear.
- Long history gets trimmed or compacted.
- Noisy retrieved data gets tightened by better selection.
- Oversized user input gets truncated or summarized.
Keep a habit of labeling the parts in your code, as the parts dictionary above does. When every section has a name, you can log its size, test it alone, and change one part without disturbing the others.
Exercise
A chat assistant works well for ten turns, then starts running out of window space. The system prompt is 600 tokens, the tool definitions are 900 tokens, and the user messages are about 40 tokens each. Which part is most likely responsible, and what does that tell you about where to look first?
Show answer
Conversation history. The system prompt and tool definitions are fixed and the user input is small, so the growth comes from accumulated turns, including model replies and tool results. Measure the history's token count per turn, then consider trimming or compaction.