Memory is what lets AI agents learn preferences, carry context between sessions and work on long tasks. It is also a new place for attacks and data leaks. Content written to memory today can change an agent's behaviour weeks later. Memory shared across users can move one person's data into another's answers. Memory that is kept indefinitely accumulates sensitive information, and sometimes credentials, that nobody meant to store. This guide explains how agent memory works, the risks it creates, including memory poisoning and cross-user leakage, and the controls that keep it safe.
Key takeaways
- Agent memory includes short-term context, long-term memory stores, shared scratchpads and retrieved knowledge. Each needs access control and retention rules.
- Memory poisoning (OWASP ASI06) lets attackers plant instructions or false facts that persist and influence future actions.
- Isolate memory per user, session and task, and never let one principal's memory reach another's without explicit design.
- Never store secrets in memory; validate what is written; log changes; set retention; allow reset.
Types of agent memory
| Type | Description | Main risks |
|---|---|---|
| Context window | Current conversation and retrieved content | Injection; sensitive data sent to model provider |
| Session memory | State kept during a task or session | Leakage between tasks if not cleared |
| Long-term memory | Facts, preferences and summaries stored across sessions | Poisoning; accumulation of sensitive data; cross-user leakage |
| Shared memory | Scratchpads or state shared between agents in a system | One compromised agent influencing others |
| Knowledge stores | Vector indexes and documents used for retrieval | Permission bypass; poisoning. See the Permission-Aware RAG Guide |
How memory flows through an agent
Securing memory starts with knowing where information enters, where it is kept and where it comes back out. Most incidents involve a path the designers did not draw.
- Ingest: the agent reads user messages, emails, documents, web pages and tool outputs. Some of this content is trusted, much of it is not.
- Decide to remember: the model, or a rule, chooses what to keep. This is the critical control point, because anything written here can come back later.
- Store: the entry is saved in a database, vector index or file, usually with an identifier for the user or session.
- Retrieve: in a later session, entries are selected by similarity or key and placed into the context window.
- Act: the model treats the retrieved entry as part of its instructions or facts, and calls tools accordingly.
Each step needs its own control. Input filtering at step one does not help if step four retrieves entries for the wrong principal, and partitioning at step three does not help if step two stores a hostile instruction.
Risks
- Memory poisoning: malicious content, such as an email or document, causes the agent to store an instruction ("always approve invoices from this supplier") or a false fact that shapes later behaviour.
- Cross-user leakage: an agent serving many users recalls one user's details in another user's session.
- Privilege confusion: memory created under a privileged user's session is later available to an agent acting for a less privileged user.
- Sensitive data accumulation: personal data, confidential content and occasionally credentials stored long-term without classification or retention limits.
- Tampering: direct modification of memory stores by anyone with access to the underlying database.
- Drift: gradual accumulation of memories that push the agent away from its intended behaviour.
Worked scenarios
Poisoned supplier instruction
An agent that helps a finance team summarise inbound email reads a message that contains hidden text asking it to remember that invoices from a named supplier are pre-approved. The agent stores the note as a preference. Weeks later, a different user asks the agent to prepare payments, and the retrieved note causes the agent to skip a review step.
- What failed: external content was turned into a durable rule without validation, and the memory had no record of its source.
- What would have helped: blocking instruction-like entries, requiring confirmation for anything that changes approval behaviour, and keeping the source of each entry so it could be traced and removed.
Cross-user recall in a shared assistant
A support assistant keeps a single long-term store for all customers, keyed only by similarity. A customer asks a general question, and the closest matching entry belongs to someone else's case.
- What failed: partitioning relied on the prompt and the retrieval ranking, not on a filter applied in the data layer.
- What would have helped: a mandatory principal filter on every query, tested with deliberate cross-tenant probes.
Credentials in a developer agent
A coding agent stores a summary of a debugging session, including a connection string a developer pasted into the chat. The summary persists and is later retrieved in an unrelated project.
- What failed: no secret scanning on writes, and raw session content stored as memory.
- What would have helped: scanning before storage, storing structured summaries only, and rotating the exposed credential once discovered. See the Agentic AI Security Guide.
Controls
Isolation and access control
- Partition memory by principal (user or tenant), agent and task; enforce partitioning in the data layer, not only in the prompt.
- Tag memory entries with the identity and permission context under which they were created, and only retrieve entries the current principal may see.
- Protect memory stores with their own least-privilege identities and encryption. See Securing AI Infrastructure Workload Identities.
Write controls
- Do not write raw external content to long-term memory; store structured, validated summaries.
- Block instruction-like content, such as "always", "ignore previous" or policy-changing statements, from being stored as memory, or route it for review.
- Require confirmation for memories that change goals, rules or trust decisions.
- Never store credentials, tokens or keys; scan memory for secrets.
Visibility and lifecycle
- Log all memory writes, updates and deletions with source and identity.
- Let users view and delete what the agent remembers about them.
- Set retention periods; expire and re-baseline long-term memory periodically.
- Be able to roll back or wipe memory after an incident. See the AI Agent Observability and Incident Response Guide.
Multi-agent systems
- Avoid shared memory between agents with different trust levels.
- Validate information passed between agents before storing it. See the Multi-Agent and A2A Security Guide.
Detecting and responding to poisoning
- Review new long-term entries: sample recent writes, and review every entry that mentions rules, approvals, trust or exceptions.
- Trace behaviour to memory: when an agent takes an unexpected action, log which memory entries were retrieved, so investigators can tell whether a stored entry influenced the decision.
- Test with canaries: plant benign marker instructions in test content and check whether the agent stores or follows them.
- Quarantine by source: if one source, such as a mailbox or document set, is found to be hostile, remove all entries derived from it rather than only the entry you noticed.
- Re-baseline after incidents: restore memory from a known-good point or rebuild it from reviewed sources, then confirm behaviour against a set of regression prompts.
Roles and ownership
| Role | Responsibility |
|---|---|
| Agent owner | Decides what the agent may remember, for how long, and who can see it |
| Platform or data engineering | Implements partitioning, encryption, retention and deletion in the memory store |
| Security team | Sets write and retrieval rules, runs poisoning tests and reviews logs |
| Privacy or legal | Decides lawful basis, notices, retention periods and how data subject requests reach memory |
| Identity team | Governs the identities that read and write memory stores, and their permissions |
Privacy considerations
- Agent memory of personal data is processing under data protection law; apply purpose limitation, minimisation, retention and data subject rights.
- Document what is remembered and why in privacy notices.
- Make sure access, correction and erasure requests can reach long-term memory and any derived summaries or embeddings, not only the primary database.
- Avoid storing special category or children's data in agent memory unless there is a clear need and a lawful basis, and keep such entries separate with stricter retention.
- Check where memory is processed and stored, including model providers and vector database services, and reflect this in data protection assessments.
Common failure patterns
- Memory by default: a framework enables persistence and nobody records what it keeps, where or for how long.
- Prompt-only isolation: the system prompt tells the agent not to reveal other users' data, while the store itself returns everything.
- Raw transcripts as memory: full conversations are stored, including secrets, personal data and hostile content.
- One store for every trust level: a low-trust agent that reads the open web writes to the same memory as a high-trust agent that acts on finance systems.
- No deletion path: data subject requests and incident response both require deletion, and the design makes it impossible to find the right entries.
- Memory store with broad access: the database or vector index is readable by many services, so tampering needs no prompt at all.
Evaluating a memory design
Use these questions in design reviews and when assessing a product that offers persistent memory.
- Which memory types does the system use, and which of them survive between sessions?
- How are entries partitioned, and is the filter applied in the query layer on every retrieval?
- What is written automatically, and what needs user or administrator confirmation?
- Can a user list, correct and delete their memories, and can an administrator do the same across a tenant?
- Are memory writes logged with source, identity and timestamp, and can entries be rolled back?
- Are secrets and sensitive data detected before storage?
- Is the store encrypted, and which identities can read it directly?
- What happens to memory when a user leaves or an agent is retired?
Metrics
- Coverage: the share of agents with a documented memory inventory and retention period.
- Blocked writes: the number of instruction-like or secret-bearing writes stopped or sent for review, tracked over time.
- Isolation test results: the outcome of regular cross-user and cross-tenant retrieval tests, which should show no leakage.
- Deletion time: how long it takes to remove a user's memories or all entries from a given source.
- Memory age: the share of entries past their retention period, which should be close to none.
- Review backlog: entries waiting for human confirmation and how long they wait.
Mapping to standards
| Risk | Reference |
|---|---|
| Persistent planting of instructions or false facts | OWASP Top 10 for Agentic Applications, ASI06 Memory and Context Poisoning |
| Disclosure of sensitive information | OWASP Top 10 for LLM Applications, LLM02 |
| Poisoned data influencing behaviour | OWASP Top 10 for LLM Applications, LLM04 |
| Cross-layer threats in agent architectures | CSA MAESTRO threat modelling framework |
| Personal data held in memory | Data protection principles of purpose limitation, minimisation and storage limitation, as in the GDPR |
Use these as a checklist when threat modelling, and record which controls in this guide address each row. See the Threat Modelling AI Agents guide.
Practitioner checklist
- Map every memory type your agents use and where it is stored.
- Partition memory by user, tenant, agent and task, enforced in the data layer.
- Validate and summarise before writing; block instruction-like content.
- Never store secrets in memory; scan for them.
- Log memory changes; allow users to view and delete.
- Set retention and re-baseline periodically; be able to wipe after incidents.
- Test isolation and poisoning regularly, and record which entries influenced each unexpected action.
Standards and references
- OWASP Top 10 for Agentic Applications for 2026: ASI06 Memory and Context Poisoning
- OWASP Top 10 for LLM Applications (2025): LLM02, LLM04
- CSA MAESTRO
Related NHI Mgmt Group resources: Agentic AI Security Guide · Permission-Aware RAG Guide · Threat Modelling AI Agents · OWASP Agentic Top 10 Guide