Skip to content

Memory, State and Long-Running Work ​

Keep the facts an agent needs where the window cannot lose them, and make a long run survive a crash without paying for its work twice.

In this chapter: three kinds of memory · clearing tool output before summarising · a working state outside the conversation · checkpoints and idempotent effects · approval as a pause

💡 The Core Idea ​

A model is stateless: it remembers nothing between calls. Every memory is something your code put back into the window. So memory is a storage and selection problem you own. It has three parts that people mix up: what the model sees this turn, what survives compaction, and what is still true next week.

Long runs add a fourth problem: what survives a crash. An agent loop that takes forty minutes cannot live in an HTTP request. Deploys, timeouts and crashes all end it, and every lost step was a paid model call. So a long-running agent is a durable workflow that happens to call a model.

The conversation is a cache, not a database. The unit of durability is the step. If a step completed, its result must survive. If it did not, it must be safe to run again.

How It Works ​

Three kinds, three lifetimes ​

KindLives inLifetimeCost
Working contextThe windowThis requestTokens, every step
Session stateYour store, reloaded per turnThe task or conversationStorage, plus selection logic
Long-term memoryA database or indexAcross sessionsStorage, plus retrieval and staleness

Most "it forgot" bugs are a missing middle row. The fact was in the window ten steps ago. Compaction (shrinking the history to fit) dropped it, and nothing else held it.

Tool output eats the window ​

In an agent, history is mostly tool output: file contents, search results, API responses. The documentation assistant reads whole pages of docs. By step 8, tool results can be 94,000 tokens against 6,000 of messages. Most were read once and never used again. So clearing old tool results is the cheapest way to get space back. It needs no model call. Leave a marker, so the model fetches the result again when it needs it, instead of deciding the step never happened.

Clearing stale tool results, with a marker left behind

typescript
function clearStaleToolResults(messages: Message[], keepRecent = 3): Message[] {
  const cutoff = messages.length - keepRecent * 2;
  return messages.map((m, i) =>
    m.role === 'tool' && i < cutoff
      ? { ...m, content: `[cleared: ${summarise(m)} — call the tool again if needed]` }
      : m,
  );
}

When the conversation must shrink, go cheapest first: clear tool results (free), then summarise old turns (a model call, and a model decides what mattered). Truncating is free but loses facts silently. Context Engineering has the full table.

⚠️ Summarisation loses things in ways you cannot inspect. In the classic failure, the agent forgets a constraint from the first message, such as "do not touch the billing service". A summary at step 12 judged it no longer relevant. Pin constraints outside the summary.

A working state the window cannot lose ​

Stop treating the conversation as the source of truth. Keep a small, structured state next to the loop. Update it as the agent works, and inject it again every turn.

The state that survives compaction and restarts

typescript
interface AgentState {
  readonly goal: string;
  readonly constraints: string[];        // pinned; never summarised away
  readonly findings: Record<string, string>;
  readonly completed: string[];
  readonly failed: { step: string; reason: string }[];
}

It costs a few hundred tokens. Constraints survive compaction. Finished work is not repeated. Failures stay visible, so the agent stops retrying them. And because it is structured, it can be saved to disk.

Long-term memory ​

Memory across sessions is retrieval with a different corpus. See Embeddings, Vector Stores and Retrieval. The hard problem is contradiction over time. "The user prefers dark mode" stored in March may be wrong in June, and the store cannot tell. Timestamp every memory, prefer recent ones, and let new facts replace old ones. Memory is also personal data. Never store secrets or tokens, give memories a time-to-live, and scope them by user id in the store, not the prompt.

Persist after every step ​

A checkpoint, written before the next step starts

typescript
interface Checkpoint {
  readonly runId: string;
  readonly step: number;
  readonly messages: Message[];      // the conversation so far
  readonly state: AgentState;        // goal, constraints, findings
  readonly status: 'running' | 'awaiting-approval' | 'done' | 'failed';
  readonly spentTokens: number;
}

async function step(run: Checkpoint): Promise<Checkpoint> {
  const res = await model.generate({ messages: run.messages, tools });
  const results = await Promise.all(res.toolCalls.map((c) => dispatch(c, run.runId)));
  const next = advance(run, res, results);
  await checkpoints.put(next);        // durable before anything else observes it
  return next;
}

A crash now costs at most one step. A resume reads the last checkpoint. It does not replay from the start, because a model step is not deterministic. The budget lives in the checkpoint too. Kept in process memory, it would reset on every restart, and a crash-loop would become an unbounded bill.

Duplicate effects are the hard part ​

Retrying a read is free. Retrying sendEmail sends a second email. A crash between "tool ran" and "checkpoint written" is exactly where that happens. The fix is an idempotency key, an id that lets the receiver ignore a repeat. Derive it from runId and step, because a fresh key on retry defeats it.

ApproachHowFits
Idempotency key${runId}:${step}:${toolName} passed downstreamAny API that supports it
Write-ahead intentRecord "about to call X"; on resume, check whether it happenedSystems with no idempotency support
Design it outsetStatus('sent') rather than incrementCounter()Always, where possible

Retries at three levels ​

  1. The tool. Retry transient failures with backoff and the same idempotency key.
  2. The model. If the tool is really down, return the failure as an observation, such as "the search index is unavailable". Then the model can take another route or stop honestly.
  3. The run. If nothing works, fail checkpointed, so a retry resumes rather than restarts.

The middle level is the one people leave out. Without it, a run burns its budget on one broken tool.

Human approval is a pause, not a prompt ​

An approval gate on a destructive action can suspend a run for days. That is a state, not a blocking call.

Suspending the run until someone approves

typescript
if (tool.destructive) {
  await checkpoints.put({ ...run, status: 'awaiting-approval', pending: call });
  await notify(run.owner, call);
  return;                                   // the process exits; nothing is held open
}
// resumed later by an approval webhook, which loads the checkpoint and continues

Nothing is held open, so a deploy during the wait is harmless. The approval carries runId and step, so a double-clicked button resolves to the same step and cannot run the action twice. Give approvals a timeout, so a run does not resume a week later into a world that has moved on.

⚠️ Moving target: durable execution frameworks, such as hosted workflow runtimes, queue-backed schedulers and agent platforms, change quickly and name these ideas differently. Whatever the framework, check that it checkpoints every step, keys effects idempotently and models approval as a suspended state.

When to Use It ​

SituationMechanism
Window full after ten stepsClear old tool results first
Constraints must never be lostStructured state injected every turn
Facts needed next weekLong-term store with timestamps and expiry
Run of 5 min or more, or scheduledCheckpointed workflow, idempotent tools, resumable
Any destructive toolApproval gate, whatever the duration

Common Mistakes ​

❌ Treating the conversation as durable storage

It gets compacted, truncated and lost on restart. Anything that matters lives outside it.

✅ Pin goal and constraints in a structured state

They cost a few hundred tokens, and losing them causes the worst failures.

❌ Running a long agent inside an HTTP request, or holding it open for approval

The first deploy kills it, and every completed step is paid for twice.

✅ Checkpoint every step, persist the budget, and exit while waiting

A resume costs at most one step, and a crash-loop stops at the budget you chose.

🔑 Key Takeaways ​

  • Models are stateless, so every memory is something your code supplies again. That makes memory a storage problem you own.
  • Tool output fills an agent's window, so clear it before summarising anything.
  • Pin goal and constraints in a small structured state, because summaries drop them silently.
  • A long-running agent is a durable workflow: checkpoint every step, and persist the budget with it.
  • Derive idempotency keys from run and step, and model human approval as a suspended state.

Interview Questions ​

Q: Your agent forgot a constraint from the first message. What went wrong?

Compaction. A summary at a later step judged the constraint no longer relevant, and nothing raised an error. A better summariser is not the fix. I keep goal and constraints in a structured state and inject it every turn. That costs a few hundred tokens and removes the failure mode.

Q: An agent's window fills after ten steps. Summarise, or something else first?

Something else first: clear old tool results. Most of the window is tool output read once and never used again. Clearing it needs no model call. Summarising costs a call and lets a model decide what mattered. I leave a marker so the agent knows it can fetch the result again.

Q: How do you stop a retry from sending two emails?

An idempotency key built from run id, step number and tool name. Downstream, it turns the retry into a no-op. The key must be derived, not generated, because a fresh key on retry defeats it. Without idempotency support downstream, I record the intent first and check on resume whether it happened.

Q: How do you implement human approval for a destructive step?

As a state change, not a blocking wait. The run checkpoints as "awaiting approval" with the pending call, notifies the owner, and the process exits. A webhook loads the checkpoint and resumes. So a deploy during the wait is harmless, and a duplicate approval cannot run the action twice. The approval also expires.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.