RAG Scoping & Filtering
Filter retrieval with user_id, agent_id, and run_id for multi-tenant isolation
Scope knowledge retrieval per user, agent, or session so tenants and conversations stay isolated.
from praisonaiagents import Agent, MemoryConfig
agent = Agent(
name="SupportBot",
instructions="Answer from the customer's knowledge only.",
knowledge=["./support_docs/"],
memory=MemoryConfig(user_id="customer_42"),
)
agent.start("What was the status of my refund request?")The user asks in a scoped session; retrieval filters by tenant and user so answers stay isolated.
How It Works
Quick Start
- Simple Usage
Set
user_idvia memory config — the agent passes it to every knowledge search:python from praisonaiagents import Agent, MemoryConfig agent = Agent( name="PersonalAssistant", instructions="You are a personal assistant.", knowledge=["./documents/"], memory=MemoryConfig(user_id="alice"), ) agent.start("Find my recent notes about the project") - With Configuration
Combine scopes for multi-tenant SaaS — pass
run_idon retrieve for session-specific context:python from praisonaiagents import Agent, MemoryConfig session_id = "session_2026_05_30" agent = Agent( name="SupportBot", instructions="Answer customer questions using their session history.", knowledge=["./support_docs/"], memory=MemoryConfig(user_id="customer_42"), ) agent.retrieve("Refund status?", run_id=session_id)
How It Works
The agent forwards user_id and agent_id (auto-generated) to the knowledge backend. Vector stores combine filters with $and when multiple scopes are set.
| Scope | Source | Purpose |
|---|---|---|
user_id | MemoryConfig(user_id=…) | Per-user isolation |
agent_id | agent.agent_id (auto) | Per-agent isolation |
run_id | retrieve(…, run_id=…) or store metadata | Per-session isolation |
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
user_id | str | "praison" | User scope on MemoryConfig |
agent_id | str | auto UUID | Agent scope (read-only property) |
run_id | str | None | Session scope on retrieve/store |
Backends: ChromaDB supports all scopes; mem0 requires at least one scope identifier.
Best Practices
Set user_id for tenant isolation
Use memory=MemoryConfig(user_id=customer_id) in SaaS apps — each customer’s retrieval stays separate.
Pass run_id for ephemeral sessions
Scope temporary conversation context with agent.retrieve(query, run_id=session_id).
Pick the backend for your scoping needs
mem0 enforces scope at ingest; ChromaDB filters at query time with combined $and clauses.
Use consistent identifier naming
Prefer readable IDs like customer_alice_123 over opaque tokens for easier debugging.