Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions core/src/main/java/com/google/adk/agents/ContextCacheConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package com.google.adk.agents;

import com.google.errorprone.annotations.InlineMe;
import com.google.genai.types.HttpOptions;
import java.time.Duration;
import org.jspecify.annotations.Nullable;

/**
* Configuration for context caching across all agents in an app.
Expand All @@ -27,33 +30,55 @@
* <p>Context caching can significantly reduce costs and improve response times by reusing
* previously processed context across multiple requests.
*
* @param maxInvocations Maximum number of invocations to reuse the same cache before refreshing it.
* @param cacheIntervals Maximum number of invocations to reuse the same cache before refreshing it.
* Defaults to 10.
* @param ttl Time-to-live for cache. Defaults to 1800 seconds (30 minutes).
* @param minTokens Minimum estimated request tokens required to enable caching. This compares
* against the estimated total tokens of the request (system instruction + tools + contents).
* Context cache storage may have cost. Set higher to avoid caching small requests where
* overhead may exceed benefits. Defaults to 0.
* @param createHttpOptions HTTP options, such as a timeout, for the call that creates a cache; null
* uses the client's defaults. Defaults to null.
*/
public record ContextCacheConfig(int maxInvocations, Duration ttl, int minTokens) {
public record ContextCacheConfig(
int cacheIntervals, Duration ttl, int minTokens, @Nullable HttpOptions createHttpOptions) {

public ContextCacheConfig() {
this(10, Duration.ofSeconds(1800), 0);
this(10, Duration.ofMinutes(30), 0);
}

/** Creates a config that creates caches with the client's default HTTP options. */
public ContextCacheConfig(int cacheIntervals, Duration ttl, int minTokens) {
this(cacheIntervals, ttl, minTokens, /* createHttpOptions= */ null);
}

/**
* Returns {@link #cacheIntervals()}.
*
* @deprecated Use {@link #cacheIntervals()}, the name ADK Python and ADK Kotlin use.
*/
@Deprecated
@InlineMe(replacement = "this.cacheIntervals()")
public int maxInvocations() {
return cacheIntervals();
}

/** Returns TTL as string format for cache creation. */
public String getTtlString() {
return ttl.getSeconds() + "s";
return ttl.toSeconds() + "s";
}

@Override
public String toString() {
return "ContextCacheConfig(maxInvocations="
+ maxInvocations
// Says only whether HTTP options are set, since their headers can carry credentials.
return "ContextCacheConfig(cacheIntervals="
+ cacheIntervals
+ ", ttl="
+ ttl.getSeconds()
+ ttl.toSeconds()
+ "s, minTokens="
+ minTokens
+ ", createHttpOptions="
+ (createHttpOptions == null ? "null" : "set")
+ ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.adk.agents;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableMap;
import com.google.genai.types.HttpOptions;
import java.time.Duration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class ContextCacheConfigTest {

private static final Duration TTL = Duration.ofMinutes(30);

@Test
public void defaultConstructor_usesDefaults() {
ContextCacheConfig config = new ContextCacheConfig();

assertThat(config.cacheIntervals()).isEqualTo(10);
assertThat(config.ttl()).isEqualTo(Duration.ofMinutes(30));
assertThat(config.minTokens()).isEqualTo(0);
assertThat(config.createHttpOptions()).isNull();
}

@Test
public void threeArgConstructor_leavesHttpOptionsUnset() {
ContextCacheConfig config = new ContextCacheConfig(5, TTL, 100);

assertThat(config.cacheIntervals()).isEqualTo(5);
assertThat(config.createHttpOptions()).isNull();
}

@Test
public void constructor_keepsHttpOptions() {
HttpOptions httpOptions = HttpOptions.builder().timeout(10_000).build();

assertThat(new ContextCacheConfig(5, TTL, 100, httpOptions).createHttpOptions())
.isEqualTo(httpOptions);
}

@Test
@SuppressWarnings({"deprecation", "InlineMeInliner"}) // Covers the deprecated alias.
public void maxInvocations_returnsCacheIntervals() {
assertThat(new ContextCacheConfig(7, TTL, 0).maxInvocations()).isEqualTo(7);
}

@Test
public void toString_withoutHttpOptions_saysNull() {
assertThat(new ContextCacheConfig().toString())
.isEqualTo(
"ContextCacheConfig(cacheIntervals=10, ttl=1800s, minTokens=0,"
+ " createHttpOptions=null)");
}

@Test
public void toString_hidesHttpOptionValues() {
HttpOptions httpOptions =
HttpOptions.builder().headers(ImmutableMap.of("x-goog-api-key", "secret")).build();

assertThat(new ContextCacheConfig(5, TTL, 100, httpOptions).toString())
.isEqualTo(
"ContextCacheConfig(cacheIntervals=5, ttl=1800s, minTokens=100,"
+ " createHttpOptions=set)");
}
}
Loading