Working Memory / Scratchpad

Per-task notes the agent keeps while it reasons.

A temporary working state holds intermediate steps, variables, and open tasks during a single execution run.

In practiceA code-review agent writes intermediate findings — duplicate logic found, security issue noted — to a scratchpad state field, then collates all findings into a final report at the last step.

Level
04 · Production
Subdomain
Memory Architecture
Also known as
Scratchpad, Short-Term State, Task State
Trade-off
Better control during a single run is gained against the need for additional state management logic.
Native in
LangGraph, OpenAI Agents SDK, Microsoft Agent Framework, AutoGen / AG2, Google ADK
The mechanism

How it works.

Working memory is a data structure that outlives the single model call. Each step reads a part of it into the prompt and writes its result back as named fields.

1 · Every step of the run

  1. Working memoryGoal, inputs, results so far
  2. Build the promptA subset of the state, not all of it
  3. Model or tool callOne step of the task
  4. Write backThe output is parsed into fields

2 · At the end of the run

  1. Final stepReads the collected results
  2. AnswerBuilt from the fields, not from the transcript
  3. State discardedUnless a Recorder persists it
Inputs
What the agent just perceived: the task, a tool result, a user message.
Active knowledge
What reasoning or retrieval produced during this run.
Carried over
What must survive from step to step, above all the goal.
Walk it through

A real run, step by step.

TaskA code-review agent checks a pull request with three changed files. Each step reads only the fields it needs and writes its result back.
StepReadsWrites
1 · Parse the diffgoalfiles: auth.py, utils.py, api.py
2 · Check auth.pygoal, filesfinding: the token is compared with ==
3 · Check utils.pygoal, filesfinding: duplicates helpers.slugify
4 · Check api.pygoal, filesno finding
5 · Write the reportfindingsreport
What this shows: step 5 never sees the diff again. It writes the report from two findings. Without the fields, every step would carry the whole transcript, and the last step would have to find the two findings in the output of three file checks.
In code

The pattern in code.

Plain Pythonpython
state = {"goal": "review PR 812", "files": [], "findings": []}

for step in plan:
    # a subset, not the whole state
    view = {key: state[key] for key in step.reads}
    result = parse(model(render(step, view)))
    state.update(result)  # written back as named fields

report = model(render("write the report", {"findings": state["findings"]}))
Fit

Use it, or not.

When to reach for it

  • Multi-step tasks require tracking intermediate states.
  • Tool results will be referenced later.
  • Reasoning must be traceable separately from execution.

When it backfires

  • The task is atomic.
  • Intermediate thoughts contain sensitive data that cannot be stored.
  • Persistence across runs is required.
Pitfalls

Two ways this pattern will hurt you.

Scratchpad leaks into the final answer

The model treats its own thinking notes as part of the response and emits the chain-of-thought to the user.

Countermeasure

Hold scratchpad in a typed state field separate from the message channel. Render only the explicit final-answer field downstream.

Parallel branches clobber shared working state

Two fan-out branches write the same scratchpad key in one super-step. Last-writer-wins silently discards one branch's result and the run continues on half the data.

Countermeasure

Give concurrent branches an append/reduce channel or per-branch namespaces, never a plain overwritten field; merge deterministically when they join.

Threat exposure

What this pattern exposes

Adopting this pattern opens these attack surfaces. Each links to its entry in the threat model.

Full threat model →
Framework support

Where Working Memory / Scratchpad is native.

LangGraphLangGraph StateNative
OpenAI Agents SDKrun context objectNative
Microsoft Agent Frameworkthread stateNative
AutoGen / AG2shared agent stateNative
Google ADKsession stateNative
Sources

Where this pattern comes from.

  1. Cognitive Architectures for Language Agents (CoALA)In the source list

Search

Search patterns, frameworks, and pages.