Context windows
Write a scoped skill package and manage its instructions and references within a task's context budget.
THE IDEA
Context windows
A model request has a finite context contract. Everything competes for it: instructions, conversation history, tool observations and the room reserved for the answer. In a synthetic 8,192-token budget with 1,200 for instructions, 2,800 for history, 1,600 for tool results and 1,500 for output, 1,092 tokens remain. A 900-token skill body fits; a 2,000-token reference does not, and the remedy is to select the relevant passage, not to drop task constraints silently.
Bookkeeping model: available = window − instructions − history − tool results − reserved output. Real provider limits may account for these separately, so use the documented accounting of the model you choose.
Predict first. Can unlimited tool text be added without affecting available context?
Chapter context
A skill packages reusable task instructions and optional supporting material. Context engineering decides which instructions and evidence are available for a particular operation. Loading more text is not automatically better than selecting the right text.
TRY IT YOURSELF
Part 1. Do it hands-on
Do the budget arithmetic by hand, then write the allocator and a passage selector.
Bookkeeping model: available = window − instructions − history − tool results − reserved output. Real provider limits may account for these separately, so use the documented accounting of the model you choose.
- To do: A synthetic 8192-token budget reserves 1200 for instructions, 2800 for history, 1600 for tool results and 1500 for output. Tokens remaining?
- To do: A 900-token skill body is added. Tokens left?
- To do: A 2000-token reference is added to the original 1092 remaining. By how many tokens does it overflow?
- To do: Can unlimited tool text be added without affecting the available context? Type yes or no
Part 2. Do it in code
- allocate(window, instructions, history, tools, output): {"used": sum of the four, "remaining": window − used}.
- fits(remaining, n): True if n tokens fit.
- overflow(remaining, n): how many tokens do NOT fit (0 if it fits).
- select_passages(budget, passages): passages is a list of {"name", "tokens", "score"}. Greedily take passages in descending score (ties: list order), skipping any that do not fit in the remaining budget. Return the chosen names in the order taken.
- trim_history(messages, budget): messages is a list of {"tokens", "pinned"}. Keep every pinned message; then add the newest unpinned messages that still fit. Return the kept indices in ascending order; ValueError if the pinned messages alone exceed the budget.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Can unlimited tool text be added without affecting available context?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
For a synthetic combined budget W, a bookkeeping model is available = W − instructions − history − tool results − reserved output. Actual provider contracts can separate these limits, so use the chosen model's documented accounting when implementing.
Worked example
A synthetic 8192-token budget reserves 1200 for instructions, 2800 for history, 1600 for tool results and 1500 for output. Remaining space is 1092 tokens. A 900-token skill body fits under this model; adding a 2000-token reference does not. The remedy is to select or retrieve the relevant passage rather than silently dropping task constraints.
Reference code
The following example uses the Python standard library and does not call a remote service.
window=8192
used=1200+2800+1600+1500
remaining=window-used
assert remaining == 1092
assert 900 <= remaining
assert 2000 > remaining
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
Compaction can lose evidence and constraints. Skill installation is not task authorization. A context limit and a product's memory feature are different mechanisms.