Cross-Session Agentic Memory with ADD, UPDATE, DELETE, and SKIP
Learn how to build cross-session memory for AI agents using JSON-backed facts and rules. You’ll reconcile new statements with existing records, apply ADD, UPDATE, DELETE, or SKIP safely, load memory into future prompts, and test each outcome while reducing stale, duplicate, and conflicting information.
We'll cover the following...
The context management techniques we've implemented so far operate within a single session. Pruned tool results, compacted history, structured notes, and retained errors are not automatically carried over to new sessions.
When a new incident begins, the assistant starts a new session without access to the previous session's state. Certain preferences and findings should persist across sessions. An engineer tells the assistant, “Always give me numbered steps, each with its citation.” The assistant should retain this preference across future sessions. Similarly, if an incident review establishes that v2.31 increased the tax-service timeout to 2.0 seconds, that finding remains relevant to future incidents until the configuration changes.
In this lesson, we'll implement persistent memory across sessions. We'll examine why an append-only memory store can accumulate duplicate, outdated, and conflicting information. We'll then implement a memory store that reconciles incoming statements with existing records. New records are added, changed records are updated, withdrawn records are deleted, and unchanged records are left intact. We'll load persisted memory at the start of each new session and verify that each reconciliation operation produces the expected file state.
Why appending fails
A simple way to implement persistent memory is with an append-only log. Whenever the assistant identifies information worth retaining, it appends a new entry without modifying or removing existing records. Let's examine what happens when previously stored information changes.
In session 1, the incident review establishes that v2.31 increased the tax-service time-out to 2.0 seconds. In session 2, a fix is deployed, restoring the timeout to 0.5 seconds.
In the code above:
Lines 6–8: We define
naive_append(), which adds a line to the log without checking what’s already there.Lines 11–17: We define
naive_lookup(), which returns the first entry it finds for a key.Lines 21–26: We record the timeout twice, then print every entry for the key and what a lookup returns.
The log now holds two ...
entries for the key: ['2.0', '0.5']lookup returns: 2.0