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.
| Element | Example | Why it is kept |
|---|---|---|
| Goal and constraints | User request, deadlines, permissions | Anchors every decision to the task |
| System instructions | Role, policies, tool rules | Defines allowed behavior |
| Current plan | Ordered list of remaining steps | Prevents drift and repetition |
| Recent observations | Tool outputs, search results, errors | Grounds the next action in facts |
| Scratchpad notes | Hypotheses, partial results | Carries reasoning across steps |
| Retrieved memories | Relevant past episodes or facts | Brings long-term knowledge into play |
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.
- Assemble: combine instructions, goal, plan and relevant memories into the prompt.
- Act: the model reasons, then calls a tool or answers.
- Observe: the tool result is added to working memory.
- Curate: older or verbose content is summarized, truncated or written to external storage.
- 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.
| Technique | How it works | Main risk |
|---|---|---|
| Sliding window | Drops the oldest turns | Loses early constraints |
| Context compaction | Summarizes history into a shorter note | Summary omits critical details |
| Tool result truncation | Keeps only the head or relevant lines | Hides the key error message |
| Offloading to files | Writes notes to disk, reloads on demand | Agent forgets to reload them |
| Paging (MemGPT style) | Model moves data between tiers itself | Extra calls and latency |
| Subagent delegation | Isolates a subtask in a fresh context | Results return incomplete |
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 type | Content | Lifetime |
|---|---|---|
| Working memory | Current task context and state | One task or session |
| Episodic memory | Records of past interactions and outcomes | Persistent across sessions |
| Semantic memory | Facts about users, domains and the world | Persistent, periodically updated |
| Procedural memory | Skills, prompts, tool usage patterns | Persistent, changes slowly |
| Parametric knowledge | What the model learned in training | Fixed until retraining |
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.
| Situation | Recommended approach | Reason |
|---|---|---|
| Short tasks under 20 steps | Keep full history in context | Simplest and most faithful |
| Long-running tasks | Compaction plus external notes file | Preserves goals beyond the window |
| Large tool outputs | Store raw output, show a summary | Saves tokens without losing data |
| Parallel subtasks | Delegate to subagents | Each gets a clean, focused context |
| Critical constraints | Restate them in every turn | Protects against eviction |
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.