Skip to main content
Prefetch pulls relevant long-term memories into the system prompt at the start of every turn — the agent answers with context it never had to ask for. Prefetch is “auto” by default. On query-based backends (sqlite, chroma, mem0, {"provider": ...}) it runs automatically — facts saved with agent.store_memory(..., memory_type="long_term") reach the model with no prefetch=True and no recall() call. On the file backend it stays off because file memory already injects recent items into the system prompt itself. Pass prefetch=True or prefetch=False to override.

Quick Start

1

Simple Usage

Pick a query-based backend and stored facts reach the model automatically.
2

Scope per user (optional but recommended)

Set user_id so recalled memories persist across runs and stay private to one user.
3

Tune the budget

Cap how many memories are injected and how large the block can grow.
4

Force prefetch on or off

Override the auto default when you need to.
5

Automatic cross-instance recall with auto_memory

Set auto_memory=True on a shared user_id and the first agent persists each turn durably — a second agent on the same backend recalls it with no explicit store call.
6

Cross-agent recall with an explicit store

Use store_memory(..., memory_type="long_term") when you want to control exactly what is stored, instead of persisting the whole turn.

How It Works

Prefetch searches long-term memory with the user prompt, dedupes and budgets the matches, then appends them to the system prompt before the first LLM call. Multimodal prompts (a list of {type: "text", text: ...} blocks) also drive the search — non-text parts are ignored for the query.
Prefetch defaults to auto, not True. On FileMemory it never queries the backend (recent items are already in the system prompt). On sqlite/chroma/mem0 the backend is queried once per turn, scoped by user_id / session_id. Backend errors are swallowed at debug level — the turn continues without recalled context.

Configuration Options

Full list of options, types, and defaults — MemoryConfig
Identity scoping (user_id, session_id) is inherited from the same MemoryConfig. Backends: file (default), sqlite, chroma, mongodb, mem0, dakera, in_memory — see Memory.

Common Patterns

Pattern 1 — Personal assistant with recalled preferences

Pattern 2 — Tight budget for cost control


Best Practices

With a query-based backend (sqlite, chroma, mem0, {"provider": ...}), stored facts reach the model with no extra flags. Only pass prefetch=True when you want it on for FileMemory, or prefetch=False to force it off for cost reasons.
Without user_id, prefetch searches the per-instance store — a fresh agent-<uuid> on every process. Set user_id so recalled memories persist across runs.
prefetch_token_budget=512 is the ceiling, not the target. Large recalled blocks push out user context — start small and raise only when needed.
The async path awaits async backends; sync backends are offloaded off the event loop. No extra configuration required.
Any backend error is logged at debug level and the agent proceeds without recalled context — prefetch never fails a turn.
Prefetch appends to the system prompt, so an agent with use_system_prompt=False returns an empty prefetch and never queries the backend — even when prefetch is auto or True. Keep the system prompt enabled for recalled context to appear.

Memory

Memory backends and the remember/recall API

Knowledge

Add documents and URLs as agent knowledge