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.
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.
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
2 · At the end of the run
| Step | Reads | Writes |
|---|---|---|
| 1 · Parse the diff | goal | files: auth.py, utils.py, api.py |
| 2 · Check auth.py | goal, files | finding: the token is compared with == |
| 3 · Check utils.py | goal, files | finding: duplicates helpers.slugify |
| 4 · Check api.py | goal, files | no finding |
| 5 · Write the report | findings | report |
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"]}))When to reach for it
When it backfires
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.
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
Adopting this pattern opens these attack surfaces. Each links to its entry in the threat model.
Full threat model →Keep going
Search patterns, frameworks, and pages.