Skip to main content
Prompt caching can help reducing processing time and costs. Consider it if you are using the same prompt multiple times in any flow. You can read more about prompt caching with Anthropic models here.

Usage

To use prompt caching in your Agno setup, pass the cache_system_prompt argument when initializing the Claude model:
Notice that for prompt caching to work, the prompt needs to be of a certain length. You can read more about this on Anthropic’s docs.

Extended cache

You can also use Anthropic’s extended cache beta feature. This updates the cache duration from 5 minutes to 1 hour. To activate it, pass the extended_cache_time argument and the following beta header:

Multi-block caching with per-block TTL

Split the system prompt into independently-cacheable blocks with system_prompt_blocks. Each SystemPromptBlock controls its own cache flag and ttl. This lets you cache static instructions while leaving dynamic per-request content uncached.
cookbook/11_models/anthropic/prompt_caching_multi_block.py
Blocks are appended after the agent-built system message in the Anthropic system array. system_prompt_blocks may also be a zero-arg callable that returns the list, evaluated on every request, which is how you inject dynamic content into a cached prompt without reinstantiating the model.
Anthropic requires any 1h cached block to appear before any 5m block in the request. Since the agent-built block comes first and inherits the model-level TTL, set extended_cache_time=True whenever any SystemPromptBlock uses ttl="1h". Agno validates this ordering at assembly time and raises a clear error if it is violated.

Tool caching

Set cache_tools=True to cache tool definitions. Anthropic caches all tools as a prefix when cache_control is on the last tool.

Working example

cookbook/11_models/anthropic/prompt_caching_extended.py