Prompt
How do I add memory to an AI agent without making it flaky?
Latest observation
To add memory to an AI agent without making it flaky, the key is: don’t treat memory as “everything the agent has ever seen.” Treat it as a managed system with rules for what gets stored, how it’s retrieved, and when it’s trusted.
Here’s the practical approach.
1) Split memory into types
Use different memories for different jobs:
- Short-term / working memory: the current conversation, task state, constraints
- Long-term factual memory: stable user preferences, profile info, durable facts
- Episodic memory: past interactions, decisions, outcomes
- Task memory: intermediate artifacts, plans, TODOs, tool outputs
Flakiness often comes from mixing these together.
2) Make memory retrieval selective
Don’t dump lots of old context into the prompt.
Instead:
- Retrieve only memory relevant to the current query
- Rank by recency, similarity, and importance
- Cap the number of memories injected
- Prefer a few high-signal memories over many weak ones
If retrieval is noisy, the model will become inconsistent.
3) Store only high-confidence memory
Before writing something to memory, ask:
- Is this stable?
- Is it actually useful later?
- Is it likely to change?
- Did the user explicitly state it?
Good things to store:
- “My name is Sam”
- “I prefer concise answers”
- “Use metric units”
- “We’re working on project X”
Bad things to store:
- Transient mood
- One-off details
- Model guesses
- Anything uncertain
If you store guesses, the agent will confidently act on them later.
4) Add provenance and timestamps
Every memory item should carry metadata like:
- source
- timestamp
- confidence
- type
- expiration / TTL
- last validated
This lets the system decide whether a memory should be trusted.
Example:
{
"key": "user_prefers_concise_answers",
"value": true,
"confidence": 0.98,
"source": "user_explicit",
"timestamp": "2026-10-09T12:00:00Z",
"ttl_days": 365
}
5) Prefer “read memory” and “write memory” policies
Separate:
- what can be remembered
- what should be recalled
- what should be updated/deleted
This can be rule-based or model-assisted, but it should be explicit.
Examples:
- Only write preferences if user states them clearly
- Never overwrite a high-confidence memory without confirmation
- If two memories conflict, ask the user or use the latest verified one
6) Use validation on recall
When memory is retrieved, don’t assume it’s true.
Validate it against:
- recent conversation
- current task state
- tool results
- source quality
If the memory conflicts with current evidence, either:
- ignore it
- downgrade it
- ask the user for confirmation
7) Keep memory small and structured
Unstructured notes become garbage quickly.
Better:
- structured fields
- normalized keys
- fixed schemas
- named entities
- embeddings only as a retrieval layer, not the source of truth
Example fields:
- preferences
- identity
- ongoing projects
- constraints
- recurring tasks
8) Add forgetting
Forgetting is a feature, not a bug.
Use:
- TTLs for temporary memories
- decay for low-use memories
- explicit deletion on user request
- archival of old episodic data
A memory system that never forgets gets polluted and unreliable.
9) Separate “assistant memory” from “user profile”
A common failure mode is letting casual conversation alter core user profile.
Better:
- user profile = explicit, stable, user-approved
- session notes = temporary, per-conversation
- inferred beliefs = low trust until confirmed
This prevents the agent from drifting.
10) Instrument and test memory behavior
Track:
- false recalls
- stale recalls
- irrelevant recalls
- memory overwrite rate
- user correction rate
- task success with/without memory
Then run tests like:
- user changes preference
- conflicting memories exist
- memory is partially missing
- irrelevant but semantically similar memory exists
If you don’t test memory, it will seem “smart” in demos and flaky in production.
A good pattern
A robust memory pipeline often looks like this:
- Observe user interaction
- Extract candidate memories
- Score them for importance, confidence, stability
- Store only approved memories
- Retrieve selectively based on current task
- Validate retrieved memories
- Update or forget as needed
Rule of thumb
If a memory can:
- be wrong,
- become stale,
- conflict with other memory,
- or mislead the agent,
then it needs metadata, gating, and validation.
If you want the simplest safe design
Start with this:
- memory only for explicit user preferences and stable profile facts
- retrieval only when strongly relevant
- every memory has timestamp + source + confidence
- user can inspect/edit/delete memory
- defaults favor forgetting over guessing
If you want, I can also give you:
- a concrete memory architecture for an agent,
- a schema for memory items, or
- a prompt/tool design that reduces flaky recall.