Register and share your invite link to earn from video plays and referrals.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
Joined May 2026
279 Following    409 Followers
# 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#
Show more