Topic 1 of 5 · 4 min read
Short-term memory in the context window
Goal: Identify what in an agent's context is working memory and what should be cleared or summarised before it crowds out the task.
An agent's short-term memory is its context window: everything the model can see on this turn, and nothing it cannot. The model has no other memory. If a fact is not in the window, the model does not know it.
Check yourself · 1 of 3
A coding agent has run 40 tool calls in one session. What is its short-term memory?
Why it matters
Each turn, the agent harness sends the model the system prompt, the conversation so far, every tool call and every tool result. A loop that runs 40 steps carries all 40 results forward. Raw tool output is the biggest cost: a file read, a web page or a 500-line test log can be larger than everything else combined.
A full window causes three problems. It costs more and runs slower. It hits the hard limit, and the harness cuts old content or fails. Worse, long contexts tend to degrade quality: the model misses details buried in the middle and drifts from the original goal. A window that is nearly full is a risk even before it is full.
How to manage it
- Name the parts. Working memory has four kinds of content: the instructions, the conversation, the tool results, and any scratchpad (a plan or checklist the agent rewrites as it goes).
- Keep the goal and the plan near the end. Restate the task and the current step in a short block that is rewritten each turn, so it is always fresh.
- Trim tool results. Return only the lines needed: the failing test, not the whole log. Clear results the agent has already used.
- Summarise old turns. When the history gets long, replace early steps with a short summary of decisions, open questions and facts found. Anthropic calls this compaction.
- Pick a trigger. Compact at a set size, such as 70 percent of the window, not when the error appears.
Before and after
Before: the agent runs grep over a repo and keeps 900 matching lines in context for the rest of the session. After: the harness keeps the 12 relevant matches, and one line saying "searched for parseDate; 14 call sites, listed above".
What goes wrong
- Summaries that drop constraints. "Do not touch the billing module" disappears in compaction, and the agent edits billing 20 steps later.
- Summaries that keep mistakes. A wrong guess written into the summary becomes a fact the agent trusts.
- Stale results. A file read at step 3 is edited at step 9, but the old copy still sits in context and the agent reasons from it.
Examples
Trimming a test log
An agent runs the test suite and gets 3,000 lines back. The harness passes the model this instead:
120 passed, 1 failed
FAIL tests/orders.test.ts > totals > applies coupon once
Expected 90, received 80 (orders.test.ts:44)
The full log stays in a file on disk. The agent can open it with a tool if it needs more, but it is not carried through every later turn.
A compaction summary that works
Goal: add pagination to GET /orders.
Done: query uses take/skip (orders.ts:31).
Constraint: do not change the response shape; the mobile app depends on it.
Open: total count query is slow on large accounts.
It keeps the goal, one constraint and the open question. The 30 earlier turns are gone.
Key points
- The context window is the only working memory an agent has; what is not in it does not exist for the model.
- Tool results are usually the largest part of the window, so trim or clear them first.
- Compact the history at a set threshold, and write the summary so goals and constraints survive.
- Restate the goal and current plan each turn in a short block rather than hoping the opening prompt is still noticed.
- Old file contents in context go stale when the file changes; re-read instead of trusting them.
Common mistakes
- Leaving every tool result in context for the whole session.
- Compacting without keeping the user's constraints, so the summary silently drops them.
- Assuming a larger window means the agent will use all of it well.
- Letting an old copy of a changed file stay in context and reasoning from it.
Check yourself · 2 of 3
Which part of an agent's context usually grows fastest in a long session?
Check yourself · 3 of 3
An agent compacts its history at step 30 and then edits a module the user said was off limits. What most likely happened?
Practice items, not counted towards your mastery.
Start here
- MemGPT: Towards LLMs as Operating Systems (opens in a new tab)PaperFree
The paper that frames context as fast memory and external stores as slow memory the model pages in and out.
- Memory tool (Claude API docs) (opens in a new tab)DocsFree
How a directory-backed memory tool works: view, create and edit commands, and security notes on path handling.
- Memory overview (LangChain docs) (opens in a new tab)DocsFree
Short-term versus long-term memory, and semantic, episodic and procedural memory types, with write timing.
More for this topic
- Effective context engineering for AI agents (Anthropic) (opens in a new tab)ArticleFree
Why context is a limited resource, with compaction, structured note-taking and just-in-time retrieval.