Back to course overview
Module 4Memory & state 12 min

Short-term memory

The context window as working memory: how it fills on long trajectories, and the compaction patterns that keep an agent coherent past its budget.

An agent's short-term memory is its message list — every reasoning beat, tool call, and result, accumulating turn by turn. This is a feature (the agent remembers what it just learned) and a time bomb: tool results are verbose, and long tasks overflow the context window. A 20-turn trajectory pasting order JSON, doc chunks, and DB rows can hit the ceiling mid-task — and then the earliest turns (the original goal, the plan) fall off the front, silently.

The failure you'll actually see

It's not a crash. It's an agent that, fifteen turns in, forgets the customer's second request, re-fetches an order it already looked up, or drifts from the goal — because the tokens carrying that information aged out of the window. Position effects (Prompt Engineering M1) make it worse: even before overflow, facts stranded in the middle of a long trajectory get attended to poorly. Long-running agents don't fail loudly; they get gradually dumber.

Compaction: memory management for agents

  • Result trimming (cheapest). Once a tool result has been used, it doesn't need to persist verbatim. Keep the finding ('order HL-1042: delivered Mar 3, $240, one item'), drop the 4KB raw JSON. Your adapters already shape results; this trims them again after use.
  • Trajectory summarization. When the window approaches a threshold, replace the oldest turns with a model-written summary: 'Established: order delivered, damage confirmed via photo, policy allows replacement. Refund of $80 computed and verified. Remaining: update ticket, draft reply.' The agent continues against the summary — coherence preserved, tokens reclaimed.
  • The persistent scratchpad. Give the agent a notes object it writes key findings to — order facts, the decision, the amount. Because it lives outside the message flow, it survives summarization intact. This is the pattern behind 'memory' in production coding agents: the working state is a file, not the transcript.
  • Pin the goal. Whatever else compacts, keep the original task and the current plan at the top of every turn. An agent that forgets its objective is worse than one that forgets a detail.
the compaction triggertext
each turn:
  if estimated_tokens(messages) > 0.7 * context_limit:
      summary = summarize(old_turns)          # a model call
      messages = [system, goal, plan, summary] + recent_turns
      # raw tool results from >N turns ago are gone; findings live in
      # the summary and the scratchpad. The agent barely notices.
The 70% rule

Don't wait for the hard limit — compact at ~70% of the window. It leaves headroom for the summarization call itself and for a few more turns, and it keeps the agent out of the degraded position-effect zone. Cheaper than the alternative: an agent that forgets the goal on turn 19 of a refund it's about to issue.