PromptisePromptise
PlatformSoonEnterprise
DocsGitHub
Talk to usGet started
Promptise - AI Framework LogoPromptise

The full-stack framework for agentic systems. Build with the framework. Run it on the platform. Launch it with our engineers.

pip install promptise

[01] Product

  • Framework
  • Platform (coming soon)
  • Enterprise
  • How it works
  • Contact

[02] Framework

  • The Agent
  • The Interface
  • The Harness

[03] Resources

  • Documentation
  • GitHub
  • Why Promptise
  • Use cases
  • Roadmap
  • Guides
  • Learning Paths
  • Questions

[04] Company

  • About
  • Terms of Service
  • Privacy Policy
  • Cookie Policy
  • Subprocessors

© 2026 Promptise by Manser Ventures. All rights reserved.

hello@promptise.com
← Learning paths> Agentic Engineering with Promptise Foundry
Lesson 54 / 100
54%

Per-User Agent Memory Isolation: Stop Leaks Between Users

Why shared AI agent memory leaks between users, how per-user scopes and role-gated team notes stop it, and a canary test that proves the isolation works.

Intermediate18 minAgent MemoryMulti-TenancyAuthentication and Access Control
All 100 lessons↓
  1. ✓What Is an AI Agent? Definition, How It Works, Examples
  2. ✓How to Build an AI Agent in Python (2026 Guide)
  3. ✓Build a Personal AI Agent: Memory, Reminders, Chat
  4. ✓What Is an Agent Harness? Parts and How to Build One
  5. ✓System Prompt Best Practices for AI Agents in Python
  6. ✓Best LLM for AI Agents (2026): Picks, Prices, How to Test
  7. ✓AI Agent YAML: Define and Run an Agent From a Config File
  8. ✓Streaming LLM Responses with FastAPI: Tokens and Tool Calls
  9. ✓Best Local LLM for AI Agents: Ollama Models by GPU (2026)
  10. ✓Claude AI Agent with MCP: Use Anthropic Models in Promptise
  11. ✓Gemini AI Agent with MCP: Use Google Models in Promptise
  12. ✓Azure OpenAI Agent with MCP: Use Deployments in Promptise
  13. ✓OpenRouter AI Agent with MCP: Many Models in Promptise
  14. ✓Ollama AI Agent with MCP: Run Local Models in Promptise
  15. ✓LLM Semantic Cache and Model Fallback in Python
  16. ✓What Is MCP (Model Context Protocol)? A Developer's Guide
  17. ✓What Is Tool Calling? LLM Function Calling Explained
  18. ✓MCP Tools vs Resources vs Prompts (With Python Examples)
  19. ✓How to Connect MCP Servers to Your AI Agent in Python
  20. ✓Build a Production MCP Server in Python (Step by Step)
  21. ✓MCP Transports: stdio vs Streamable HTTP vs SSE (Python)
  22. ✓MCP 2026-07-28 Migration: Make Your MCP Server Stateless
  23. ✓OpenAPI to MCP: Turn Any REST API into an MCP Server
  24. ✓MCP Authentication in Python: API Keys, JWT and OAuth
  25. ✓Build an Authenticated MCP Server for Your AI Agent (Python)
  26. ✓MCP Security Best Practices: A Checklist for MCP Servers
  27. ✓Testing MCP Servers in Python: pytest Without a Live Server
  28. ✓Too Many MCP Tools? How to Fix LLM Tool Selection in Python
  29. ✓Idempotent Tool Calls: Make AI Agent Retries Safe
  30. ✓AI Agent Tool Failures: Stop Unsafe Workarounds
  31. ✓MCP Server Caching, Timeouts and Circuit Breakers in Python
  32. ✓MCP Middleware for Servers: Auth, Logging, Rate Limits
  33. ✓MCP vs CLI for AI Agents: Tokens, Security, When to Use Each
  34. ✓LangChain vs MCP: Differences and How They Work Together
  35. ✓FastMCP Alternatives for Production MCP Servers (2026)
  36. ✓AI Agent Audit Logging: Tamper-Evident Logs in Python
  37. ✓AI Agent Human in the Loop: Approve Tool Calls in Python
  38. ✓AI Agent Prompt Injection: How to Defend Against It
  39. ✓Hidden Text Prompt Injection: Sanitize Pages for AI Agents
  40. ✓Build a Multi-Agent System in Python (and When Not To)
  41. ✓Context Engineering for AI Agents: Context Window Management
  42. ✓Context Compaction for AI Agents: Trim vs Summarize
  43. ✓How to Deploy an AI Agent with Docker (2026 Guide)
  44. ✓LLM Tool Rate Limiting: Per-Client Limits for MCP Servers
  45. ✓MCP Long-Running Tasks: Background Jobs in Python
  46. ✓MCP Server Monitoring: Prometheus, OpenTelemetry and Logs
  47. ✓Best AI Agent Frameworks (2026): Python Options Compared
  48. ✓Self-Improving AI Agent: Learn From Tool Failures in Python
  49. ✓AI Agent Identity: Authenticate Agents to MCP Servers
  50. ✓Code-Action Agents in Python: One Program vs 37 Tool Calls
  51. ✓Multi-Tenant AI Agents: Keep Each Customer's Data Apart
  52. ✓What Is Agentic RAG? How It Differs from Classic RAG
  53. ✓AI Agent Memory in Python: Short-Term and Long-Term
  54. 54Per-User Agent Memory Isolation: Stop Leaks Between Users
  55. 55Agentic RAG in Python: Give Your AI Agent a Search Tool
  56. 56AI Agent Reasoning Patterns in Python: From ReAct to Debate
  57. 57Plan-and-Execute Agents in Python: Beyond the ReAct Loop
  58. 58Self-Modifying AI Agent in Python: Open Mode & Limits
  59. 59CrewAI Alternatives: 7 Options and How to Migrate (2026)
  60. 60Migrate from AutoGen: Agent Framework, AG2 and More
  61. 61What Is an LLM Guardrail? Types, Examples and Limits
  62. 62Best LLM Guardrails Tools (2026): 9 Platforms Compared
  63. 63AI Agent Guardrails in Python: Block Injection, Redact PII
  64. 64NeMo Guardrails vs Guardrails AI: Which to Choose (2026)
  65. 65AI-Based PII Detection and Redaction Tools for LLM Apps
  66. 66Stop Secrets Leaking Into LLM Logs and Traces
  67. 67AI Agent Code Execution Sandbox: Run LLM Code Safely
  68. 68AI Agent Network Egress: Limit Where Agents Can Connect
  69. 69AutoGen Docker Task Isolation: Secure the Code Executor
  70. 70SPIFFE for AI Agents: Workload Identity Without API Keys
  71. 71On-Behalf-Of Auth for AI Agents: Act as Each User
  72. 72AI Agent Production Checklist: What to Check Before Launch
  73. 73Approval Fatigue: Auto-Approve Safe AI Agent Actions
  74. 74What Is Human in the Loop AI? Approvals for AI Agents
  75. 75AI Agent Notifications: Webhook and Slack Alerts in Python
  76. 76AI Agent Observability: Trace Tool Calls, Tokens and Cost
  77. 77Best AI Agent Observability Tools (2026), Compared
  78. 78AI Agent Stuck in a Loop? How to Detect and Stop It
  79. 79How to Run, Debug and Deploy an AI Agent in Python
  80. 80AI Agent Cost Tracking: Tokens and Cost per Run and User
  81. 81How to Evaluate AI Agents: Evals, LLM Judges and pass^k
  82. 82AI Agent Cost Control: Budgets That Stop Runaway Spend
  83. 83How to Test LLM Prompts and AI Agents in Python
  84. 84AI Agent Evals in CI: Run Them on Every Pull Request
  85. 85What Is an Agentic Runtime? AI Agent Runtimes Explained
  86. 86Autonomous AI Agent Runtime: Budgets, Crash Recovery, API
  87. 87Event-Driven AI Agents: Webhook, Cron and File Triggers
  88. 88AI Agent Cron Jobs: Run an Agent on a Schedule
  89. 89AI Agent Webhook Trigger: Run an Agent on HTTP Events
  90. 90AI Agent File Watcher: Process New Files Automatically
  91. 91Custom AI Agent Triggers: Wake an Agent From Any Source
  92. 92AI Agent Event Filters: Skip Runs Before the LLM
  93. 93AI Agent Retries and a Dead Letter Queue for Failed Runs
  94. 94Run Multiple AI Agents: One Runtime, Many Processes
  95. 95AI Agent Manifest: Declare a Long-Running Agent in YAML
  96. 96AI Agent CLI: Start, Stop and Inspect Running Agents
  97. 97AI Agent Monitoring Dashboard in the Terminal
  98. 98Multi-Agent Event Bus: Chain Agents With Events
  99. 99Distributed AI Agents: Run Agents Across Many Machines
  100. 100Pause and Resume an AI Agent: Lifecycle States Explained

Lessons in this path

  1. ✓What Is an AI Agent? Definition, How It Works, Examples
  2. ✓How to Build an AI Agent in Python (2026 Guide)
  3. ✓Build a Personal AI Agent: Memory, Reminders, Chat
  4. ✓What Is an Agent Harness? Parts and How to Build One
  5. ✓System Prompt Best Practices for AI Agents in Python
  6. ✓Best LLM for AI Agents (2026): Picks, Prices, How to Test
  7. ✓AI Agent YAML: Define and Run an Agent From a Config File
  8. ✓Streaming LLM Responses with FastAPI: Tokens and Tool Calls
  9. ✓Best Local LLM for AI Agents: Ollama Models by GPU (2026)
  10. ✓Claude AI Agent with MCP: Use Anthropic Models in Promptise
  11. ✓Gemini AI Agent with MCP: Use Google Models in Promptise
  12. ✓Azure OpenAI Agent with MCP: Use Deployments in Promptise
  13. ✓OpenRouter AI Agent with MCP: Many Models in Promptise
  14. ✓Ollama AI Agent with MCP: Run Local Models in Promptise
  15. ✓LLM Semantic Cache and Model Fallback in Python
  16. ✓What Is MCP (Model Context Protocol)? A Developer's Guide
  17. ✓What Is Tool Calling? LLM Function Calling Explained
  18. ✓MCP Tools vs Resources vs Prompts (With Python Examples)
  19. ✓How to Connect MCP Servers to Your AI Agent in Python
  20. ✓Build a Production MCP Server in Python (Step by Step)
  21. ✓MCP Transports: stdio vs Streamable HTTP vs SSE (Python)
  22. ✓MCP 2026-07-28 Migration: Make Your MCP Server Stateless
  23. ✓OpenAPI to MCP: Turn Any REST API into an MCP Server
  24. ✓MCP Authentication in Python: API Keys, JWT and OAuth
  25. ✓Build an Authenticated MCP Server for Your AI Agent (Python)
  26. ✓MCP Security Best Practices: A Checklist for MCP Servers
  27. ✓Testing MCP Servers in Python: pytest Without a Live Server
  28. ✓Too Many MCP Tools? How to Fix LLM Tool Selection in Python
  29. ✓Idempotent Tool Calls: Make AI Agent Retries Safe
  30. ✓AI Agent Tool Failures: Stop Unsafe Workarounds
  31. ✓MCP Server Caching, Timeouts and Circuit Breakers in Python
  32. ✓MCP Middleware for Servers: Auth, Logging, Rate Limits
  33. ✓MCP vs CLI for AI Agents: Tokens, Security, When to Use Each
  34. ✓LangChain vs MCP: Differences and How They Work Together
  35. ✓FastMCP Alternatives for Production MCP Servers (2026)
  36. ✓AI Agent Audit Logging: Tamper-Evident Logs in Python
  37. ✓AI Agent Human in the Loop: Approve Tool Calls in Python
  38. ✓AI Agent Prompt Injection: How to Defend Against It
  39. ✓Hidden Text Prompt Injection: Sanitize Pages for AI Agents
  40. ✓Build a Multi-Agent System in Python (and When Not To)
  41. ✓Context Engineering for AI Agents: Context Window Management
  42. ✓Context Compaction for AI Agents: Trim vs Summarize
  43. ✓How to Deploy an AI Agent with Docker (2026 Guide)
  44. ✓LLM Tool Rate Limiting: Per-Client Limits for MCP Servers
  45. ✓MCP Long-Running Tasks: Background Jobs in Python
  46. ✓MCP Server Monitoring: Prometheus, OpenTelemetry and Logs
  47. ✓Best AI Agent Frameworks (2026): Python Options Compared
  48. ✓Self-Improving AI Agent: Learn From Tool Failures in Python
  49. ✓AI Agent Identity: Authenticate Agents to MCP Servers
  50. ✓Code-Action Agents in Python: One Program vs 37 Tool Calls
  51. ✓Multi-Tenant AI Agents: Keep Each Customer's Data Apart
  52. ✓What Is Agentic RAG? How It Differs from Classic RAG
  53. ✓AI Agent Memory in Python: Short-Term and Long-Term
  54. 54Per-User Agent Memory Isolation: Stop Leaks Between Users
  55. 55Agentic RAG in Python: Give Your AI Agent a Search Tool
  56. 56AI Agent Reasoning Patterns in Python: From ReAct to Debate
  57. 57Plan-and-Execute Agents in Python: Beyond the ReAct Loop
  58. 58Self-Modifying AI Agent in Python: Open Mode & Limits
  59. 59CrewAI Alternatives: 7 Options and How to Migrate (2026)
  60. 60Migrate from AutoGen: Agent Framework, AG2 and More
  61. 61What Is an LLM Guardrail? Types, Examples and Limits
  62. 62Best LLM Guardrails Tools (2026): 9 Platforms Compared
  63. 63AI Agent Guardrails in Python: Block Injection, Redact PII
  64. 64NeMo Guardrails vs Guardrails AI: Which to Choose (2026)
  65. 65AI-Based PII Detection and Redaction Tools for LLM Apps
  66. 66Stop Secrets Leaking Into LLM Logs and Traces
  67. 67AI Agent Code Execution Sandbox: Run LLM Code Safely
  68. 68AI Agent Network Egress: Limit Where Agents Can Connect
  69. 69AutoGen Docker Task Isolation: Secure the Code Executor
  70. 70SPIFFE for AI Agents: Workload Identity Without API Keys
  71. 71On-Behalf-Of Auth for AI Agents: Act as Each User
  72. 72AI Agent Production Checklist: What to Check Before Launch
  73. 73Approval Fatigue: Auto-Approve Safe AI Agent Actions
  74. 74What Is Human in the Loop AI? Approvals for AI Agents
  75. 75AI Agent Notifications: Webhook and Slack Alerts in Python
  76. 76AI Agent Observability: Trace Tool Calls, Tokens and Cost
  77. 77Best AI Agent Observability Tools (2026), Compared
  78. 78AI Agent Stuck in a Loop? How to Detect and Stop It
  79. 79How to Run, Debug and Deploy an AI Agent in Python
  80. 80AI Agent Cost Tracking: Tokens and Cost per Run and User
  81. 81How to Evaluate AI Agents: Evals, LLM Judges and pass^k
  82. 82AI Agent Cost Control: Budgets That Stop Runaway Spend
  83. 83How to Test LLM Prompts and AI Agents in Python
  84. 84AI Agent Evals in CI: Run Them on Every Pull Request
  85. 85What Is an Agentic Runtime? AI Agent Runtimes Explained
  86. 86Autonomous AI Agent Runtime: Budgets, Crash Recovery, API
  87. 87Event-Driven AI Agents: Webhook, Cron and File Triggers
  88. 88AI Agent Cron Jobs: Run an Agent on a Schedule
  89. 89AI Agent Webhook Trigger: Run an Agent on HTTP Events
  90. 90AI Agent File Watcher: Process New Files Automatically
  91. 91Custom AI Agent Triggers: Wake an Agent From Any Source
  92. 92AI Agent Event Filters: Skip Runs Before the LLM
  93. 93AI Agent Retries and a Dead Letter Queue for Failed Runs
  94. 94Run Multiple AI Agents: One Runtime, Many Processes
  95. 95AI Agent Manifest: Declare a Long-Running Agent in YAML
  96. 96AI Agent CLI: Start, Stop and Inspect Running Agents
  97. 97AI Agent Monitoring Dashboard in the Terminal
  98. 98Multi-Agent Event Bus: Chain Agents With Events
  99. 99Distributed AI Agents: Run Agents Across Many Machines
  100. 100Pause and Resume an AI Agent: Lifecycle States Explained
← Path overview

Per-user memory isolation is what stops a shared AI agent from telling one employee what another employee told it. When a single internal assistant serves HR, sales and engineering, long-term memory becomes a quiet leak path: a fact stored during one person's conversation can be recalled into someone else's. This guide shows how that leak happens, how per-user scopes and role-gated team notes prevent it, what is automatic in Promptise Foundry and what isn't, and how to prove isolation with a canary test.

[01]

How do you stop agent memory leaking between users?

Partition memory by a verified user identity and make every read and write carry it. Each user's memories live under their own key, so a search can only return entries that user wrote. Knowledge meant for a group goes in a separate, read-only store that application code writes and filters by the caller's roles. A request with no user identity gets no memory at all.

Key takeaways:

  • A shared memory pool plus automatic storing is a leak. Every exchange one person has becomes recall material for everyone else.

  • In Promptise Foundry the default memory scope is SHARED. Set scope=MemoryScope.PER_USER on the provider; it isn't on by default. Since 1.7.0 the agent logs a warning the first time a provider left at that default serves a second caller.

  • The key must come from the verified caller, not from a tool argument or the prompt. Foundry uses CallerContext.isolation_key, which is tenant::user when a tenant is set.

  • Privilege levels inside one company need their own rule. Per-user scope keeps people apart; it doesn't know that HR notes are for HR. That part is a small custom provider.

  • Test it with a canary. Plant a unique string as one user and try every way to reach it as another.


[02]

How agent memory leaks between users

Long-term memory for agents usually works the same way everywhere. After a conversation, some text is stored: the whole exchange, a summary, or extracted facts. Before the next request, the agent searches the store with the new question and puts the best matches into the prompt. The model then treats those matches as context it can use.

The leak comes from three ordinary design choices meeting:

  1. One store for everyone. It's the simplest setup and the default in several libraries.

  2. Automatic writes. The agent saves every exchange, so sensitive things land in memory without anyone deciding they should.

  3. Similarity search. Retrieval returns whatever is closest to the question. It has no idea who wrote an entry or who should see it.

So when an HR manager tells the assistant "Priya's raise is approved, new salary 145,000", that sentence is stored. Later a salesperson asks "What do you know about Priya?", the search finds the HR exchange, and the model answers with the salary. No attack was needed. Here is that exact scenario, run with a shared store:

Pythonleak.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""One shared assistant, two employees, auto-stored memory. Run with: shared | per_user."""

import asyncio
import logging
import sys

from promptise import CallerContext, build_agent
from promptise.memory import InMemoryProvider, MemoryScope


class OneLine(logging.Formatter):
    def formatException(self, ei):  # keep the exception type and message only
        return f"  {ei[0].__name__}: {ei[1]}"


handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(OneLine("%(levelname)s %(name)s: %(message)s"))
logging.basicConfig(level=logging.WARNING, handlers=[handler])

DANA = CallerContext(user_id="dana", roles={"hr"})
SAM = CallerContext(user_id="sam", roles={"sales"})


async def say(agent, caller, text):
    result = await agent.ainvoke({"messages": [{"role": "user", "content": text}]}, caller=caller)
    who = caller.user_id if caller else "no caller"
    print(f"[{who}] {text}\n  -> {result['messages'][-1].content}")


async def main(mode: str) -> None:
    # "shared" leaves the scope at its default, as most code does.
    memory = InMemoryProvider() if mode == "shared" else InMemoryProvider(scope=MemoryScope.PER_USER)
    agent = await build_agent(
        model="openai:gpt-5-mini",
        memory=memory,
        memory_auto_store=True,  # every exchange is written to memory
        instructions="You are Acme's internal assistant. Answer in one or two sentences.",
    )
    try:
        await say(agent, DANA, "Note for later: Priya Shah's raise is approved, new salary 145,000 from January.")
        question = "What do you know about Priya Shah?"
        found = await memory.search(question, user_id=SAM.isolation_key)
        print(f"memory search as sam: {len(found)} result(s)")
        await say(agent, SAM, question)
        if mode == "per_user":
            await say(agent, None, question)
    finally:
        await agent.shutdown()


if __name__ == "__main__":
    asyncio.run(main(sys.argv[1]))
>_Terminal
python leak.py shared
Output
[dana] Note for later: Priya Shah's raise is approved, new salary 145,000 from January.
  -> Noted and saved: Priya Shah’s raise approved, new salary $145,000 effective January. Do you mean January of next year or should I record a specific year?
memory search as sam: 1 result(s)
WARNING promptise.memory: Memory provider InMemoryProvider uses the default scope=SHARED, and this agent now serves more than one caller: every caller's memory search returns entries stored for other callers. memory_auto_store=True stores every conversation in that pool, so one user's messages can be recalled for another. Pass scope=MemoryScope.PER_USER to keep each caller's memories separate, or scope=MemoryScope.SHARED to share them on purpose (which also silences this warning).
[sam] What do you know about Priya Shah?
  -> I have a saved note that Priya Shah’s raise was approved with a new salary of $145,000 effective January; would you like me to record which year for that change?

Sam is in sales. The memory search as Sam found Dana's exchange, and the model repeated her salary note to him. The same thing happens with a vector database, where "Priya" questions sit close to "Priya" memories in embedding space.

The WARNING line is Foundry noticing the problem. The provider was created without scope=, so it uses the default SHARED scope, and when a second caller identity (Sam) reached it, the agent logged what is shared and how to fix it. It warns once per agent. It doesn't block anything: the leak still happened. A provider created with an explicit scope=MemoryScope.SHARED is treated as a deliberate choice and stays quiet.

Prompt injection makes it worse. If users can write to memory that others read, one user can plant instructions that run in another user's session. LangChain's Deep Agents documentation warns about exactly this for agent-wide memory and recommends user scope by default, with shared memory kept read-only.


[03]

How per-user scopes work

A per-user scope adds an owner to every memory entry and a filter to every query. There are two ways to build it:

Approach

How it works

Trade-off

Namespace or collection per user

Each user's memories live in a separate partition; a query can only touch one

Strongest isolation; more partitions to manage

Owner tag plus filter

One store, each entry tagged with its owner, every query filtered by owner

Simple to run; correctness depends on the filter always being applied

AWS's guide for LangGraph memory on DynamoDB recommends namespace scoping over metadata filters "for anything that determines correctness", and notes that filters applied after a top-N similarity search can return fewer results for quiet tenants. Mem0 scopes memories by user_id, and LangChain's Deep Agents scope them by namespace. Promptise Foundry's providers use the owner-tag approach, with the filter enforced inside the provider so callers can't forget it:

  • InMemoryProvider stores the owner with each entry and skips other owners' rows on search and delete.

  • ChromaProvider writes the owner into entry metadata and passes it as a where filter on every query.

  • Mem0Provider passes the owner as Mem0's own user_id.

In all three, a per-user call without a user ID raises MemoryIsolationError instead of falling back to the whole store.

Grid of what each caller can recall: Dana (role hr) reads her own memories and team notes tagged all and hr; Sam (role sales) reads his own memories and notes tagged all and sales; a request with no caller raises MemoryIsolationError and gets no memory.
FigureWhat each caller can recall: personal memory by user, team notes by role

[04]

When you need per-user memory (and when shared is fine)

Use per-user scope when:

  • More than one person talks to the same agent, and memory can contain anything they say.

  • Users have different access in your company: HR, finance, legal, management.

  • You store memories automatically rather than curating them.

  • People may ask you to delete what the assistant knows about them.

A shared scope is reasonable when:

  • One person uses the agent, or the agent runs unattended with no users.

  • The store holds curated, non-sensitive knowledge that application code writes, such as product docs or office policies, and the agent never writes to it.

Shared and per-user aren't exclusive. The pattern later in this guide combines a per-user store for personal memory with a read-only store for team knowledge.


[05]

What you need

  • Python 3.10 or newer and uv pip install "promptise>=1.7.0"; add chromadb for the vector store example.

  • An OpenAI key for the agent examples (gpt-5-mini). The canary test needs no model.

  • A way to know who is asking: your app's sign-in, with user ID, tenant and roles taken from a verified token.


[06]

Isolate memory per user, step by step

Turn on per-user scope

Run the same script with a per-user provider. The only change is the scope passed to the provider:

Python
memory = InMemoryProvider(scope=MemoryScope.PER_USER)
>_Terminal
python leak.py per_user
Output
[dana] Note for later: Priya Shah's raise is approved, new salary 145,000 from January.
  -> Noted: Priya Shah’s raise is approved — new salary 145,000 effective January. Would you like me to set a reminder or add this to payroll/HR?
memory search as sam: 0 result(s)
[sam] What do you know about Priya Shah?
  -> Which Priya Shah do you mean — there are many people with that name; I can’t provide personal details about private individuals, but if you mean a public figure (or give a link or context like their profession), I can summarize publicly available information.
WARNING promptise.agent: Memory search failed
  MemoryIsolationError: InMemoryProvider.search requires a user_id when scope=PER_USER
WARNING promptise.memory: Memory auto-store failed
  MemoryIsolationError: InMemoryProvider.add requires a user_id when scope=PER_USER
[no caller] What do you know about Priya Shah?
  -> I don't know which Priya Shah you mean—there are many people with that name; tell me which one (role, location, or a public link) and I can summarize publicly available information, noting I can't access or share private personal data.

The search as Sam returned nothing, so the model had nothing to repeat and treated Priya as an unknown name. There is no shared-scope warning this time. The last call had no caller at all. Both the search and the automatic store raised MemoryIsolationError, the agent logged a warning, and it answered without memory. That is the safe failure: no identity, no recall, no write.

Pass a verified identity on every request

Scoping only works if the agent knows who is asking. In Foundry, you pass a CallerContext to ainvoke() or chat(), and the agent uses its isolation_key for both memory search and automatic storing:

Python
caller = CallerContext(user_id=claims["sub"], tenant_id=claims["org"], roles=set(claims["roles"]))
result = await agent.ainvoke({"messages": messages}, caller=caller)

Build it from your verified session or token, never from something the user typed. When tenant_id is set, the key becomes acme::dana, so two companies that each have a user called dana never share memories. To carry the same user's identity to the tools the agent calls, add their token as bearer_token; on-behalf-of auth covers that side.

Use the same rule on a persistent store

InMemoryProvider is for tests. With ChromaDB, the scope argument is the same, and the owner becomes a metadata filter on the query:

Pythonchroma_scope.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""The same per-user rule on a persistent vector store (ChromaDB)."""

import asyncio

from promptise.memory import ChromaProvider, MemoryScope, owner_key


async def main() -> None:
    memory = ChromaProvider(collection_name="assistant_memory", persist_directory=".chroma",
                            scope=MemoryScope.PER_USER)
    dana, sam = owner_key("dana", "acme"), owner_key("sam", "acme")  # "acme::dana", "acme::sam"
    await memory.add("Prefers summaries as three bullet points.", user_id=dana)
    await memory.add("Works on the Northwind renewal.", user_id=sam)
    for user in (dana, sam):
        hits = await memory.search("How does this person like their summaries?", user_id=user)
        print(user, [(r.content, round(r.score, 2)) for r in hits])
    await memory.close()


asyncio.run(main())
Output
acme::dana [('Prefers summaries as three bullet points.', 0.54)]
acme::sam [('Works on the Northwind renewal.', 0.0)]

owner_key(user_id, tenant_id) returns the key the agent stores a caller's entries under, so code that writes memory directly uses the same key as the agent. Each user only gets their own entry back. Notice Sam's result: his only memory has nothing to do with summaries, and it still came back, with a score of 0.0. Vector search always returns the nearest entries, relevant or not, so set memory_min_score in build_agent to drop weak matches before they reach the prompt.

Add team knowledge gated by role

Per-user scope keeps people apart, but companies also have knowledge that belongs to a group: HR policies, sales pricing rules, on-call runbooks. Putting those in a shared store visible to everyone recreates the leak. Foundry has no built-in notion of role-gated memory, so this is a small provider of your own. build_agent accepts any object that implements the MemoryProvider protocol:

Pythonteam_memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Personal memory per user, plus read-only team notes gated by the caller's roles."""

import asyncio

from promptise import CallerContext, build_agent, get_current_caller
from promptise.memory import InMemoryProvider, MemoryResult, MemoryScope


class RoleScopedMemory:
    """A MemoryProvider: the user's own memories plus the team notes their roles allow."""

    scope = MemoryScope.PER_USER

    def __init__(self, personal: InMemoryProvider, team: InMemoryProvider) -> None:
        assert personal.scope is MemoryScope.PER_USER
        self.personal, self.team = personal, team

    async def search(self, query, *, limit=5, user_id=None) -> list[MemoryResult]:
        caller = get_current_caller()
        allowed = {"all"} | (caller.roles if caller else set())
        mine = await self.personal.search(query, limit=limit, user_id=user_id)
        notes = [r for r in await self.team.search(query, limit=50)
                 if r.metadata.get("audience") in allowed]
        return sorted(mine + notes, key=lambda r: r.score, reverse=True)[:limit]

    async def add(self, content, *, metadata=None, user_id=None) -> str:
        # The agent only ever writes to the user's personal memory.
        return await self.personal.add(content, metadata=metadata, user_id=user_id)

    async def delete(self, memory_id, *, user_id=None) -> bool:
        return await self.personal.delete(memory_id, user_id=user_id)

    async def purge_user(self, user_id, *, tenant_id=None) -> int:
        return await self.personal.purge_user(user_id, tenant_id=tenant_id)

    async def close(self) -> None:
        await self.personal.close()
        await self.team.close()


async def load_team_notes() -> InMemoryProvider:
    """Written by application code from an approved source, never by the agent."""
    team = InMemoryProvider(scope=MemoryScope.SHARED)
    await team.add("Expense reports are due on the 5th of each month.", metadata={"audience": "all"})
    await team.add("The 2027 salary bands are in the HR drive, folder Comp/2027.", metadata={"audience": "hr"})
    await team.add("The Q4 discount ceiling is 15 percent without VP approval.", metadata={"audience": "sales"})
    return team


async def main() -> None:
    memory = RoleScopedMemory(InMemoryProvider(scope=MemoryScope.PER_USER), await load_team_notes())
    agent = await build_agent(
        model="openai:gpt-5-mini",
        memory=memory,
        instructions="You are Acme's internal assistant. Answer in one or two sentences from memory.",
    )
    dana = CallerContext(user_id="dana", roles={"hr"})    # roles come from the verified token
    sam = CallerContext(user_id="sam", roles={"sales"})
    question = "Where are the 2027 salary bands, and when are expense reports due?"
    try:
        for caller in (dana, sam):
            result = await agent.ainvoke({"messages": [{"role": "user", "content": question}]}, caller=caller)
            print(f"[{caller.user_id}] {result['messages'][-1].content}")
        hits = await memory.search(question, user_id="sam", limit=5)  # outside an invocation: no caller
        print("search with no caller:", [r.metadata["audience"] for r in hits])
    finally:
        await agent.shutdown()


if __name__ == "__main__":
    asyncio.run(main())
Output
[dana] The 2027 salary bands are on the HR drive in the folder Comp/2027. Expense reports are due on the 5th of each month.
[sam] I don't have the location of the 2027 salary bands in my memory; expense reports are due on the 5th of each month.
search with no caller: ['all']

Dana, with the hr role, got the salary band location and the expense rule. Sam asked the same question and only got the note tagged all. Three rules keep this sound:

  • Team notes are read-only for the agent. add() only ever writes to the user's personal store; application code loads team notes from an approved source.

  • Roles come from the verified caller. The provider reads them with get_current_caller(), which only exists inside an invocation. Outside one, as the last line shows, it falls back to the all notes.

  • Personal memory still fails closed. With no caller, the personal search raises, so the whole search fails and the agent answers without memory.

For stricter separation, give each group its own collection instead of tagging entries, which is the namespace approach from the table above.

Prove isolation with a canary test

A canary test plants a unique string as one user and checks that no other identity can reach it. It needs no model, so it belongs in your test suite:

Pythontest_isolation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Canary test: plant a secret as one user, try every way to reach it as another."""

import asyncio

from promptise import CallerContext
from promptise.memory import InMemoryProvider, MemoryIsolationError, MemoryScope

CANARY = "canary-7f3a: Priya Shah salary 145000"


async def main() -> None:
    memory = InMemoryProvider(scope=MemoryScope.PER_USER)
    dana = CallerContext(user_id="dana", tenant_id="acme").isolation_key
    sam = CallerContext(user_id="sam", tenant_id="acme").isolation_key
    other_dana = CallerContext(user_id="dana", tenant_id="globex").isolation_key
    canary_id = await memory.add(CANARY, user_id=dana)

    for query in ("canary-7f3a", "Priya Shah", "salary", CANARY):
        assert await memory.search(query, user_id=sam) == [], query
        assert await memory.search(query, user_id=other_dana) == [], query
    print(f"search as {sam} and {other_dana}: nothing found for 4 queries")

    assert await memory.delete(canary_id, user_id=sam) is False
    print("delete as sam: refused")

    try:
        await memory.search("Priya Shah")
    except MemoryIsolationError as exc:
        print(f"search with no user: {type(exc).__name__}")

    assert [r.content for r in await memory.search("Priya Shah", user_id=dana)] == [CANARY]
    print(f"search as {dana}: finds her own entry")
    # Deletion on request: the entries are stored under "acme::dana", so say which tenant.
    print(f"purge_user('dana') removed {await memory.purge_user('dana')} entries")
    print(f"purge_user('dana', tenant_id='acme') removed {await memory.purge_user('dana', tenant_id='acme')} entry")
    assert await memory.search("Priya Shah", user_id=dana) == []


asyncio.run(main())
Output
search as acme::sam and globex::dana: nothing found for 4 queries
delete as sam: refused
search with no user: MemoryIsolationError
search as acme::dana: finds her own entry
purge_user('dana') removed 0 entries
purge_user('dana', tenant_id='acme') removed 1 entry

The test covers a different user in the same tenant, the same user name in another tenant, deleting another user's entry, searching with no user, and deleting on request. The last two lines show the deletion pitfall: the agent stores Dana's entries under acme::dana, so purge_user("dana") finds nothing and returns 0. Pass the tenant, purge_user("dana", tenant_id="acme"), or the key itself. Check the count it returns when you handle a deletion request. Run the same test against your production provider type, not only the in-memory one, and see how to test AI agent memory for recall tests that check the right memories do come back.


[07]

What isn't automatic in Promptise Foundry

Some isolation surfaces are handled for you once a caller is passed, and some are not. Be clear about which is which:

Surface

Automatic?

What to do

Memory scope

No: default is SHARED, with a warning when a second caller arrives

Pass scope=MemoryScope.PER_USER to the provider

Memory search and auto-store key

Yes, when a CallerContext is passed

Always pass the caller

Your own provider.add() calls

No

Pass user_id=caller.isolation_key or owner_key(user_id, tenant_id) yourself

Role or group gating inside a company

No

A custom provider, as above

Conversation sessions

Yes: sessions record an owner and refuse other users

Pass the caller to chat()

Semantic cache

Yes: default scope is per user

Leave it; shared requires an explicit acknowledgment

Traces

Partly: since 1.9.0 every entry records the caller's user and tenant

Delete a user's entries with collector.purge_user(user_id, tenant_id=...); redact what they hold

Logs, model provider retention

No

Redact and control them separately

The sessions row matters because chat history is memory too. The AI agent memory guide shows a user being refused another user's session even with the exact session ID.


[08]

Running shared memory in production

  • Decide what gets stored. Automatic storing of whole exchanges is convenient and risky. For agents that see sensitive data, store facts your app extracts and checks, or let users save memories explicitly.

  • Keep sensitive data out of memory entirely when it isn't needed later. Redacting before storage is easier than isolating perfectly; see PII redaction.

  • Treat recalled memory as untrusted input. Foundry wraps injected memories in a fenced block and strips known role markers such as SYSTEM:, but the text of an injected instruction can survive. Shared notes should be written by code, not by users. The prompt injection guide covers the wider defense.

  • Handle deletion requests across every store: purge_user(user_id, tenant_id=...) for memory, session deletion for conversations, collector.purge_user(user_id, tenant_id=...) for the traces still in the collector's buffer, and your logs and exported traces. Both purge_user functions match a bare user ID only to entries stored without a tenant, so pass the tenant. On a provider with SHARED scope, purge_user deletes nothing and returns 0, because its entries have no owner: one more reason to use PER_USER.

  • Watch for two warnings. A Memory search failed ... MemoryIsolationError log line means a request reached the agent without a caller; it usually points to a code path that forgot to pass the identity. A uses the default scope=SHARED, and this agent now serves more than one caller line means a provider is pooling every user's memories. Alert on both, and treat the second as a bug unless sharing is intended.


[09]

Common problems and fixes

Symptom

Cause

Fix

One user's details show up in another's answer

Provider uses the default SHARED scope

Set scope=MemoryScope.PER_USER

Warning uses the default scope=SHARED, and this agent now serves more than one caller

A provider created without scope= served two users

Set scope=MemoryScope.PER_USER, or scope=MemoryScope.SHARED if the pool is shared on purpose

Memory never recalled, warning MemoryIsolationError in logs

No CallerContext passed on that code path

Pass caller= to every ainvoke(), chat() and astream()

purge_user removes 0 entries

Called with the bare user ID while the caller had a tenant

Pass tenant_id=, or the key from owner_key()

Irrelevant memories injected

Vector search returns the nearest entries even when they don't match

Set memory_min_score

Team notes visible to the wrong group

Notes stored in a shared store without gating

Tag notes by audience and filter by verified roles, or use one collection per group

Memories from before the change still leak

Old entries were stored without an owner

Re-create the store, or migrate entries with an owner tag


[10]

Limitations

  • Owner tags are a filter, not separate storage. A bug in a custom provider, or direct access to the database, bypasses it. Namespaces or collections per user give structural separation.

  • The isolation is only as good as the identity. If your app builds the CallerContext from unverified input, scoping protects nothing.

  • Role-gated memory is your code. The example provider is a starting point; it doesn't handle role changes for memories already recalled into a running conversation.

  • Isolation doesn't stop a user's own memory being misused. Instructions planted in a user's memory still reach that user's sessions.


[11]

FAQ

Why does my AI agent remember things other users told it?

Its long-term memory is one shared store, and it searches that store with each new question. Anything another user said that is similar to the question can be recalled. Scope memory per user, so each search only returns the asking user's entries, and stop storing sensitive exchanges automatically.

What is per-user memory isolation in an AI agent?

Per-user memory isolation means every stored memory has an owner and every search is limited to the asking user's memories. It is enforced in the memory layer with a verified user ID, not in the prompt, so the model never sees other users' entries.

Should I use a namespace per user or a metadata filter?

Both work when applied consistently. A namespace or collection per user gives structural separation, so no query can cross users. A metadata filter on one store is simpler to operate but relies on the filter being applied on every read. Use separate namespaces when a mistake would be serious.

How do I share some memories with a team but not others?

Keep team knowledge in a separate store that application code writes, tag each entry with its audience, and filter it by the caller's verified roles at search time. Keep personal memory in a per-user store and let the agent write only there.

How do I test that agent memory doesn't leak between users?

Use a canary test. Store a unique string as one user, then search for it as a different user, as the same user name in another tenant, and with no user, and assert nothing comes back. Run it against the same provider type you use in production.

Does Promptise Foundry isolate memory per user by default?

No. Memory providers default to MemoryScope.SHARED, and since 1.7.0 the agent logs a warning when such a provider serves a second caller. Set scope=MemoryScope.PER_USER and pass a CallerContext on every request; Foundry then keys memory search and automatic storing on the caller's isolation key. Conversation sessions and the semantic cache are per user by default.


[12]

Next steps

  • Add short-term and long-term memory from the start: AI Agent Memory in Python.

  • Check that the right memories come back, not just that the wrong ones don't: How to Test AI Agent Memory.

  • Make tools act as the same user: On-Behalf-Of Auth for AI Agents.

  • Separate whole customers, not just users: Multi-Tenant AI Agents.

  • Keep the cache per user too: LLM Semantic Cache and Model Fallback.

← PreviousAI Agent Memory in Python: Short-Term and Long-TermNext lesson →Agentic RAG in Python: Give Your AI Agent a Search Tool