Long-Term Memory: What Should Survive Between Agent Sessions
Long-term memory is the set of facts, preferences, and durable state you deliberately keep after a session ends so later sessions can start smarter.
Search across all documentation pages
Long-term memory is the set of facts, preferences, and durable state you deliberately keep after a session ends so later sessions can start smarter.
Everything else - raw chat turns, one-off tool dumps, temporary plans - should reset or compress so cost, privacy, and correctness stay under control.
An agent session is one bounded run of the perceive-decide-act loop: a chat thread, a ticket handling job, or a CI agent invocation.
Short-term memory lives inside that run (or a short TTL keyed by session_id): recent messages, the current plan, tool observations still relevant to the open goal.
Long-term memory outlives the run and is keyed by a stable principal - usually user_id, tenant_id, project_id, or a combination.
The decision "should this survive?" is a product and safety policy, not an automatic side effect of chatting.
Three filters help:
Examples that usually should survive:
Examples that usually should not survive as raw long-term memory:
Prefer pointers over copies when the truth lives elsewhere: store order_id and a retrieval recipe, not a frozen snapshot of the order.
At write time, something must promote content from the session into durable storage.
Common promotion paths:
preferred_language=pt-BR).At read time, a later session loads a small, relevant slice of long-term memory into context or tools.
You almost never dump the entire memory store into the prompt.
Session start typically looks like:
Scope matters as much as content.
| Scope | Survives for | Example |
|---|---|---|
| User | That person across products | "Prefer concise answers" |
| Project / workspace | Everyone on the project | "Default deploy env is staging" |
| Org / tenant | Company-wide defaults | "PII redaction rules" |
| Session only | One run | Current tool traces |
Crossing scopes by accident is a privacy bug: never promote user A facts into a shared org profile without a deliberate product decision.
# Conceptual gate: only durable, non-sensitive, re-usable claims become LTM
def should_persist(claim: dict) -> bool:
if claim.get("contains_secret"):
return False
if claim.get("source") == "speculation":
return False
if claim.get("ttl_hours", 0) == 0 and not claim.get("user_confirmed"):
return False
return claim.get("durable", False) and claim.get("reuse_likely", False)This gate is illustrative.
Production policies add schema validation, PII detectors, and human review for high-risk categories.
Freshness vs fidelity. Some memories need TTLs (temporary travel dates). Others need versioning (address changes). Stale durable facts are worse than missing ones when the agent acts confidently.
System of record vs agent memory. If CRM already stores the customer tier, teach the agent a tool to read CRM. Use long-term memory for preferences and interaction learnings CRM does not hold.
Compliance. Retention limits, right-to-erasure, and regional storage apply to agent memory like any other personal data store. Design delete-by-user-id from day one.
Multi-agent systems. Specialists should not each keep private contradictory long-term stores for the same principal without a shared write policy. Prefer one memory service with role-scoped read/write.
Eval. Measure not only task success but memory quality: precision of stored facts, retrieval usefulness, and rate of user corrections after "remembered" behavior.
| Approach | Strength | Weakness | Best Fit |
|---|---|---|---|
| Persist nothing | Simple, private | No continuity | One-shot tools, stateless APIs |
| Always-on profile fields | Cheap, predictable | Limited expressiveness | Preferences and defaults |
| Full transcript store as "memory" | Easy to implement | Noisy, costly, stale | Audit logs only, not agent context |
| Extracted fact store + retrieval | Scalable continuity | Extraction and conflict cost | Multi-session assistants |
| System-of-record tools only | Always fresh | No soft preference learning | Ops agents over live systems |
Durable state stored outside a single run, loaded later to personalize or ground future decisions for the same principal.
Document RAG retrieves shared knowledge bases. Long-term memory is usually per-user or per-tenant interaction state plus preferences, though both may use similar vector stores.
Store summaries or identifiers when re-use is likely. Do not store large raw payloads that will be stale or blow retrieval budgets.
On logout for shared devices, after sensitive workflows, when the user says "forget that," and when TTLs expire.
Yes for user and team conventions (style, deploy norms). Prefer repo files and issue trackers as systems of record for project truth.
Fine for demos and single-device preferences. Multi-device, multi-agent, and compliance-sensitive products need a server-side store keyed by authenticated identity.
Small enough to include every turn without retrieval: often a few hundred tokens of structured fields, not free-form essays.
Prefer trusted server paths: validated tools, extraction jobs, and admin APIs. Do not let untrusted tool output silently become permanent fact.
The active goal, recent turns, scratchpad plans, and tool observations for the current task. See short-term memory coverage in the sibling section.
Store with an explicit expiry timestamp and filter expired rows at retrieval. Do not rely on the model to remember the TTL.
Yes if every row is scoped by tenant_id (and usually user_id) and every query enforces that predicate. Missing the predicate is a severe isolation bug.
Possibly for audit copies. Separate compliance archives from online agent memory so product delete and legal retention do not fight the same row without process.
High user correction rate ("I never said that"), rising token cost from memory, or frequent retrieval of unused facts.
Stack versions: Pins from the category manifest (verify at build): OpenRouter (~315+ models, July 2026 pricing/fees); LangGraph 1.0+; CrewAI 1.14+; Microsoft Agent Framework 1.0; Vercel AI SDK 6; Pydantic AI (latest); LlamaIndex (latest); OpenAI Agents SDK (latest + MCP); MCP (Linux Foundation governance); A2A (HTTP+SSE+JSON-RPC 2.0); Solana
@solana/web3.js+@solana/spl-token.
Reviewed by Chris St. John·Last updated Jul 16, 2026