# Practical and Useful Patterns with ADK
Sending the same system prompts and tool definitions over and over? Context Caching dramatically reduces repeated prefix token costs 💰
📌 **Title**: Context Caching
🔗 **URL**:
## 🧩 Overview
Context Caching caches the static portions of context sent to the LLM (system instructions, tool definitions, etc.) to reduce repeated token costs.
By configuring `ContextCacheConfig`, instead of resending the same prefix tokens with every request, the agent references cached context.
The cost optimization impact is especially significant in multi-user environments where many users share the same agent with identical prompts and tool definitions.
## 🛠 How to Use
Import `Agent` from `google.adk` and `ContextCacheConfig` from `google.adk.agents`. Create a cache configuration with `ContextCacheConfig(max_entries=100, ttl_seconds=3600)` to set the maximum number of cache entries and the time-to-live in seconds. Pass this `cache_config` to the `Agent`'s `context_cache_config` parameter so that the static portions of the `instruction` (your long system prompt) and `tools` definitions (e.g., `search_kb`, `create_ticket`, `escalate`) are cached, reducing repeated token costs.
## 🏗 Practical Usage
**Large-scale customer support optimization:**
In a customer support agent, these elements are common across all users:
- System instructions (response guidelines, tone, prohibited actions)
- Tool definitions (knowledge base search, ticket creation, escalation)
- Few-shot examples
These static contexts can amount to thousands of tokens per request. For a support bot handling 10,000 requests daily, Context Caching delivers massive token savings.
**RAG pipeline optimization:**
When tool definitions include knowledge base schemas and search parameter descriptions, caching these optimizes per-query costs.
**Multi-tenant SaaS:**
When sharing the same agent definition across multiple tenants, only tenant-specific information becomes the dynamic portion while common prompts and tool definitions are shared via cache.
## 💡 Use Cases
- 💰 Cost reduction: Cut costs from repeatedly sending long system prompts
- 🚀 Latency improvement: Faster prefill processing on cache hits
- 👥 Multi-user optimization: Share cache across multiple users with the same prompts
- 🏢 Multi-tenant: Efficiently cache tenant-common context portions
- 📚 Large tool definitions: Optimize tool definition costs for agents with many tools
## ⚠️ Caveats
- Context Caching depends on model provider support. Verify available models in advance
- TTL too short reduces hit rates; too long consumes memory. Tune based on access patterns
- Benefits are limited if system prompts or tool definitions change frequently
- Caching itself may incur costs. Check provider pricing and evaluate total cost
- Dynamic context (user-specific information, etc.) is not cacheable. Design clear separation between static and dynamic portions
✨ Context Caching implements "don't repeat yourself" at the infrastructure level. It delivers major cost optimization benefits in multi-user environments!
#
ADK# #
AIAgent#