Long-term memory for AI agents: how it works
How long-term memory for AI agents works: scoped facts, recall by relevance and recency, retiring contradictions, time travel, graphs and the limits.
Long-term memory for AI agents is a store outside the model's context window where an agent writes the facts it learns and reads them back by meaning in a later session. A working memory layer does four jobs: it scopes facts to one agent and user, ranks recall by relevance and recency, retires facts that have been contradicted, and forgets on request.
In plain words. The context window is working memory, and it empties when the session ends. Long-term memory is a small, searchable store of facts that the agent writes in one session and reads back by meaning in the next.
Why do AI agents forget between sessions?
A model can only use what is in its context window. Anthropic's documentation describes the window as all the text a model can reference when generating a response, a "working memory" that is separate from its training data, and notes that accuracy and recall degrade as the token count grows (context windows). Nothing in that window survives the end of a session unless your application saves it.
The usual patch is to replay the whole history into every prompt. That costs tokens on every turn, and it stops working once the history is longer than the window. Research systems solve it by moving memory outside the model. MemGPT manages tiers of memory the way an operating system moves data between fast and slow storage. Generative Agents keeps a complete record of an agent's experiences and retrieves the relevant ones when it plans. The shared idea: write facts to a store, then retrieve only the few that matter for the current step.
What does a long-term memory layer have to do?
Storing embeddings is not enough. A memory layer has to know whose fact it is, which fact is current, and when to let go. These are the jobs, with the Crowkis command that does each one:
| Job | Why it matters | Command |
|---|---|---|
| Scope facts to one agent and user | One user's facts must never answer another user's question | CMEMSET with USER |
| Recall by meaning | "How should we contact them?" has to find "prefers email" | CMEMGET |
| Retire contradicted facts | "Lives in Berlin" should replace "lives in Paris", not sit beside it | built into CMEMSET |
| Keep history | Someone will ask what the agent believed in March | CMEMHISTORY, CMEMASOF |
| Pull facts out of conversation | Nobody writes facts by hand in production | CMEMEXTRACT |
| Hold relationships | "Who works at this customer's company?" is a graph walk | CMEMLINK, CMEMGRAPH |
| Forget on request | Users ask to be forgotten | CMEMFORGET |
A useful outside checklist comes from LongMemEval, a benchmark that tests five abilities of a chat assistant's memory: information extraction, multi-session reasoning, temporal reasoning, knowledge updates, and abstention. Any memory layer you evaluate should have an answer for each.
How do you store and recall a fact?
CMEMSET agent fact [USER u] [EX secs]
CMEMGET agent query [USER u] [K n]
CMEMSET support "prefers email over phone" USER u_42
CMEMSET support "lives in Paris" USER u_42
CMEMGET support "how should we contact this customer?" USER u_42 K 3The first argument is the agent and the second is the fact. USER scopes the fact to one person, and EX gives it an expiry in seconds. The fact is embedded by the bundled model and indexed for semantic recall, so nothing is sent to an outside API (see local offline embeddings). Memory is invisible to CGET: a fact about one user can never come back as a cached answer for the whole tenant.
CMEMGET returns the top K facts, ranked by relevance blended with recency. The recency boost is mild. It decays with a half-life set by CROWKIS_MEMORY_HALFLIFE_SECS, which defaults to 30 days. Each (agent, user) scope is bounded: past CROWKIS_MEMORY_MAX_PER_USER, 500 by default, the oldest facts are forgotten automatically.
What happens when a fact changes?
CMEMSET consolidates. When a new fact contradicts a stored fact about the same subject, the old one is retired. It is kept for history and never deleted, and CMEMGET returns only the currently valid version of each subject. You do not have to find and delete the stale fact yourself.
CMEMSET support "lives in Berlin" USER u_42
CMEMGET support "where does this customer live?" USER u_42 K 1
CMEMHISTORY support "where does this customer live?" USER u_42
CMEMASOF support "where does this customer live?" 1767225600000 USER u_42CMEMHISTORY agent query [USER u] [K n] returns retired versions too. Each match is [fact, similarity, state, recorded_ms, valid_to_ms], newest first, where state is current or superseded. CMEMASOF agent query unix_ms [USER u] [K n] answers a narrower question: which facts did memory believe were current at that instant? Recording when a fact was written and when it stopped being valid is the idea a temporal database uses to keep valid time apart from transaction time. It lets you reconstruct why an agent said what it said last month.
Every CMEM* command also takes an optional TOPIC <t>. Storing with a topic files the fact in that room. Recalling with a topic returns only that room, and recalling without one sees every room for the user. Contradictions resolve within a room only. A coding agent can keep one room per repository, so a naming convention learned in one project does not retire a different convention in another.
How do facts get into memory from a conversation?
CMEMEXTRACT agent "<conversation>" [USER u] [EX secs]CMEMEXTRACT takes a chunk of conversation and keeps the durable facts: identity, preference and first-person statements. It drops questions, greetings and filler, stores each kept fact through the same consolidating path as CMEMSET, and returns the facts it kept. It is model-free and deterministic. There is no LLM call, and the same transcript always yields the same facts.
The trade-off is reach. Rules catch statements, not implications. If you need facts that a conversation only implies, extract them with your own model and write them with CMEMSET.
Sessions and graphs: the other two kinds of memory
Facts are one shape of memory. Two others come up in most agents.
CSESSION ADD s role text [EX secs]
CSESSION RECENT s [N n]
CSESSION SEARCH s query [K n]
CMEMLINK agent subject relation object [USER u]
CMEMGRAPH agent entity [USER u] [DEPTH d]Session memory is the conversation buffer. CSESSION ADD appends a turn with its role. RECENT reads the last turns for the usual sliding window. SEARCH runs a semantic query over the whole buffer, so "what did they say about budget earlier?" finds a turn the window has already dropped. A session keeps at most CROWKIS_SESSION_MAX_TURNS turns, 500 by default, and the oldest drop first.
Graph memory stores relationships as subject, relation, object edges, the same shape as a semantic triple. CMEMGRAPH walks outward from an entity to a given depth and returns the triples it finds. Fan-out per entity is capped by CROWKIS_GRAPH_MAX_EDGES, 512 by default, and one traversal reads at most 2000 edges.
Is one user's memory isolated from another's?
Yes, by scope. Two different USER values never see each other's data, and structural characters are stripped from identifiers so one cannot be forged into another. Memory lives in its own key family, which plain key-value commands cannot reach. The wider design is covered in tenant isolation. Two details decide whether the isolation means anything in your app:
- The application owns the mapping. Crowkis cannot verify that your
USERvalues map to real people. Pass one constant for everyone and you have told it they are one person. - An unscoped write is shared.
CMEMSETwithoutUSERlands in one bucket per agent that every caller of that agent in the tenant can read. Because its reach is tenant-wide, the text is redacted for personal data before storage, as the response cache is. A user-scoped fact is stored as written, so the assistant recalls the address the user gave and not a placeholder. See PII scrubbing and erasure.
CMEMFORGET agent [query] [USER u] [THRESHOLD f] forgets the facts that match a query, or the whole (agent, user) memory when no query is given.
How do you call it from Python or an MCP client?
Any RESP3 client can send the commands above. The Python SDK also wraps them in a CrowkisMemory object, documented in the Python SDK reference:
from crowkis import CrowkisMemory
mem = CrowkisMemory(agent="support-bot", user="alice")
mem.remember("Alice prefers email over phone")
mem.recall("how should I contact Alice?")The same object has extract, history, as_of, forget, link and graph. If your assistant is Claude Code or another MCP client, the Crowkis MCP server gives it memory with no code at all.
When not to use agent memory
- As a transcript archive. A scope holds 500 facts by default and trims the oldest. It is built for durable facts, not for every message.
- For documents. Manuals and policies belong in a document store. See self-hosted RAG with CDOC.
- When you need exhaustive recall inside one very large shared index. Scoped search is a bounded walk with a budget of 16,384 node visits. A fact sitting behind more than roughly sixteen thousand nearer out-of-scope vectors can be missed.
- At a scale nobody has measured. The largest vector index verified so far is 1.18M vectors, on a development laptop. Recall quality on public datasets is in the agent memory benchmarks.
Memory saves cost because the agent stops re-sending and re-deriving context it already had. How much it saves depends on how repetitive your sessions are.
Common questions
Is long-term memory the same as RAG?
No. RAG retrieves passages from documents you own. Memory stores facts the agent learned from a user, scoped to that user, and updates them when they change. Both use vector search, and many agents need both.
How is agent memory different from a semantic cache?
A semantic cache reuses a finished answer for anyone in the tenant who asks the same thing. Memory holds facts about one user and never appears in cache lookups. In Crowkis the two run in one engine, in separate key families.
Do I need a separate vector database for agent memory?
Not for this. Crowkis indexes memory in its own in-process vector index. A dedicated database makes sense when you need large-scale document search, as semantic cache vs vector database explains.
Can a user ask the agent to forget them?
Yes. CMEMFORGET with a USER and no query removes that user's whole memory for the agent. With a query it removes only the matching facts, and THRESHOLD controls how close a match has to be.