Agent Memory, State and Durable Execution

How agents remember: short-term working memory, conversation history management, long-term semantic, episodic and procedural memory, memory write and retrieval policies, state machines, checkpoints, durable execution for long-running agents, and privacy.

On this page

Simple definition#

LLMs are stateless: each call knows only what is in its prompt. Memory is everything your system stores and feeds back so an agent can continue a conversation, remember a user’s preferences, learn from past cases, and resume a long task after a crash.

Types of memory#

Memory types
flowchart TB
    M["Agent memory"] --> ST["Short-term / working memory<br/>current conversation and task state<br/>lives in the context window"]
    M --> LT["Long-term memory<br/>persists across sessions<br/>stored outside the model"]
    LT --> SEM["Semantic<br/>facts: 'customer prefers email',<br/>'uses product X v3'"]
    LT --> EPI["Episodic<br/>past interactions and outcomes:<br/>'last month's ticket was solved by …'"]
    LT --> PRO["Procedural<br/>how to do things: learned instructions,<br/>playbooks, improved prompts"]
Type Human analogy Storage Example
Working / short-term What you are thinking about now Context window + session store Last 10 messages, current plan, tool results
Semantic Facts you know Database / vector store / profile “User is in the Munich office; prefers German”
Episodic Remembering past events Vector store of past episodes Summary of a similar resolved incident
Procedural Skills Prompts, playbooks, tool definitions “For VPN issues, check certificate first”

Managing short-term memory#

Conversation history grows each turn and is re-sent every call. Strategies:

Keeping history within budget
flowchart LR
    H["Full history<br/>40 turns, 30K tokens"] --> W["Sliding window<br/>keep last N turns"]
    H --> SUM["Summarise older turns<br/>+ keep recent verbatim"]
    H --> TR["Trim tool results<br/>keep only key fields"]
    H --> RET["Retrieve relevant past turns<br/>vector search over history"]
    W --> P["Prompt within budget"]
    SUM --> P
    TR --> P
    RET --> P
  • Sliding window — simplest; loses early details (like the user’s name or order number).
  • Summary + window — a running summary of older turns plus the last few turns verbatim. The usual production choice.
  • Pin key facts — extract and keep important entities (order ID, decision made) in a structured “state” section.
  • Clear tool results — old, bulky tool outputs can be dropped or shortened once used.

Long-term memory: write and read#

Long-term memory flow
sequenceDiagram
    participant U as User
    participant A as Agent
    participant MS as Memory service
    participant DB as Memory store
    U->>A: new message
    A->>MS: recall(user_id, current query)
    MS->>DB: profile facts + semantic search over memories
    DB-->>MS: relevant memories
    MS-->>A: "Prefers email, had failed payment 2 weeks ago"
    A->>A: respond using memories
    A-->>U: answer
    A-)MS: extract new memories from this turn (async)
    MS->>MS: dedupe, resolve conflicts, check policy
    MS->>DB: upsert memories with source, timestamp, TTL

Write policy — what is worth remembering? Extract stable facts and preferences, not every message. Resolve conflicts (new address replaces old). Record source and time.

Read policy — retrieve only memories relevant to the current task (by user, recency and similarity), and show them to the model as data, clearly labelled.

Forget policy — expiry (TTL), user-requested deletion, and correction.

State machines and workflow state#

For structured processes, model the agent’s progress as explicit state, not just chat history:

Claims agent as a state machine
stateDiagram-v2
    [*] --> Intake
    Intake --> GatherEvidence: claim details complete
    Intake --> AwaitCustomer: details missing
    AwaitCustomer --> Intake: customer replies
    GatherEvidence --> Assess
    Assess --> AwaitApproval: payout > limit
    Assess --> Payout: payout ≤ limit
    AwaitApproval --> Payout: approved
    AwaitApproval --> Rejected: rejected
    Payout --> [*]
    Rejected --> [*]

The LLM handles judgement inside states (understanding documents, assessing evidence); code controls the transitions. This hybrid is common in regulated industries because it is auditable.

Durable execution and checkpoints#

Agents that run for minutes, hours or days — waiting for approvals, external systems or users — must survive restarts and deployments.

Checkpointed agent execution
flowchart LR
    S1["Step 1: retrieve data"] --> CP1[("Checkpoint")]
    CP1 --> S2["Step 2: call LLM"] --> CP2[("Checkpoint")]
    CP2 --> S3["Step 3: wait for approval<br/>hours"] --> CP3[("Checkpoint")]
    CP3 --> S4["Step 4: execute payment"]
    CRASH["Pod restarts during step 3"] -.->|"resume from CP2/CP3,<br/>no repeated LLM calls or payments"| S3
  • Checkpointing — save state after each step (LangGraph checkpointers, Spring AI chat memory repositories, your own tables).
  • Durable execution engines — Temporal, AWS Step Functions, Azure Durable Functions, Restate — persist every step and replay deterministically after failures.
  • Idempotency — side-effecting tools must use idempotency keys, because a step may be retried.
  • Time travel / replay — saved checkpoints let you inspect and debug what the agent saw at each step.

Privacy and governance of memory#

  • Memory is personal data when it is about people: apply purpose limitation, retention limits, access control and the right to erasure.
  • Let users see and delete what the assistant remembers.
  • Separate memories by tenant and user; never retrieve one user’s memories for another.
  • Memory poisoning — an attacker plants false “facts” through content the agent reads. Only write memories from trusted signals, record provenance, and allow review.

Common mistakes#

Review questions#

What types of memory does an agent have? Short-term working memory (the current context and task state) and long-term memory stored outside the model: semantic (facts and preferences), episodic (past interactions and outcomes) and procedural (instructions and playbooks).

How do you handle long conversations within the context window? Keep the latest turns verbatim, summarise older turns, pin key facts in a structured state section, trim old tool results, and optionally retrieve relevant earlier turns by similarity.

How would you make a long-running agent reliable? Persist state at every step (checkpoints) or use a durable execution engine, make side-effecting tools idempotent, support pause/resume for human approval, and trace each step so runs can be replayed and debugged.

What are the privacy concerns with agent memory? Memory often contains personal data, so it needs consent and purpose, retention limits, access control, user visibility and deletion, tenant isolation, and protection against memory poisoning.

Summary#

  • LLMs are stateless; memory is a system you build.
  • Short-term memory: window + summary + pinned facts. Long-term: semantic, episodic, procedural.
  • Define write, read and forget policies for long-term memory.
  • Model structured processes as state machines; checkpoint long-running agents; make actions idempotent.
  • Treat memory as governed personal data.

References#