Quick Start
Use deterministic memory ordering
How It Works
| Component | Caching Behavior |
|---|---|
| Memory search results | Returned in deterministic order based on content hashing and timestamps |
| Tool schemas | Consistent ordering for reproducible prompts |
| Context structure | Stable system prompt + memory + dynamic user input |
| Cache effectiveness | High hit rates when underlying data is unchanged |
Configuration Options
Thebuild_context_for_task() method provides these parameters for cache-optimized context:
| Parameter | Type | Default | Description |
|---|---|---|---|
task_descr | str | — | Task description for memory search |
user_id | Optional[str] | None | Optional user ID for personalised memory |
additional | str | "" | Additional context to include in search |
max_items | int | 3 | Maximum items per memory category |
include_in_output | Optional[bool] | None | Whether to include memory content in output (set to True for manual prompt assembly) |
str — deterministically ordered context string combining relevant memories
Common Patterns
Multi-turn chat with memory
Just usingmemory=True is enough; deterministic ordering is automatic.
Manual prompt assembly for custom LLM calls
Usebuild_context_for_task() with explicit output control for manual prompt construction.
Tool schema consistency
Tools are ordered consistently to maintain prompt cache effectiveness across invocations.Best Practices
Keep memory writes batched between turns
Keep memory writes batched between turns
Minimize memory updates during conversation turns to maintain cache consistency. Update memories at conversation boundaries.
Structure prompts for caching
Structure prompts for caching
Place stable system content (instructions, tools, static memory) before dynamic content (user messages, fresh data).
Use consistent parameters
Use consistent parameters
Varying
max_items or other context parameters between turns changes the context and reduces cache effectiveness.Maintain deterministic ordering
Maintain deterministic ordering
The framework automatically ensures consistent ordering for memory search results and tool schemas to optimize cache hits.
Related
Advanced Memory
Memory configuration and search strategies
Stateful Agents
Building agents that maintain conversation state

