Skip to content

Commit 12defee

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: introduces context caching configuration for apps, ported from Python ADK
This change introduces ContextCacheConfig, a configuration class for enabling and controlling context caching across all agents within an ADK application. The App class is updated to include an optional ContextCacheConfig, allowing applications to specify caching parameters such as cache intervals, TTL, and minimum token thresholds for caching. PiperOrigin-RevId: 866030713
1 parent 76f86c5 commit 12defee

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.adk.agents;
17+
18+
import java.time.Duration;
19+
20+
/**
21+
* Configuration for context caching across all agents in an app.
22+
*
23+
* <p>This configuration enables and controls context caching behavior for all LLM agents in an app.
24+
* When this config is present on an app, context caching is enabled for all agents. When absent
25+
* (null), context caching is disabled.
26+
*
27+
* <p>Context caching can significantly reduce costs and improve response times by reusing
28+
* previously processed context across multiple requests.
29+
*
30+
* @param maxInvocations Maximum number of invocations to reuse the same cache before refreshing it.
31+
* Defaults to 10.
32+
* @param ttl Time-to-live for cache. Defaults to 1800 seconds (30 minutes).
33+
* @param minTokens Minimum estimated request tokens required to enable caching. This compares
34+
* against the estimated total tokens of the request (system instruction + tools + contents).
35+
* Context cache storage may have cost. Set higher to avoid caching small requests where
36+
* overhead may exceed benefits. Defaults to 0.
37+
*/
38+
public record ContextCacheConfig(int maxInvocations, Duration ttl, int minTokens) {
39+
40+
public ContextCacheConfig() {
41+
this(10, Duration.ofSeconds(1800), 0);
42+
}
43+
44+
/** Returns TTL as string format for cache creation. */
45+
public String getTtlString() {
46+
return ttl.getSeconds() + "s";
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "ContextCacheConfig(maxInvocations="
52+
+ maxInvocations
53+
+ ", ttl="
54+
+ ttl.getSeconds()
55+
+ "s, minTokens="
56+
+ minTokens
57+
+ ")";
58+
}
59+
}

core/src/main/java/com/google/adk/apps/App.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.adk.apps;
1818

1919
import com.google.adk.agents.BaseAgent;
20+
import com.google.adk.agents.ContextCacheConfig;
2021
import com.google.adk.plugins.Plugin;
2122
import com.google.adk.summarizer.EventsCompactionConfig;
2223
import com.google.common.collect.ImmutableList;
@@ -41,18 +42,21 @@ public class App {
4142
private final ImmutableList<? extends Plugin> plugins;
4243
@Nullable private final EventsCompactionConfig eventsCompactionConfig;
4344
@Nullable private final ResumabilityConfig resumabilityConfig;
45+
@Nullable private final ContextCacheConfig contextCacheConfig;
4446

4547
private App(
4648
String name,
4749
BaseAgent rootAgent,
4850
List<? extends Plugin> plugins,
4951
@Nullable EventsCompactionConfig eventsCompactionConfig,
50-
@Nullable ResumabilityConfig resumabilityConfig) {
52+
@Nullable ResumabilityConfig resumabilityConfig,
53+
@Nullable ContextCacheConfig contextCacheConfig) {
5154
this.name = name;
5255
this.rootAgent = rootAgent;
5356
this.plugins = ImmutableList.copyOf(plugins);
5457
this.eventsCompactionConfig = eventsCompactionConfig;
5558
this.resumabilityConfig = resumabilityConfig;
59+
this.contextCacheConfig = contextCacheConfig;
5660
}
5761

5862
public String name() {
@@ -77,13 +81,19 @@ public ResumabilityConfig resumabilityConfig() {
7781
return resumabilityConfig;
7882
}
7983

84+
@Nullable
85+
public ContextCacheConfig contextCacheConfig() {
86+
return contextCacheConfig;
87+
}
88+
8089
/** Builder for {@link App}. */
8190
public static class Builder {
8291
private String name;
8392
private BaseAgent rootAgent;
8493
private List<? extends Plugin> plugins = ImmutableList.of();
8594
@Nullable private EventsCompactionConfig eventsCompactionConfig;
8695
@Nullable private ResumabilityConfig resumabilityConfig;
96+
@Nullable private ContextCacheConfig contextCacheConfig;
8797

8898
@CanIgnoreReturnValue
8999
public Builder name(String name) {
@@ -115,6 +125,12 @@ public Builder resumabilityConfig(ResumabilityConfig resumabilityConfig) {
115125
return this;
116126
}
117127

128+
@CanIgnoreReturnValue
129+
public Builder contextCacheConfig(ContextCacheConfig contextCacheConfig) {
130+
this.contextCacheConfig = contextCacheConfig;
131+
return this;
132+
}
133+
118134
public App build() {
119135
if (name == null) {
120136
throw new IllegalStateException("App name must be provided.");
@@ -123,7 +139,8 @@ public App build() {
123139
throw new IllegalStateException("Root agent must be provided.");
124140
}
125141
validateAppName(name);
126-
return new App(name, rootAgent, plugins, eventsCompactionConfig, resumabilityConfig);
142+
return new App(
143+
name, rootAgent, plugins, eventsCompactionConfig, resumabilityConfig, contextCacheConfig);
127144
}
128145
}
129146

0 commit comments

Comments
 (0)