Search⌘ K
AI Features

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.

naive_memory.py · Python 3.14.0
import json
LOG = "naive_memory.jsonl"
def naive_append(key, value):
with open(LOG, "a") as f: # never checks what is already stored
f.write(json.dumps({"key": key, "value": value}) + "\n")
def naive_lookup(key):
with open(LOG) as f:
for line in f:
entry = json.loads(line)
if entry["key"] == key:
return entry["value"] # the first match wins, so later corrections are never seen
return None
if __name__ == "__main__":
open(LOG, "w").close()
naive_append("tax_service_timeout_seconds", "2.0") # session 1: v2.31 raised the timeout
naive_append("tax_service_timeout_seconds", "0.5") # session 2: the fix restored it
entries = [json.loads(line) for line in open(LOG)]
print("entries for the key:", [e["value"] for e in entries])
print("lookup returns: ", naive_lookup("tax_service_timeout_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
The corrected value is stored, but the lookup still returns the stale one
...