An agent's memory gets written by many hands. The chat, a CRM sync, a scraper, another agent. Sooner or later two of them disagree. One says the customer's office is in Denver, another says Chicago. One says the plan has a 14-day return window, another says the plan "does not allow returns".
Most memory layers store both. At recall time, whichever sentence is closest to the question comes back, and the agent now holds two beliefs that can't both be true. It doesn't know that, and neither do you.
This post shows how to catch those contradictions at write time, before the second fact is stored. The check runs inside the database: EMBED finds the existing memories on the same topic, a local NLI (natural language inference) model checks the new fact against them, and SQL sorts the batch into conflict, already-known and new. There's no LLM judge and no external API call.
We build SynapCores, and the examples are SynapCores SQL. Every query below, and the output shown for it, comes from recipe 036, tested on SynapCores 2.5.1.
We'll also cover the part most posts skip: the NLI model gives confident wrong answers when you use it naively, and the thresholds that are fine for flagging are too loose for deleting.
Watch the video: AI Agent Memory: Types, Architecture & How to Build It (2026) builds agent memory in SQL step by step. Its write-rule chapter, at 6:47, runs this contradiction check on six incoming facts.
Why recall can't fix this later
Similarity search ranks by closeness, not by consistency. "The customer's office is in Denver" and "The customer's office is in Chicago" are almost the same sentence to an embedding model. Once both are stored, a question about the office can return either one, or both. Nothing at read time knows which is current.
So the cheapest place to catch a contradiction is when the fact is written. That needs two things: a way to find the memories the new fact is about, and a way to tell whether it agrees with them. Similarity does the first. An NLI model does the second. It reads two sentences and scores whether the second follows from the first (entailment), conflicts with it (contradiction), or neither.
Before you start: turn NLI on
The NLI functions run on a local model that you install and enable once. Install and enable the curated model (DeBERTa-v3-small, Apache-2.0, English, about a 577 MB download), then restart the engine:
synapcores --config /etc/synapcores/gateway.toml pull nli-default
synapcores --config /etc/synapcores/gateway.toml models enable-nli nli-default
For Docker or environment-driven installs, set AIDB_NLI_ENABLED=1 and AIDB_NLI_MODEL=nli-default at startup. GET /v1/ai/nli/models should report the model as ready.
If you don't have SynapCores yet, the Community Edition is free and self-hosted:
curl -fsSL https://get.synapcores.com | SYNAPCORES_REF=blog-nli-memory sh
Step 1: What the agent already believes
Memories here are plain rows with an embedding, so you can join on them. (SynapCores also has a built-in memory layer: REMEMBER, CURRENT, TRACE and RECALL in SQL, and /v1/memory/* over REST. Step 7 uses the lower-level MEMORY_RECALL function. A table makes the batch check easiest to see.)
CREATE TABLE IF NOT EXISTS recipe_memcheck_memories (
id INT PRIMARY KEY,
fact TEXT,
embedding VECTOR(384)
);
INSERT INTO recipe_memcheck_memories (id, fact, embedding) VALUES
(1, 'The customer prefers to be contacted by email, never by phone.',
EMBED('The customer prefers to be contacted by email, never by phone.')),
(2, 'The customer is on the Aurora plan, which includes a 14-day return window.',
EMBED('The customer is on the Aurora plan, which includes a 14-day return window.')),
(3, 'The customer manages procurement for a mid-size logistics company.',
EMBED('The customer manages procurement for a mid-size logistics company.')),
(4, 'The customer''s office is in Denver.',
EMBED('The customer''s office is in Denver.'));
Step 2: A batch of new facts from different writers
CREATE TABLE IF NOT EXISTS recipe_memcheck_incoming (
id INT PRIMARY KEY,
source TEXT,
fact TEXT,
embedding VECTOR(384)
);
INSERT INTO recipe_memcheck_incoming (id, source, fact, embedding) VALUES
(101, 'chat', 'The customer asked us to call them by phone from now on.',
EMBED('The customer asked us to call them by phone from now on.')),
(102, 'crm-sync', 'The customer works in logistics procurement.',
EMBED('The customer works in logistics procurement.')),
(103, 'webinar-tool', 'The customer attended the October product webinar.',
EMBED('The customer attended the October product webinar.')),
(104, 'scraper', 'The customer''s office is in Chicago.',
EMBED('The customer''s office is in Chicago.')),
(105, 'agent-b', 'The customer''s plan does not allow returns.',
EMBED('The customer''s plan does not allow returns.')),
(106, 'chat', 'The customer is learning to play the cello.',
EMBED('The customer is learning to play the cello.'));
We know the right answer up front. Facts 101, 104 and 105 conflict with a stored memory. Fact 102 restates one. Facts 103 and 106 are new.
Step 3: Look at the scores before you pick a threshold
Before writing the check, list every incoming/memory pair the NLI model scores as a contradiction (above 0.6), next to its topic similarity:
SELECT i.id AS incoming, m.id AS memory,
ROUND(COSINE_SIMILARITY(i.embedding, m.embedding), 3) AS similarity,
ROUND(CAST(JSON_EXTRACT(NLI_CLASSIFY('nli-default', m.fact, i.fact),
'$.scores.contradiction') AS DOUBLE), 3) AS contradiction
FROM recipe_memcheck_incoming i
CROSS JOIN recipe_memcheck_memories m
WHERE CAST(JSON_EXTRACT(NLI_CLASSIFY('nli-default', m.fact, i.fact),
'$.scores.contradiction') AS DOUBLE) > 0.6
ORDER BY similarity DESC;
The result (the "reality" column is ours):
| incoming |
memory |
similarity |
contradiction |
reality |
| 104 |
4 |
0.740 |
0.999 |
real conflict (office city) |
| 105 |
2 |
0.524 |
1.000 |
real conflict (returns) |
| 101 |
1 |
0.502 |
0.999 |
real conflict (contact channel) |
| 104 |
2 |
0.408 |
0.760 |
false: office city vs plan |
| 103 |
3 |
0.366 |
0.888 |
false: webinar vs procurement job |
| 106 |
4 |
0.235 |
0.997 |
false: cello vs office |
| 106 |
3 |
0.228 |
1.000 |
false: cello vs procurement job |
| 106 |
1 |
0.169 |
0.999 |
false: cello vs contact channel |
| 106 |
2 |
0.132 |
0.996 |
false: cello vs plan |
Nine pairs score as contradictions. Three are real.
This is the most important table in the post. An NLI model scores pairs of sentences. Hand it two sentences about different things and it often still says "contradiction", with high confidence. The cello fact "contradicts" all four memories at 0.996 or higher. The contradiction score can't separate real conflicts from false ones here.
Similarity can. The real conflicts sit between 0.50 and 0.74. The false ones are at 0.41 or below. We set the floor at 0.45, inside that gap. That number was read off this data. It isn't a universal constant, so run this query on a sample of your own memories before you pick yours.
Step 4: The trap: check against every memory
NLI_ANCHORS collects a group's memories into an evidence array, and NLI_VERIFY checks the new fact against all of them. Here's the naive version, with no similarity gate:
SELECT i.id, i.source,
JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default', i.fact, NLI_ANCHORS(m.fact)),
'$.verdict')) AS verdict,
JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default', i.fact, NLI_ANCHORS(m.fact)),
'$.violated_anchor')) AS conflicts_with
FROM recipe_memcheck_incoming i
CROSS JOIN recipe_memcheck_memories m
GROUP BY i.id, i.source, i.fact
ORDER BY i.id;
| id |
source |
verdict |
conflicts_with |
| 101 |
chat |
LIE |
The customer prefers to be contacted by email, never by phone. |
| 102 |
crm-sync |
TRUTH |
|
| 103 |
webinar-tool |
LIE |
The customer manages procurement for a mid-size logistics company. |
| 104 |
scraper |
LIE |
The customer's office is in Denver. |
| 105 |
agent-b |
LIE |
The customer is on the Aurora plan, which includes a 14-day return window. |
| 106 |
chat |
LIE |
The customer manages procurement for a mid-size logistics company. |
Five of six facts are rejected, including the webinar and the cello, which conflict with nothing. NLI_VERIFY gives contradiction precedence, so one unrelated memory that scores as a "contradiction" vetoes the whole fact. A memory that rejects everything new can't learn anything.
Step 5: The fix: one gated query for the whole batch
Add the similarity floor, and use a LEFT JOIN so a fact with no related memory comes back as NEW instead of dropping out:
SELECT i.id, i.source,
CASE g.verdict
WHEN 'LIE' THEN 'CONFLICT'
WHEN 'TRUTH' THEN 'ALREADY_KNOWN'
ELSE 'NEW'
END AS status,
g.conflicts_with
FROM recipe_memcheck_incoming i
LEFT JOIN (
SELECT c.id AS cand_id,
JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default', c.fact, NLI_ANCHORS(m.fact)),
'$.verdict')) AS verdict,
JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default', c.fact, NLI_ANCHORS(m.fact)),
'$.violated_anchor')) AS conflicts_with
FROM recipe_memcheck_incoming c
CROSS JOIN recipe_memcheck_memories m
WHERE COSINE_SIMILARITY(c.embedding, m.embedding) > 0.45
GROUP BY c.id, c.fact
) g ON g.cand_id = i.id
ORDER BY i.id;
| id |
source |
status |
conflicts_with |
| 101 |
chat |
CONFLICT |
The customer prefers to be contacted by email, never by phone. |
| 102 |
crm-sync |
ALREADY_KNOWN |
|
| 103 |
webinar-tool |
NEW |
|
| 104 |
scraper |
CONFLICT |
The customer's office is in Denver. |
| 105 |
agent-b |
CONFLICT |
The customer is on the Aurora plan, which includes a 14-day return window. |
| 106 |
chat |
NEW |
|
All six land where they should, and each conflict names the memory it collides with. Two details:
- Fact 103 (the webinar) cleared the floor against one memory, "office is in Denver", at similarity 0.487. Its contradiction score there was 0.488, below the 0.6 threshold, so
NLI_VERIFY returned UNVERIFIED_PLAUSIBLE, which maps to NEW.
- Fact 106 (the cello) cleared no memory at all. The
LEFT JOIN returns a NULL verdict, which also maps to NEW.
Step 6: Store what's safe, queue what conflicts
Save the triage once, so the NLI work isn't repeated:
CREATE TABLE IF NOT EXISTS recipe_memcheck_triage (
id INT PRIMARY KEY,
source TEXT,
fact TEXT,
status TEXT,
conflicts_with TEXT
);
INSERT INTO recipe_memcheck_triage (id, source, fact, status, conflicts_with)
SELECT i.id, i.source, i.fact,
CASE g.verdict
WHEN 'LIE' THEN 'CONFLICT'
WHEN 'TRUTH' THEN 'ALREADY_KNOWN'
ELSE 'NEW'
END,
g.conflicts_with
FROM recipe_memcheck_incoming i
LEFT JOIN (
SELECT c.id AS cand_id,
JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default', c.fact, NLI_ANCHORS(m.fact)),
'$.verdict')) AS verdict,
JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default', c.fact, NLI_ANCHORS(m.fact)),
'$.violated_anchor')) AS conflicts_with
FROM recipe_memcheck_incoming c
CROSS JOIN recipe_memcheck_memories m
WHERE COSINE_SIMILARITY(c.embedding, m.embedding) > 0.45
GROUP BY c.id, c.fact
) g ON g.cand_id = i.id;
Only NEW facts become memories. ALREADY_KNOWN would just add a duplicate, so it's skipped:
INSERT INTO recipe_memcheck_memories (id, fact, embedding)
SELECT i.id, i.fact, i.embedding
FROM recipe_memcheck_incoming i
JOIN recipe_memcheck_triage t ON t.id = i.id
WHERE t.status = 'NEW';
That returns rows_affected = 2: the webinar and the cello.
The review queue is the conflicts, each paired with the belief it collides with:
SELECT id, source, fact, conflicts_with
FROM recipe_memcheck_triage
WHERE status = 'CONFLICT'
ORDER BY id;
Three rows: 101 (chat), 104 (scraper), 105 (agent-b).
A conflict isn't always an error
Look at that queue again. Fact 101, "asked us to call them by phone from now on", is probably a real change of preference, not a mistake. Fact 104 comes from a scraper and contradicts your CRM. The model can't tell those apart. All it says is that the two statements can't both be true. What happens next is a trust policy, and it's yours:
| Conflict source |
Typical policy |
| The user, first-hand ("from now on…") |
Supersede: update the old memory, keep a history row |
| A lower-trust writer (scraper, another agent) |
Reject and keep the existing memory |
| Two equally trusted sources |
Hold for a human, and serve neither as fact |
The check's job is to make sure no contradiction gets stored without anyone seeing it. Who wins is your call.
Step 7: The same check on a single fact with MEMORY_RECALL
If your agent uses the MEMORY_STORE / MEMORY_RECALL functions instead of a table, apply the same floor to recall's similarity column. Load the four memories into a namespace:
SELECT MEMORY_STORE('recipe_memcheck_ns', fact) AS memory_id
FROM recipe_memcheck_memories
WHERE id <= 4;
Then check the phone fact against whatever recall returns above 0.45:
SELECT JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default',
'The customer asked us to call them by phone from now on.',
NLI_ANCHORS(content)), '$.verdict')) AS verdict
FROM MEMORY_RECALL('recipe_memcheck_ns',
'The customer asked us to call them by phone from now on.', 5)
WHERE similarity > 0.45;
It returns LIE: a conflict, so don't call MEMORY_STORE. Recall gave the same similarity values as Step 3 (0.502 for fact 101 against memory 1), because it's the same embedding model.
Count before you verify. NLI_VERIFY over an empty set of anchors raises an error instead of guessing:
SELECT COUNT(*) AS related
FROM MEMORY_RECALL('recipe_memcheck_ns',
'The customer is learning to play the cello.', 5)
WHERE similarity > 0.45;
It returns 0. Nothing related is stored, so the cello fact is NEW and safe to store. MEMORY_RECALL takes its query as a string literal, so this path checks one fact per statement. For a batch in one query, use the table form in Step 5.
Can it replace old memories automatically?
Everything above flags conflicts so a person or a policy can decide. If you want a trusted update to replace the old memory with nobody in the loop, the settings have to be much tighter. Deleting a good memory is worse than keeping a stale one.
| Setting |
Flag a conflict |
Replace automatically |
MEMORY_RECALL similarity |
> 0.45 |
> 0.60 |
| NLI contradiction |
> 0.6 (NLI_VERIFY default) |
> 0.95 |
| Memories replaced |
none, flagged for a decision |
only the single closest memory |
Replace only the closest memory, never every memory that passes. One broad fact ("the customer downgraded to the Free plan") can look like a contradiction of several unrelated memories about the same customer.
This statement finds the memory to replace (at most one row), stores the new fact with a supersedes pointer so the history is kept, and forgets the old memory:
SELECT MEMORY_STORE('recipe_memcheck_ns', 'The customer''s office is in Chicago.',
json_object('supersedes', v.id)) AS new_id,
MEMORY_FORGET('recipe_memcheck_ns', v.id) AS forgotten
FROM (
SELECT id, similarity FROM (
SELECT id, similarity,
CAST(JSON_EXTRACT(NLI_CLASSIFY('nli-default', content,
'The customer''s office is in Chicago.'),
'$.scores.contradiction') AS DOUBLE) AS con
FROM MEMORY_RECALL('recipe_memcheck_ns', 'The customer''s office is in Chicago.', 20)
WHERE similarity > 0.60
) t
WHERE con > 0.95
ORDER BY similarity DESC
LIMIT 1
) v;
The inner query picks the Denver memory (similarity 0.740, contradiction 0.999), and the statement returns one row with forgotten = true. If the inner query returns no row, nothing is replaced, and the fact goes through the review path instead.
How the two settings compare. We tuned the tighter settings on 34 cases, then ran both settings unattended on 26 held-out cases that weren't used for tuning:
| Settings, applied unattended |
Good memories wrongly overwritten |
Real updates caught |
| Tighter (0.60 / 0.95, closest memory only) |
0 of 26 cases |
11 of 16 |
| Flagging (0.45 / 0.6, every match) |
6 of 26 cases |
14 of 16 |
That's the trade-off in one table. The flagging settings catch more real updates, and a false flag only costs someone a glance at a queue. Used to delete memories, those same settings overwrote good facts in 6 of 26 cases. The tighter settings overwrote nothing, but they missed 5 of 16 real updates, which then sit next to the old memory until someone reviews them.
About that data. It's 60 short synthetic cases that we wrote. Re-check thresholds on your own memories whenever you change the embedding model, the NLI model or the engine version.
If you use MEMORY_UPSERT with 'policy', 'replace', note that it decides on similarity alone, with no NLI check, so it doesn't replace the gate above.
Clean up
DROP TABLE IF EXISTS recipe_memcheck_memories;
DROP TABLE IF EXISTS recipe_memcheck_incoming;
DROP TABLE IF EXISTS recipe_memcheck_triage;
SELECT MEMORY_FORGET('recipe_memcheck_ns', id) AS forgotten
FROM MEMORY_RECALL('recipe_memcheck_ns', 'customer', 10);
Try it on your own memories
Install the free Community Edition, enable nli-default, and run Step 3 against a sample of your agent's real memories. That one query tells you whether the similarity gap exists in your data and where to put the floor.
curl -fsSL https://get.synapcores.com | SYNAPCORES_REF=blog-nli-memory sh
Or download SynapCores for macOS, Linux or Docker. The full recipe, with every step, is AI Agent Memory: Catch Contradictions on Write, in One SQL Query. For the bigger picture on why similarity search alone isn't agent memory, see AI Agent Memory: Why a Vector Database Isn't Enough.