Skip to main content
Turn on memory on a supported model — the SDK sorts tools and lays out context so providers can cache the stable prefix.
The user chats across turns; stable prefixes stay cached for lower cost.

How It Works

Quick Start

1

Simple Usage

Automatic on supported models when memory is enabled:
2

With Configuration

Make prompt caching explicit:

How It Works

CACHE_BOUNDARY is a reserved marker constant ("--- CACHE_BOUNDARY ---") on praisonaiagents.memory.memory for future structured-cache metadata. No current provider consumes it, so it is intentionally not appended to the outgoing system prompt. Providers hit the cache because the prefix is deterministic — not because of any marker.
On OpenAI and Gemini the SDK emits no explicit cache markers. Those providers apply prefix caching automatically as long as the request prefix is byte-identical across turns. The optimisations on this page — deterministic tool order, stable memory layout, no wall-clock in the system prompt — are what make automatic caching work.
Anthropic works differently: it gets explicit cache_control breakpoints on the system block and the stable history prefix. See Prompt Caching for the Anthropic breakpoint behaviour and the provider-behaviour table.
Check model support:

Detecting Cache Invalidation

When a turn’s prefix changes and the provider cache misses, the gateway can tell you — via a hook event and a metric, automatically when metrics are on.
Fingerprinted: model identity (from agent.llm, or .model, or str()), the sorted set of tool schema fingerprints (name + description + JSON-serialised params for dict tools; name + docstring for callables), and the agent.instructions string. Tool reordering keeps the signature stable. Excluded (to avoid false positives): arrival time, routing facts, memory/RAG recall, and per-turn user text. The PROMPT_PREFIX_INVALIDATED hook listener receives:
Best-effort semantics: prompt_prefix_signature never raises — missing attributes degrade to empty strings. The baseline is cleared on session reap/clear/reset, so a reopened session does not fire a false invalidation. The metric only moves when GatewayMetrics is enabled.

Configuration Options

Set via Agent(caching=True) or Agent(caching=CachingConfig(...)). The include_cache_boundary flag on build_cache_optimized_context() controls whether the returned dict contains the marker; it does not affect what is sent on the wire.

Best Practices

OpenAI, Anthropic, Bedrock, and Deepseek support caching — local models like Ollama do not.
Changing system prompts between turns breaks the cached prefix.
Without memory, only tool sorting applies — memory=True activates the cache-optimised context path.
Do not manually reorder tool lists — the SDK sorts deterministically by name.

Prompt Caching

Deterministic memory ordering for cache hits

Prompt Caching CLI

Enable caching from the command line