How to Stop a Prompt Injection Spreading Between AI Agents and Their Memory

To stop a prompt injection from spreading between AI agents and their memory, treat every memory write as a security event. Log which agent wrote each entry and what content was in its context at the time. Keep entries that came from untrusted content apart from entries a person approved, and give every entry an expiry date. When one turns out to be poisoned, you need to find every run that read it and every entry those runs wrote. Most hardening guides end before that point.
Injection attempts now show up in measurements of the public web. On 23 April 2026 Google published a scan of the public web using Common Crawl, which provides monthly snapshots of 2 to 3 billion pages. Its sentence: “We saw a relative increase of 32% in the malicious category between November 2025 and February 2026.” The scan skips login walls and most social media, and Google says it “is not exhaustive”. The malicious finds it describes aim at data theft and at vandalising the machine of anyone using an AI assistant.
The standard answer is a sandbox. That answer is right, and we wrote it up ourselves in how to sandbox an AI agent. In our view it covers half of the problem. A sandbox bounds one run. This guide is about the injection that is written down during a run and read again next week.
Why does the usual hardening stop at the sandbox?
Because the defences that try to win inside the prompt keep losing. In “The Attacker Moves Second”, Milad Nasr and colleagues bypassed 12 recent defences “with attack success rate above 90% for most”, although the majority of them had originally reported near-zero success rates. Their human red-teaming competition drew over 500 participants, and the authors write: “human red-teaming succeeds in all the cases we evaluated”. We covered the practical consequence in why prompt injection cannot be patched.
The conclusion most guides draw is the one Sophos states: contain the blast radius with controls that operate outside the model, namely “process boundaries, credential isolation, egress filtering, human gates, and comprehensive audit”. All of those act on a run that is happening. When the run ends, the container is thrown away and the injection with it. Unless the agent wrote something down.
Sophos is blunt about that case. “A poisoned memory entry is a backdoor that loads every session.” It also admits what is missing: “Memory poisoning detection is heuristic. No reliable automated solution exists,” and “Human review of memory writes is the current state of the art.” That is the unsolved part. You will probably not stop every injection from landing. What you can do is make sure it leaves a trail, and that you can remove it.
How far does an injection reach? Four levels
An injection reaches as far as the agent’s own writes carry it. Sophos describes four levels: session context, persistent memory, direct agent messages and shared state. The table uses the same four. An injection at level 0 “lives only in the current context window and dies when the session ends”, while one at level 1 “writes a poisoned entry to persistent memory. Survives session restarts, fires on every load.” The last column, what bounds each level, is our view.
| Level | Lives until | Who reads it next | What bounds it |
|---|---|---|---|
| 0. Session context | The session ends | The same run | Sandbox, budgets, human approval on writes |
| 1. Persistent memory | Someone deletes it | The same agent, every later session | Write gate, origin tag, expiry, write log |
| 2. Messages between agents | The receiver acts on it or stores it | The receiving agent, which trusts the sender | Treat the body as untrusted data, separate identities |
| 3. Shared state: shared notes, vector index, ticket board | Someone cleans it | Every agent that reads it, in any run | Per-agent write scope, versioning, read log |
OWASP lists two of the levels as risks of their own. Its Top 10 for Agentic Applications, released on 9 December 2025, has ASI06 Memory & Context Poisoning and ASI07 Insecure Inter-Agent Communication as entries of their own. OWASP’s example for the first: “Memory poisoning reshaped behaviour long after the initial interaction”.
The dangerous path, in our view, runs through levels 0, 1 and 3 in sequence. An agent reads a web page during a session. It summarises the page into memory so it can “remember” it. A second agent loads that summary a week later as a plain fact. Every hop strips the origin: the summary does not say it came from a page nobody vetted. By the time the second agent acts, nothing in its context marks the text as untrusted. Messages between agents behave the same way. The receiver gives the message the authority of the sender, and the sender may only have been quoting an attacker.
What should you log? Every memory write, and every read
You cannot clean what you cannot find. Sophos says: “Treat memory writes as security events and log them.” ISACA’s white paper on securing AI agents of 15 September 2026 asks you to log “prompts, responses, retrieved sources, content hashes, tool invocations” among other items, and says: “Use centralized, tamper-resistant logging.” For memory, that turns into one record per write and one per read, kept where the agent cannot edit it:
{
"event": "memory_write",
"entry_id": "m-20261122-0417",
"time": "2026-11-22T09:14:03Z",
"writer": "agent:research-2",
"run_id": "run-8841",
"context_sources": [
{"type": "web", "ref": "https://example.org/page", "trust": "untrusted"},
{"type": "user", "ref": "ticket-5521", "trust": "trusted"}
],
"content_sha256": "9f2c...e1",
"approved_by": null,
"expires": "2026-12-22"
}
{
"event": "memory_read",
"entry_id": "m-20261122-0417",
"reader": "agent:planner-1",
"run_id": "run-9120"
}The field that matters most is context_sources. It records what the agent had read when it wrote, so a write made after reading an untrusted page is flagged from the start. The read events are what make cleanup possible, because they tell you which later runs were exposed. ISACA also asks for redaction where needed. Memory often holds personal data, so decide before you log what gets masked.
How do you contain and clean a poisoned memory?
Containing a poisoned memory takes seven steps: freeze writes, find the entry, trace forward and backward, quarantine, restore, revoke and close the route in. The order matters, because cleaning before you have frozen writes lets the same agent write the entry again.
1. Freeze writes
Switch the affected agents to read-only memory. ISACA names the move: “disabling tools or shifting the agent to read-only mode”. Do it per agent and per store, not for the whole system, so the unaffected agents keep working.
2. Find the entry
Start from the symptom: a run that did something odd. Take its run ID, list every memory entry it loaded from the read log, and look for entries whose context_sources include untrusted content or whose text tells the agent what to do. A fact describes the world. An entry that gives the agent a standing instruction is the one to read first.
3. Trace forward and backward
Forward: list every run that read the entry, then every entry those runs wrote. Those entries are tainted until shown otherwise, and they form your cleanup set. Backward: open the source that was in context at the first write, so you know which page, document or message to block.
4. Quarantine, do not delete
Move the tainted entries to a quarantine table that no agent can read, with their log records. You want the text for the post-mortem, for your detection rules and for the test in step 7.
5. Restore from a clean point
Roll the store back to the last snapshot before the first tainted write, then replay the clean writes from the log. That needs versioned memory, and it is the best reason to add it before an incident. For a vector index, rebuild from the source documents rather than editing single vectors.
6. Revoke what the exposed runs could do
A tainted run may have acted before anyone noticed. Check the write actions of every exposed run, revoke the tokens issued to them, and rotate any secret an exposed agent could reach. The same logic applies as in the kill-switch step of the sandbox guide.
7. Close the route in
Mark the source as untrusted for memory writes, add the quarantined entry to a regression test, and run it. If a similar entry can still get through the gate, the gate is the next thing to fix. Detection stays manual for now, so keep a human in the loop for entries that change how an agent behaves.
What can a builder check this week?
Seven checks, none of which needs new products. Each takes hours, not weeks.
- List every place one of your agents can write that another run later reads: memory files, a vector store, a notes folder, ticket comments, a shared channel. The list is nearly always longer than the team expects.
- Add the write log and read log above to each of them. For a file-based memory that is one wrapper function.
- Tag each entry with its origin: typed by a user, produced by a tool, taken from the web, sent by another agent. Load web-origin and agent-origin entries into the prompt as quoted data, never as instructions.
- Give entries an expiry. ISACA: “Apply retention limits and time to live (TTL) values to persistent memory.” We start at 30 days for anything a person has not confirmed again.
- Put a human gate on entries that change behaviour: standing instructions, preferences, contacts, URLs, the location of credentials. Facts about your own data can pass without one.
- Separate memory by agent identity and use case. ISACA: “Isolate memory and context by tenant, user, and use case.” One shared notebook for every agent is level 3 in its worst form.
- Run a restore drill. Pick one entry at random, find every run that read it, roll back, and time the result. If you cannot do it in an afternoon, you cannot do it during an incident.
How is this different from our other guides?
The prompt-injection success-rate post measures how often an attack hijacks a browser agent in one session. The RAG security guide covers poisoned documents in a retrieval index you control. The write-action allowlist limits what an agent may do on the web. This guide starts after all three: the injection has landed once and was stored, and the question is who reads it next. If you run several agents, the orchestration guide covers the persistent memory this guide asks you to log, and the post on non-human identity covers the separate identities that make a write log meaningful.
Checklist
- Every shared write location listed and owned by one person
- Write log with context sources, writer, run ID, content hash; read log with reader and run ID; both outside the agent’s reach
- Origin tag on every entry; untrusted origins loaded as quoted data only
- Expiry on every entry; human gate on behaviour-changing entries
- Memory separated per agent identity and use case; versioned for rollback
- One-command read-only switch per agent and per store
- Restore drill done once, timed, written down
Related reading
- How to Sandbox an AI Agent: Network, Credentials, Kill Switch
- Prompt Injection Cannot Be Patched. Design Around It.
- Securing RAG Pipelines: Prompt Injection & Access Controls
- Prompt Injection Now Has a Number: 31.5% Agent Hijack
Sources: Google, 23 April 2026, Nasr et al., arXiv 2510.09023, Sophos (undated page), ISACA, 15 September 2026 and the OWASP Top 10 for Agentic Applications.
Frequently Asked Questions
How does a prompt injection spread between AI agents?
Through whatever one agent writes and another reads. An agent reads untrusted content, stores a summary in persistent memory, shared notes or a message, and a second agent loads that text later as a plain fact. Each hop drops the origin of the text, so nothing marks it as untrusted when the second agent acts. Sophos describes the memory case as a backdoor that loads every session.
What should you log to find a poisoned AI agent memory?
One record per memory write and one per read, stored where the agent cannot edit them. A write record holds the entry ID, time, writing agent, run ID, the sources in the agent's context at that moment with a trust label, a content hash, who approved it and an expiry date. A read record holds the entry ID, the reading agent and its run ID. The read log tells you which later runs were exposed.
How do you clean a poisoned AI agent memory?
Freeze memory writes for the affected agents, find the entry from the symptom run, trace every run that read it and every entry those runs wrote, move them to a quarantine store, restore from a snapshot taken before the first tainted write, revoke the tokens of the exposed runs and add the entry to a regression test. Sophos says detection is still heuristic and human review of memory writes is the state of the art.