Conditional Memory: State Persistence for Reproducible AI Systems
This article explains conditional memory, a state persistence approach that controls when AI systems write, recall, and expire information across turns, sessions, and tasks. It covers a three-layer memory model, a five-step write pipeline, condition-first recall, conflict resolution and forgetting mechanisms, observability and governance, and common anti-patterns that reduce long-term reliability.
1. Why Conditional Memory Matters
In most AI systems, the hard part is not a single inference call. It is maintaining state across turns, sessions, and tasks. Without state continuity, the system has to rebuild context from scratch every time. With state continuity but no constraints, it carries stale information, incorrect inferences, and contradictory conclusions into later decisions, creating a feedback loop where the system becomes less reliable the more it is used.
Conditional memory addresses one core question: under what conditions should information be written, recalled, or invalidated? It turns memory from an endlessly appended log into a trusted state set with explicit preconditions. Here, “conditions” cover at least four dimensions:
- Trigger conditions: which events or thresholds allow a write;
- Scope conditions: which tasks, scenarios, or user ranges a memory applies to;
- Conflict conditions: which source wins when new information conflicts with existing memory;
- Expiration conditions: when an entry expires, needs review, or must be deleted.
2. A Layered Model for Conditional Memory
Memory should be organized into three layers so that temporary context is not mixed with long-term conclusions.
- Working memory: short-term context for the current task, bounded by a session or task, optimized for low-latency reads and writes.
- Episodic memory: facts and operation traces of what happened, such as confirmed user preferences, completed steps, and failure reasons, optimized for traceability.
- Semantic memory: abstracted, stable conclusions such as domain rules, project conventions, and long-term preferences, optimized for low redundancy and high confidence.
Write thresholds should increase from layer to layer: working memory has almost no threshold; episodic memory requires an explicit event; semantic memory requires repeated corroboration or explicit confirmation. This significantly reduces errors such as treating a one-off behavior as a long-term preference.
3. Write Pipeline: From Events to Memory Entries
A practical write pipeline usually has five steps:
- Event extraction: extract candidate facts from conversations, operation logs, or tool responses;
- Condition evaluation: check whether trigger and scope conditions are met;
- Normalization: unify entities, timestamps, units, and terminology to remove ambiguity;
- Conflict detection: compare against existing entries and decide whether to add, overwrite, or merge;
- Persistence and indexing: store the entry and build a searchable index.
Example data structure (platform-agnostic):
{"id": "mem_001","layer": "semantic","subject": "deploy_preference","predicate": "prefer","object": "blue_green","conditions": {"scope": ["project:checkout"],"valid_from": "2025-01-10T00:00:00Z","ttl_days": 180},"confidence": 0.82,"evidence": ["evt_123", "evt_456"],"status": "active"}
4. Recall Pipeline: Filter First, Then Rank, Then Constrain
Recall should not be a plain similarity search. It should prioritize condition filtering:
- Hard filtering: exclude inapplicable entries by scope, status, and time validity;
- Relevance ranking: combine semantic similarity with recency and confidence weighting;
- Budget control: cap the number of injected entries and token size to avoid context bloat;
- Constraint injection: present memory as premises or constraints rather than absolute facts.
Example pseudocode:
def recall(query, ctx, budget=8):cands = store.filter(status="active",scope=ctx.scope,valid=now_between(ctx.now))cands = rank(cands, query, weight={"sim":0.6,"recency":0.25,"conf":0.15})return format_as_constraints(cands[:budget])
5. Conflict Resolution and Forgetting
For conflict resolution, combine explicit priority with recency: explicit confirmation beats inference; newer evidence beats older evidence; high confidence beats low confidence. For conflicts at the same level, keep both entries and mark them for review instead of silently overwriting.
Forgetting is equally important. Three strategies can be used: TTL expiration, access-frequency decay, and invalidation upon refutation. Semantic memory should also support versioning, retaining historical versions for rollback and auditing.
6. Observability and Governance
Conditional memory must be observable; otherwise it cannot be operated. At minimum, record the write source, evaluation basis, recall hits, reasons for ignoring entries, and conflict resolution outcomes. Key metrics include recall accuracy, redundancy rate, share of expired entries, and conflict rate. For governance, define data minimization, permission isolation, and a response path for deletion requests to ensure compliance and control.
7. Common Anti-Patterns
- Writing all raw conversation text into long-term memory, so noise drowns out signal;
- Recalling by similarity only while ignoring scope conditions, causing cross-project contamination;
- Silently overwriting old conclusions, making it impossible to explain why the system changed its judgment;
- Lacking TTL and review mechanisms, so memory gradually “fossilizes.”
8. Conclusion
The essence of conditional memory is to turn “remembering” into remembering with preconditions, recalling with explanations, and forgetting with the ability to undo. In general-purpose capability sets on cloud platforms such as Baidu AI Cloud, categories like object storage, message queues, logging services, and monitoring and alerting can respectively handle persistence, event-driven processing, auditing, and observability. Regardless of infrastructure choices, however, conditions, conflicts, and expiration are the three main threads that determine a system’s long-term trustworthiness.
