Lesson 55.1 · 1 of 4 · 4 min read · Reviewed 24 Nov 2026
The context window as working memory
By the end, you can
- Treat the context window as working memory.
- Recognise when working memory is overloaded.
Working memory
context window (fixed size)
+---------------------------------------------------+
| system prompt | tools | loaded memory | turn 1 ... |
| ... turn 12 | tool result (8k tokens) | turn 13 |
+---------------------------------------------------+
^ newest
Everything the model uses to answer must fit here. Nothing outside it exists for this call.
Limits
- Hard limit: the model’s context windowContext window: The maximum number of tokens a model can consider at once, counting the input and the output it generates. size. Requests that don’t fit fail or must be trimmed.
- Soft limit: long before the hard limit, a context full of irrelevant material can make the model miss important details, especially in the middle of long inputs.
What fills it
| Item | Typical growth |
|---|---|
| Conversation turns | Steady |
| Tool results | Big jumps (a file read, a search) |
| Loaded memories and documents | Up front |
| Thinking and drafts | Per step |
Keeping it healthy
Part 11 covers this in depth. The short version: load only what’s needed, trim or clear old tool results, summarise long histories, and move durable facts to long-term stores (55.3).
Key points
- The context window is everything the model can see right now; it's the agent's working memory.
- It has a hard size limit, and quality can drop well before the limit when it's full of clutter.
- Long agent runs fill it with tool results and history; plan for trimming and summarising.
Chart working memory over an agent run
Simulate how an agent's context grows step by step and find when it crosses the soft limit and the hard limit, with and without clearing old tool results.
You will have: A script that prints a text chart of context size per step and the steps where each limit is crossed.
Steps
- Use the starter's numbers: a 100k window, 10k fixed, 1.5k per step, an 8k tool result every 3 steps.
- Write simulate(clear) that returns the context size for steps 1 to 40, stopping when the window is full.
- With clear=True, keep only the 2 newest big results and replace older ones with 0.2k placeholders.
- Print the first step over the soft limit (60% of the window) and over the hard limit for both runs.
- Print a small bar chart with one # per 5k tokens for every fifth step.
Done when
- Without clearing, the run hits the hard limit before step 40.
- With clearing, the run never hits the hard limit in 40 steps.
- Both soft-limit steps are printed.
Open in the Playground No API key needed.
Starter code (Python)
WINDOW, FIXED, PER_STEP, BIG, EVERY = 100, 10, 1.5, 8, 3
def simulate(clear):
sizes = []
for step in range(1, 41):
results = step // EVERY
# TODO: if clear, keep only the 2 newest big results; older ones cost 0.2 each
used = FIXED + step * PER_STEP + results * BIG
sizes.append(used)
return sizes
def first_over(sizes, limit):
# TODO: return the first step (1-based) whose size is over the limit, or None
return None
for clear in (False, True):
s = simulate(clear)
print("clear =", clear, "soft at", first_over(s, WINDOW * 0.6), "hard at", first_over(s, WINDOW))
Show a solution
WINDOW, FIXED, PER_STEP, BIG, EVERY = 100, 10, 1.5, 8, 3
def simulate(clear):
sizes = []
for step in range(1, 41):
results = step // EVERY
if clear:
kept = min(results, 2)
used = FIXED + step * PER_STEP + kept * BIG + (results - kept) * 0.2
else:
used = FIXED + step * PER_STEP + results * BIG
sizes.append(round(used, 1))
return sizes
def first_over(sizes, limit):
return next((i + 1 for i, s in enumerate(sizes) if s > limit), None)
for clear in (False, True):
s = simulate(clear)
print("clear =", clear, "soft at", first_over(s, WINDOW * 0.6), "hard at", first_over(s, WINDOW))
for step in range(5, 41, 5):
size = s[step - 1]
bar = "#" * int(min(size, WINDOW) // 5)
print(" step %2d %6.1fk %s%s" % (step, size, bar, " FULL" if size > WINDOW else ""))