Prompt Caching
Mark a reusable system prompt for caching and inspect Claude’s cache usage on Vertex AI.
Mark the system prompt with cache_system_prompt=True, then inspect the cache usage reported for each request.
Cache writes and hits depend on provider eligibility, an identical prefix of at least 1,024 tokens for Sonnet 4 or 4.6, and reuse within the cache lifetime. The default ephemeral lifetime is five minutes. Cache writes cost more than uncached input; repeated reads can offset that cost. The source’s first-write, second-hit and savings comments are expectations to measure with cache_write_tokens and cache_read_tokens. They are not guarantees. See Google’s caching guide.
The source uses claude-sonnet-4@20250514, which Anthropic lists as deprecated on Google Cloud. Google's Sonnet 4 card does not establish a completed retirement. For a new deployment, choose a current model and its supported settings, such as Sonnet 4.6. Provider availability and lifecycle dates can differ from the direct Anthropic API.
"""
This cookbook shows how to use prompt caching with Agents using Anthropic models, to catch the system prompt passed to the model.
This can significantly reduce processing time and costs.
Use it when working with a static and large system prompt.
You can check more about prompt caching with Anthropic models here: https://docs.anthropic.com/en/docs/prompt-caching
Note: It takes a few seconds for the cache to be created and used for the second run.
"""
from pathlib import Path
from agno.agent import Agent
from agno.models.vertexai.claude import Claude
from agno.utils.media import download_file
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
# Load an example large system message from S3. A large prompt like this would benefit from caching.
txt_path = Path(__file__).parent.joinpath("system_prompt.txt")
download_file(
"https://agno-public.s3.amazonaws.com/prompts/system_promt.txt",
str(txt_path),
)
system_message = txt_path.read_text()
agent = Agent(
model=Claude(
id="claude-sonnet-4@20250514",
cache_system_prompt=True, # Activate prompt caching for Anthropic to cache the system prompt
),
system_message=system_message,
markdown=True,
)
# First run - this will create the cache
response = agent.run(
"Explain the difference between REST and GraphQL APIs with examples"
)
if response and response.metrics:
print(f"First run cache write tokens = {response.metrics.cache_write_tokens}")
# Second run - this will use the cached system prompt
response = agent.run(
"What are the key principles of clean code and how do I apply them in Python?"
)
if response and response.metrics:
print(f"Second run cache read tokens = {response.metrics.cache_read_tokens}")
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
passRun the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno "anthropic[vertex]"Set up Google Cloud access
Use a project with billing and aiplatform.googleapis.com enabled, enable the chosen Claude model in Model Garden, and give your calling identity permission to use it. Follow Google's Claude setup guide. Install the Google Cloud CLI for the local sign-in command below.
Set the project and a location supported by your model. us-east5 is a supported example for Sonnet 4 and Sonnet 4.6; check the model's current availability before switching models or locations.
export ANTHROPIC_VERTEX_PROJECT_ID="your-project-id"
export CLOUD_ML_REGION="us-east5"Authenticate locally with Application Default Credentials:
gcloud auth application-default loginADC supplies credentials; it does not enable models or grant IAM permissions. These examples authenticate to Google Cloud without an Anthropic API key.
Run the example
Save the code above as prompt_caching.py, then run:
python prompt_caching.pyFull source: cookbook/90_models/vertexai/claude/prompt_caching.py