mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
718bcf5c6d
* Document how to check whether the context cache was used The caching page explained how to turn caching on and never how to tell whether it is working. CacheMetadata has been available since adk-kotlin 0.6.0 and is undocumented: adk-python has fourteen code references to cache_metadata, while adk-docs mentions it twice, both incidental -- a BigQuery schema table and a bullet in the Live dev guide. The snippet reads it from Event.cacheMetadata and covers both states the type can be in, because the constructor enforces the split: cacheName, expireTime and invocationsUsed must either all be set (an active cache) or all be null (the fingerprint-only state used for prefix matching before a cache exists). Notes that token counts live on LlmResponse.usageMetadata rather than here, which the KDoc calls out to avoid duplication and a reader would otherwise reasonably look for on CacheMetadata. Badged Kotlin v0.6.0 and verified rather than assumed: the snippet compiles against a temporary 0.6.0 pin as well as the current 0.7.0 one. Transcluded and registered, so CI compiles and lints it. Exercised all four paths with synthetic events: no metadata, fingerprint-only, active, and active with expireSoon true. * Correct when CacheMetadata is present on an event The section claimed every event backed by an LLM response carries a CacheMetadata. It does not: LlmResponse.cacheMetadata is null when caching is disabled and also when the call produced no cache information, so the claim was wrong even with caching on. Say "can carry", name both null cases, and explain why the snippet checks before reading. The snippet's own comment made the same overstatement. * docs: clarify the behavior of expireSoon and cache status in documentation and snippets
165 lines
6.7 KiB
Markdown
165 lines
6.7 KiB
Markdown
# Context caching with Gemini
|
|
|
|
<div class="language-support-tag">
|
|
<span class="lst-supported">Supported in ADK</span><span class="lst-python">Python v1.15.0</span><span class="lst-java">Java v0.1.0</span><span class="lst-kotlin">Kotlin v0.7.0</span>
|
|
</div>
|
|
|
|
When working with agents to complete tasks, you may want to reuse extended
|
|
instructions or large sets of data across multiple agent requests to a
|
|
generative AI model. Resending this data for each agent request is slow,
|
|
inefficient, and can be expensive. Using context caching features in generative
|
|
AI models can significantly speed up responses and lower the number of tokens
|
|
sent to the model for each request.
|
|
|
|
The ADK Context Caching feature allows you to cache request data with generative
|
|
AI models that support it, including Gemini 2.0 and higher models. This document
|
|
explains how to configure and use this feature.
|
|
|
|
## Configure context caching
|
|
|
|
You configure the context caching feature at the ADK `App` object level,
|
|
which wraps your agent. Use the `ContextCacheConfig` class to configure
|
|
these settings, as shown in the following code sample:
|
|
|
|
=== "Python"
|
|
|
|
```python
|
|
from google.adk import Agent
|
|
from google.adk.apps.app import App
|
|
from google.adk.agents.context_cache_config import ContextCacheConfig
|
|
|
|
root_agent = Agent(
|
|
# configure an agent using Gemini 2.0 or higher
|
|
)
|
|
|
|
# Create the app with context caching configuration
|
|
app = App(
|
|
name='my-caching-agent-app',
|
|
root_agent=root_agent,
|
|
context_cache_config=ContextCacheConfig(
|
|
min_tokens=2048, # Minimum tokens to trigger caching
|
|
ttl_seconds=600, # Store for up to 10 minutes
|
|
cache_intervals=5, # Refresh after 5 uses
|
|
),
|
|
)
|
|
```
|
|
|
|
=== "Java"
|
|
|
|
```java
|
|
import com.google.adk.agents.BaseAgent;
|
|
import com.google.adk.agents.ContextCacheConfig;
|
|
import com.google.adk.apps.App;
|
|
import java.time.Duration;
|
|
|
|
// Create the app with context caching configuration
|
|
App app = App.builder()
|
|
.name("my-caching-agent-app")
|
|
.rootAgent(rootAgent)
|
|
.contextCacheConfig(
|
|
new ContextCacheConfig(
|
|
5, /* cache_intervals (max invocations) */
|
|
Duration.ofMinutes(10), /* ttl */
|
|
2048 /* min_tokens */))
|
|
.build();
|
|
```
|
|
|
|
=== "Kotlin"
|
|
|
|
```kotlin
|
|
import com.google.adk.kt.agents.ContextCacheConfig
|
|
import com.google.adk.kt.agents.LlmAgent
|
|
import com.google.adk.kt.annotations.ExperimentalContextCachingFeature
|
|
import com.google.adk.kt.apps.App
|
|
import com.google.adk.kt.models.Gemini
|
|
import com.google.adk.kt.types.HttpOptions
|
|
import kotlin.time.Duration.Companion.minutes
|
|
import kotlin.time.Duration.Companion.seconds
|
|
|
|
val rootAgent =
|
|
LlmAgent(
|
|
name = "my_caching_agent",
|
|
// configure an agent using Gemini 2.0 or higher
|
|
model = Gemini(name = "gemini-flash-latest"),
|
|
)
|
|
|
|
// Create the app with context caching configuration
|
|
@OptIn(ExperimentalContextCachingFeature::class)
|
|
val app =
|
|
App(
|
|
appName = "my-caching-agent-app",
|
|
rootAgent = rootAgent,
|
|
contextCacheConfig =
|
|
ContextCacheConfig(
|
|
// Gemini applies its own minimum cacheable size, which varies by model
|
|
minTokens = 8192,
|
|
ttl = 10.minutes, // Store for up to 10 minutes
|
|
cacheIntervals = 5, // Refresh after 5 uses
|
|
// On timeout the create fails and the request proceeds uncached.
|
|
createHttpOptions = HttpOptions(timeout = 10.seconds),
|
|
),
|
|
)
|
|
```
|
|
|
|
## Configuration settings
|
|
|
|
The `ContextCacheConfig` class has the following settings that control how
|
|
caching works for your agent. When you configure these settings, they apply to
|
|
all agents within your app.
|
|
|
|
- **`min_tokens`** (int): The minimum number of tokens required in a request
|
|
to enable caching. This setting allows you to avoid the overhead of caching
|
|
for very small requests where the performance benefit would be negligible.
|
|
Defaults to `0`.
|
|
- **`ttl_seconds`** (int): The time-to-live (TTL) for the cache in seconds.
|
|
This setting determines how long the cached content is stored before it is
|
|
refreshed. Defaults to `1800` (30 minutes).
|
|
- **`cache_intervals`** (int): The maximum number of times the same cached
|
|
content can be used before it expires. This setting allows you to
|
|
control how frequently the cache is updated, even if the TTL has not
|
|
expired. Defaults to `10`.
|
|
- **`create_http_options`** (HttpOptions): The HTTP options for the cache
|
|
creation call, which lets you set a timeout on it. If the call times out,
|
|
it fails and the request proceeds without caching. Available in Python and
|
|
Kotlin; defaults to none.
|
|
|
|
## Check whether the cache is being used
|
|
|
|
<div class="language-support-tag">
|
|
<span class="lst-supported">Supported in ADK</span><span class="lst-kotlin">Kotlin v0.6.0</span>
|
|
</div>
|
|
|
|
When caching is enabled, an event backed by an LLM response can carry a
|
|
`CacheMetadata` reporting what the cache did for that call. It is null when
|
|
caching is disabled, and also when the call produced no cache information, so
|
|
check for it before reading it. When present it has two states: an **active
|
|
cache**, where `cacheName`, `expireTime` and `invocationsUsed` are all set, and
|
|
a **fingerprint-only** state, where all three are null.
|
|
|
|
```kotlin
|
|
--8<-- "examples/kotlin/snippets/context/CacheMetadataExample.kt:cache_metadata"
|
|
```
|
|
|
|
`expireSoon` means the cache expires within about two minutes, or has already
|
|
expired. It is a signal for your own code, not something ADK acts on: ADK keeps
|
|
reusing a cache until it is actually past `expireTime`, has run past
|
|
`cacheIntervals`, or its cached prefix changes.
|
|
|
|
Token counts are not on `CacheMetadata`; read them from `LlmResponse.usageMetadata`.
|
|
|
|
## Next steps
|
|
|
|
For a full implementation of how to use and test the context caching feature,
|
|
see the following sample:
|
|
|
|
- [`cache_analysis`](https://github.com/google/adk-python/tree/main/contributing/samples/context_management/cache_analysis):
|
|
A code sample that demonstrates how to analyze the performance of context
|
|
caching.
|
|
|
|
If your use case requires that you provide instructions that are used throughout
|
|
a session, consider using the `static_instruction` parameter for an agent, which
|
|
allows you to amend the system instructions for a generative model. For more
|
|
details, see this sample code:
|
|
|
|
- [`static_instruction`](https://github.com/google/adk-python/tree/main/contributing/samples/context_management/static_instruction):
|
|
An implementation of a digital pet agent using static instructions. |