Durable Agent Memory and Conversation State
Model conversation turns, exact task state and semantic memory with stable identities, suitable partitioning, retention and concurrency control.
A notebook that survives a restart
An agentβs model context is not a durable database. If the application holds everything in one process, a restart or scale-out can lose the thread.
Persist the information the next authorized request needs, then retrieve the right kind of memory deliberately.
| Kind of memory | Example | Retrieval contract |
|---|---|---|
| Current task state | A workflowβs committed current step | Stable identity and version checks |
| Recent conversation | The latest exchanges in a thread | Thread scope and explicit order |
| Long-term memory | An approved preference or useful summary | Authorized semantic or lexical recall |
A vector similarity result is not automatically the latest authoritative task state.
Use bounded memory units
The documented one-item-per-turn pattern stores an exchange as one unit. Include enough metadata to reconstruct a thread:
{
"id": "turn-0007",
"tenantId": "example-tenant",
"threadId": "thread-42",
"turnIndex": 7,
"messages": [
{ "role": "user", "content": "The example question" },
{ "role": "agent", "content": "The example response" }
]
}
Add model-generated embeddings when semantic recall is required. Keep the text and source metadata so a retrieved memory remains interpretable.
One permanently growing history item can approach the item-size limit and require increasingly expensive replacement. Bounded units permit independent expiry and more focused retrieval.
Partition according to access
| Candidate | Benefit | Trade-off |
|---|---|---|
| Random identifier per item | Broad write distribution | Thread reads may fan out |
| threadId | Locality for recent turns in one thread | A single oversized or very hot thread needs attention |
| Hierarchical tenantId, threadId | Tenant-prefix access plus thread locality | Still requires authorization and complete-key limit planning |
Do not use a constant key shared by every conversation.
For hierarchical keys, include the required key components in point operations. The leading tenant prefix can support tenant-scoped access, but a partition prefix does not grant permission to access that tenant.
Reconstruct recent conversation explicitly
Filter to the authorized thread and order by a stable turn index or suitable timestamp. Do not rely on arbitrary query return order.
Concurrent writers need an application policy for turn numbering and state updates. If two workers read the same state item, use _etag with a conditional write so one cannot silently overwrite the otherβs committed state.
A failed precondition is a signal to reread and reconcile or retry, not an instruction to blindly repeat the stale replacement.
Short-term versus long-term retention
Recent working context can have a TTL. Useful approved summaries or preferences may have a different retention policy.
TTL does not decide which facts should become long-term memory. Your application must choose what to retain, avoid unsupported inferences and enforce appropriate user or tenant boundaries.
Deleting expired live items also does not by itself define the retention behaviour of every backup or downstream copy.
Semantic recall
Store embeddings for the memory units that should be found by meaning. A returning user may paraphrase an older preference rather than repeat its original wording.
Use vector search within the authorized scope, with full-text or hybrid retrieval where exact terms matter. Sharded DiskANN can focus suitable searches, but it does not replace access checks.
Do not inject every stored memory into every prompt. Retrieve relevant context with a defined budget and distinguish a historical summary from the current authoritative task state.
Try it
Create a thread with several exchanges and a separate task-state item. Restart the application between turns. Verify that:
- The thread can be reconstructed in order.
- A stale
_etagupdate is rejected. - Expired working turns do not remove deliberately retained summaries.
- A paraphrased query recalls the right summary.
- A different tenant cannot select this memory by changing a request parameter.
Two workers read the same agent-state item and prepare different replacements. Which mechanism prevents a stale replacement from silently overwriting the first committed update?