Working memory · AI agents

On this page 7

Working memory in AI agents is the short-term, task-scoped information that an agent holds in active use while it pursues the current goal: the user request, the plan, recent tool results, intermediate conclusions and open questions. In a language-model agent, working memory is primarily the content of the context window at each step of the agent loop, together with any structured state the harness injects into it.

The term is borrowed from cognitive psychology and from classic cognitive architectures such as Soar and ACT-R, where working memory is the limited buffer on which reasoning operates. In modern AI agents it is distinguished from long-term memory, which stores episodic, semantic and procedural knowledge outside the model and must be retrieved before it can influence a decision.

What working memory holds in an agent

Working memory contains everything the model can attend to right now. Anything outside it, whether stored in a database, a file or a previous session, has no effect on the next action until the harness loads it back into context. This makes working memory the decisive bottleneck for agent reasoning.

The table below lists the typical contents of working memory in a tool-using agent and why each element matters.

ElementExampleWhy it is kept
Goal and constraintsUser request, deadlines, permissionsAnchors every decision to the task
System instructionsRole, policies, tool rulesDefines allowed behavior
Current planOrdered list of remaining stepsPrevents drift and repetition
Recent observationsTool outputs, search results, errorsGrounds the next action in facts
Scratchpad notesHypotheses, partial resultsCarries reasoning across steps
Retrieved memoriesRelevant past episodes or factsBrings long-term knowledge into play
Typical contents of agent working memory

Some frameworks also keep a structured state object, such as a task list or variable store, outside the prompt and render a compact view of it into context at each turn. This separates the authoritative state from its textual representation and makes checkpointing easier.

How working memory is managed during the agent loop

Working memory is rebuilt or extended on every iteration of the agent loop. After each tool call, the harness appends the observation, and the model reads the whole context to choose the next step. Because the context window is finite, the harness must decide what to keep, compress or evict as a task grows.

  1. Assemble: combine instructions, goal, plan and relevant memories into the prompt.
  2. Act: the model reasons, then calls a tool or answers.
  3. Observe: the tool result is added to working memory.
  4. Curate: older or verbose content is summarized, truncated or written to external storage.
  5. Repeat until the goal is met or a stopping condition applies.

The curation step uses several established techniques, each with a different cost and risk.

TechniqueHow it worksMain risk
Sliding windowDrops the oldest turnsLoses early constraints
Context compactionSummarizes history into a shorter noteSummary omits critical details
Tool result truncationKeeps only the head or relevant linesHides the key error message
Offloading to filesWrites notes to disk, reloads on demandAgent forgets to reload them
Paging (MemGPT style)Model moves data between tiers itselfExtra calls and latency
Subagent delegationIsolates a subtask in a fresh contextResults return incomplete
Techniques for keeping working memory within limits

Working memory compared with other memory types

Agent memory is usually described as several cooperating stores. Working memory is the only one the model reasons over directly; the others matter only to the extent that their contents are retrieved into working memory at the right moment.

Memory typeContentLifetime
Working memoryCurrent task context and stateOne task or session
Episodic memoryRecords of past interactions and outcomesPersistent across sessions
Semantic memoryFacts about users, domains and the worldPersistent, periodically updated
Procedural memorySkills, prompts, tool usage patternsPersistent, changes slowly
Parametric knowledgeWhat the model learned in trainingFixed until retraining
Working memory and long-term memory types in agents

Working memory is also related to, but broader than, the agent scratchpad. The scratchpad holds the agent’s own reasoning notes, while working memory includes instructions, observations and retrieved content as well. Likewise, agent state is the full serialized status of a run, of which working memory is the portion currently exposed to the model.

Design guidelines for effective working memory

Good working memory design aims for high signal density: the context should contain what the next decision needs and little else. Longer contexts raise cost and latency, and models tend to attend less reliably to information buried in the middle of very long inputs.

SituationRecommended approachReason
Short tasks under 20 stepsKeep full history in contextSimplest and most faithful
Long-running tasksCompaction plus external notes filePreserves goals beyond the window
Large tool outputsStore raw output, show a summarySaves tokens without losing data
Parallel subtasksDelegate to subagentsEach gets a clean, focused context
Critical constraintsRestate them in every turnProtects against eviction
Design decisions for agent working memory

A further guideline is to keep an explicit, updated plan in working memory. Agents that rewrite a short task list after each step recover from errors more reliably than agents that rely on the implicit history of the conversation.

Failure modes and limitations

Most agent failures that look like reasoning errors are in fact working memory failures. The model acts on what it can see, so missing, stale or contradictory context leads directly to wrong actions.

  • Goal drift: the original request is compacted away and the agent pursues a side task.
  • Context pollution: long, irrelevant tool outputs crowd out important facts.
  • Stale observations: the agent relies on a file state that has since changed.
  • Repetition loops: without a record of attempts, the agent retries the same failing action.
  • Prompt injection exposure: untrusted content placed in working memory can masquerade as instructions.

Larger context windows reduce but do not remove these problems. Even with hundreds of thousands of tokens available, deliberate curation usually produces better decisions than simply keeping everything.

A plain-language summary

Working memory is what an AI agent has on its desk while it does a job. It includes the task, the plan, and the latest results. The agent can only use what is on that desk. Older material sits in filing cabinets until someone fetches it.

The desk has limited space. Good agents keep the desk tidy. They summarize old notes, file away bulky papers and keep the main goal in plain view.

An example from practice

A software team uses a coding agent to migrate 120 test files to a new testing framework. Early runs fail halfway: after about 60 tool calls, the history is compacted, the instruction to keep old assertions intact disappears, and the agent starts rewriting test logic.

The team changes the working memory design. The agent now keeps a short progress file listing completed files and the key constraints, and the harness reinserts that file into context after every compaction. Large test logs are stored on disk, and only the failing lines are shown to the model.

With these changes, the agent completes the migration in a single session, and all 120 files pass review without any lost assertions. The model itself did not change; only the management of its working memory did.