The problem: your chatbot forgets everything when the conversation ends, so you add a memory store. A month later it tells a user something that stopped being true weeks ago, or it "remembers" one customer's allergy as another's. The memory is there. It's just wrong.
This article is part of the AI agent memory guide. We build SynapCores, and the SQL examples use it (the first two ran on SynapCores 2.4.3). The ideas work with any stack, and the Python example names open-source models.
How to give your AI chatbot memory of past customer interactions
Save each interaction as a short memory under the customer's ID, then recall the few that relate to the new message and put them in the prompt before the model answers. Recall by meaning, not by date, so "has my refund gone through?" finds the refund promise from three weeks ago even though the words differ. Keep the stored note short and factual (what was asked, what you promised, what's still open), because that note is what the model will read.
-- After each conversation: one memory per interaction, per customer
SELECT MEMORY_STORE('customer_4821',
'Oct 3: asked for a refund on order 1182; we promised it within 5 to 10 business days.',
json_object('kind', 'interaction', 'channel', 'email')) AS id;
-- When the customer writes again: recall what's relevant to the new message
SELECT content, similarity
FROM MEMORY_RECALL('customer_4821', 'Has my refund gone through yet?', 3);
With three memories stored for this customer (the refund, an app bug report and a contact preference), the refund promise comes back first at similarity 0.64; the next match scored 0.25. For more on recall, see recall agent memory by meaning and recency.
How to let an agent pick up a conversation exactly where it left off
Recall alone gives the agent background, not its place in the conversation. For that, save a small checkpoint per conversation at the end of every turn: a running summary and the one open task, and load it before the next reply. The upsert keeps exactly one row per conversation, so the agent always resumes from the latest state instead of replaying the transcript.
CREATE TABLE IF NOT EXISTS conversation_state (
conversation_id TEXT PRIMARY KEY,
customer_id TEXT,
summary TEXT,
open_task TEXT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- End of every turn: overwrite the checkpoint
INSERT INTO conversation_state (conversation_id, customer_id, summary, open_task)
VALUES ('conv-8812', 'customer_4821',
'Customer is moving their account to the Team plan; billing address confirmed.',
'Waiting for the customer to choose monthly or annual billing.')
ON CONFLICT (conversation_id) DO UPDATE
SET summary = excluded.summary, open_task = excluded.open_task,
updated_at = CURRENT_TIMESTAMP;
-- Start of the next session: resume from it
SELECT summary, open_task, updated_at
FROM conversation_state WHERE conversation_id = 'conv-8812';
Put the summary and open task at the top of the prompt, then the last few turns verbatim. The rolling-summary chatbot recipe shows how to keep that summary current with GENERATE().
The rest of this article is about the harder part: keeping long-term memory correct once facts start to change.
How a chatbot remembers: short-term vs long-term memory
| Short-term memory | Long-term memory | |
|---|---|---|
| What it is | Everything the model can see in this call: the conversation, tool results, scratch notes | Facts stored outside the model that survive across sessions |
| Limit | The context window | Your storage, and how well you recall from it |
| Lifetime | One conversation or task | Weeks, months |
| How the agent gets it | It's already in the prompt | The agent (or your code) recalls it and adds it to the prompt |
| Typical failure | Old turns fall out of the window or get summarised away | Wrong, stale or duplicated facts get recalled |
Short-term memory is mostly a prompt-management problem: trimming, summarising, keeping the important turns. This article is about long-term memory.
Episodic vs semantic memory
Long-term memory usually holds two kinds of thing:
- Episodic memory is what happened. "On Oct 3 the user asked about refunds and we said 5 to 10 business days." Episodes are append-only. A new episode never makes an old one false.
- Semantic memory is what is true now. "The user lives in Denver." "The account is on the Team plan." When the world changes, a new semantic fact should replace an old one.
Some systems also keep procedural memory: how to do things, such as "this user wants answers as bullet points" or "run the tests before opening a PR". It behaves like semantic memory, because a newer instruction replaces an older one.
The split matters because corruption is almost always a semantic-memory problem. Appending an episode can't make the store wrong. Replacing a fact can.
How to give an AI agent long-term memory
Most chatbot and agent memory, whatever the product, is the same loop:
conversation ──► extract facts ──► WRITE RULE ──► store
│
add / replace / skip / review
│
next turn ◄── put in prompt ◄── recall top-k ◄─────┘
- Extract. An LLM or a rule pulls short facts out of the conversation ("prefers email", "moved to Austin").
- Write. Something decides whether the fact is new, a duplicate, or an update to an existing memory.
- Recall. At the start of a turn, find the memories most related to the current message, usually with embeddings and similarity search.
- Use. Add them to the prompt.
Frameworks such as Mem0 and LangGraph's long-term store handle some or all of these steps for you, each in its own way.
When something simple is enough
Be honest about what you need before adding machinery.
- The facts have fixed keys. Plan, timezone, language, preferred name. Use a key-value store or a JSON profile per user. "Replace" means overwrite the key. There is nothing to get wrong about which memory to replace, and no model is needed.
- You only need episodes. A log of past conversations with search over it. Append-only can't be corrupted by updates.
- You're already on a framework. If your agent runs on LangGraph or uses Mem0, start with their memory. Add a stricter write check later if you find stale or wrongly merged facts.
- A person reviews memory. If someone approves each stored fact, a simple store plus a review screen is enough.
You need more than that when facts arrive as free text, from several sources, and nobody checks them before they're stored. That is where memory starts to rot.
How memory gets corrupted
Three things go wrong, and they pull in opposite directions.
1. Stale facts stay. "Maya moved to Austin last month" arrives. The store already has "Maya lives in Denver, Colorado." If the write rule only adds, both are stored, and recall can return either. The agent now has two cities for one person.
2. Contradictions are written blindly. A scraper or a second agent writes "the customer's office is in Chicago" without checking what's already known. Whether the new or old fact is right, nobody saw the conflict.
3. Look-alikes get overwritten. The fix for problem 1 is "replace the old fact". But which one? Memories that look alike are often about different things:
| Incoming fact | Stored memory it looks like | Same fact? |
|---|---|---|
| "Alex is allergic to peanuts." | "Jordan is allergic to penicillin." | No: different person |
| "Sam's return flight from Tokyo is on May 12." | "Sam's flight to Tokyo departs on May 3." | No: different attribute |
| "Jordan's Civic is due for an oil change." | "Jordan drives a 2018 Honda Civic." | No: compatible |
| "Priya used to run only 5 miles per week last year." | "Priya runs 20 miles per week." | No: past tense |
Replace any of those and the agent loses a true fact. That's worse than keeping a stale one: a stale fact can be noticed and fixed, but a deleted one is simply gone. (Examples from our internal, synthetic test set.)
Why similarity alone can't decide
The obvious write rule is "if the new fact is very similar to a stored one, replace it". Similarity measures topic, not meaning, so it fails both ways:
- Real updates often aren't that similar. "Moved to Austin" and "lives in Denver" share a person, not many words. A high threshold such as 0.95 catches almost none of them. In our internal tests, a similarity-only upsert at 0.95 missed nearly every real update.
- Look-alikes often are similar. The return and outbound flights score higher than many real updates do. Lower the threshold to catch updates, and you start replacing siblings.
Why a contradiction model alone can't decide either
The next idea is to ask a model whether the new fact contradicts the old one. An NLI (natural language inference) model does exactly that: given two sentences, it scores entailment, contradiction and neutral.
It's good at real conflicts. It's bad at saying "these are unrelated". In recipe 036 (re-run on SynapCores 2.3.2, cross-encoder/nli-deberta-v3-small), "The customer is learning to play the cello." scored 0.996 to 1.000 contradiction against every one of four unrelated memories, including "The customer's office is in Denver." The model was trained on sentence pairs meant to be compared, so it never learned to say "different topic".
Let that model decide which memories to overwrite, and it deletes unrelated facts. In our internal tests, NLI with no similarity gate often overwrote good memories that had nothing to do with the new fact.