Domain 3 β€” Module 3 of 5 60%
29 of 31 overall
Domain 3: Implement AI and analytics capabilities in database solutions Free ⏱ ~20 min read

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 memoryExampleRetrieval contract
Current task stateA workflow’s committed current stepStable identity and version checks
Recent conversationThe latest exchanges in a threadThread scope and explicit order
Long-term memoryAn approved preference or useful summaryAuthorized 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

CandidateBenefitTrade-off
Random identifier per itemBroad write distributionThread reads may fan out
threadIdLocality for recent turns in one threadA single oversized or very hot thread needs attention
Hierarchical tenantId, threadIdTenant-prefix access plus thread localityStill 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 _etag update 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.
Question

Should the agent retrieve its authoritative current task state only by vector similarity?

Click or press Enter to reveal answer

Knowledge Check

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?

Official references