How can I explicitly enable context caching for a custom agent, and does automatic caching work for custom agents? #3877
Replies: 4 comments
-
|
Having similar questions. Also are only static instructions cached or instructions are also cached implicitly? |
Beta Was this translation helpful? Give feedback.
-
|
@adk-bot Can you answer? |
Beta Was this translation helpful? Give feedback.
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: Context caching is configured at the Hello! Here are detailed answers to your questions based on the ADK documentation. 1. How can explicit context caching be configured for a custom agent?Context caching in ADK is enabled and configured on the Since a custom agent can be the Here is an example of how to configure it: from adk.context import ContextCacheConfig
from adk.api import App
# Configure caching to last for 10 minutes
cache_config = ContextCacheConfig(ttl_seconds=600)
# app is an instance of adk.api.App
app = App(
root_agent=my_custom_agent, # Your custom agent instance
context_cache_config=cache_config
)2. Does automatic caching work for custom agents?Yes, the ADK’s explicit caching mechanism works for the operations within a custom agent. A custom agent itself doesn't make LLM calls, but it orchestrates other agents (like 3. How does caching behave with
|
Beta Was this translation helpful? Give feedback.
-
|
This is very directly related to cost. Although adk-bot provided an answer, there was no supporting evidence at all, so it is not trustworthy. I have one additional question. As a countermeasure, is it possible to separate the cacheable part and the variable part and use them within the instruction? I would appreciate a prompt response. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I’m trying to understand how to properly enable context caching for a custom agent.
Specifically, I’d like to know:
My confusion comes from the fact that a custom agent does not have an instruction field. Because of this, I’m not sure whether caching is applied to the sub-agents inside the custom agent. I also tried converting my custom agent into an app and setting a ContextCacheConfig, but I wasn’t able to observe any cached values being generated.
I’ve searched through the Google ADK documentation and related materials, but I couldn’t find any examples that address this scenario.
If anyone has successfully set up caching for a custom agent—or can confirm that it’s not supported—I’d appreciate your insights.
Additional Questions
How does context caching behave when an agent’s instruction includes state values in the {key} placeholder format?
Specifically, does caching exclude only the {key} value itself?
Or is caching applied only up to the portion of the instruction that precedes the placeholder?
Or does the presence of {key} prevent caching from occurring at all?
A related question: how does caching behave when placeholders are used in a different way—for example, using Python-side variable replacement rather than the {key} state placeholder mechanism?
In other words, if I manually inject values into the instruction string (instead of relying on the agent’s state-based {key} placeholders), does context caching work differently?
Beta Was this translation helpful? Give feedback.
All reactions