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")
+ ")";
}
}
29 changes: 28 additions & 1 deletion core/src/main/java/com/google/adk/events/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.adk.JsonBaseModel;
import com.google.adk.annotations.Experimental;
import com.google.adk.models.CacheMetadata;
import com.google.adk.platform.UuidProvider;
import com.google.adk.workflow.NodeInfo;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -70,6 +71,7 @@ public class Event extends JsonBaseModel {
private @Nullable Transcription outputTranscription;
private @Nullable Object output;
private @Nullable NodeInfo nodeInfo;
private @Nullable CacheMetadata cacheMetadata;

private long timestamp;

Expand Down Expand Up @@ -342,6 +344,19 @@ public void setNodeInfo(@Nullable NodeInfo nodeInfo) {
this.nodeInfo = nodeInfo;
}

/**
* Context cache state of the LLM response this event carries. The next request of the same agent
* reads it to reuse or refresh the cache.
*/
@JsonProperty("cacheMetadata")
public Optional<CacheMetadata> cacheMetadata() {
return Optional.ofNullable(cacheMetadata);
}

public void setCacheMetadata(@Nullable CacheMetadata cacheMetadata) {
this.cacheMetadata = cacheMetadata;
}

/** The timestamp of the event. */
@JsonProperty("timestamp")
public long timestamp() {
Expand Down Expand Up @@ -453,6 +468,7 @@ public static class Builder {
private @Nullable Transcription outputTranscription;
private @Nullable Object output;
private @Nullable NodeInfo nodeInfo;
private @Nullable CacheMetadata cacheMetadata;
private @Nullable Long timestamp;

@JsonCreator
Expand Down Expand Up @@ -646,6 +662,13 @@ public Builder nodeInfo(@Nullable NodeInfo value) {
return this;
}

@CanIgnoreReturnValue
@JsonProperty("cacheMetadata")
public Builder cacheMetadata(@Nullable CacheMetadata value) {
this.cacheMetadata = value;
return this;
}

public Event build() {
Event event = new Event();
event.setId(id);
Expand All @@ -672,6 +695,7 @@ public Event build() {
event.setOutputTranscription(outputTranscription);
event.setOutput(output);
event.setNodeInfo(nodeInfo);
event.setCacheMetadata(cacheMetadata);
return event;
}
}
Expand Down Expand Up @@ -710,6 +734,7 @@ public Builder toBuilder() {
.outputTranscription(this.outputTranscription)
.output(this.output)
.nodeInfo(this.nodeInfo)
.cacheMetadata(this.cacheMetadata)
.timestamp(this.timestamp);
}

Expand Down Expand Up @@ -743,7 +768,8 @@ public boolean equals(Object obj) {
&& Objects.equals(inputTranscription, other.inputTranscription)
&& Objects.equals(outputTranscription, other.outputTranscription)
&& Objects.equals(output, other.output)
&& Objects.equals(nodeInfo, other.nodeInfo);
&& Objects.equals(nodeInfo, other.nodeInfo)
&& Objects.equals(cacheMetadata, other.cacheMetadata);
}

@Override
Expand Down Expand Up @@ -776,6 +802,7 @@ public int hashCode() {
outputTranscription,
output,
nodeInfo,
cacheMetadata,
timestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ private Event buildModelResponseEvent(
.usageMetadata(llmResponse.usageMetadata().orElse(null))
.modelVersion(llmResponse.modelVersion().orElse(null))
.inputTranscription(llmResponse.inputTranscription().orElse(null))
.outputTranscription(llmResponse.outputTranscription().orElse(null));
.outputTranscription(llmResponse.outputTranscription().orElse(null))
.cacheMetadata(llmResponse.cacheMetadata().orElse(null));

Event event = eventBuilder.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.flows.llmflows;

import com.google.adk.agents.ContextCacheConfig;
import com.google.adk.agents.InvocationContext;
import com.google.adk.events.Event;
import com.google.adk.models.CacheMetadata;
import com.google.adk.models.LlmRequest;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.genai.types.GenerateContentResponseUsageMetadata;
import io.reactivex.rxjava3.core.Single;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* {@link RequestProcessor} that enables context caching when the app configures it. It puts the
* config, the agent's latest cache metadata and its previous prompt token count on the request; the
* model creates, reuses and deletes the caches.
*/
final class ContextCacheRequestProcessor implements RequestProcessor {

private static final Logger logger = LoggerFactory.getLogger(ContextCacheRequestProcessor.class);

@Override
public Single<RequestProcessingResult> processRequest(
InvocationContext context, LlmRequest request) {
Optional<ContextCacheConfig> cacheConfig = context.contextCacheConfig();
if (cacheConfig.isEmpty()) {
return Single.just(RequestProcessingResult.create(request, ImmutableList.of()));
}

String agentName = context.agent().name();
CacheMetadata cacheMetadata = null;
Integer previousTokenCount = null;
for (Event event : context.session().immutableEvents().reverse()) {
if (!agentName.equals(event.author())) {
continue;
}
if (cacheMetadata == null && event.cacheMetadata().isPresent()) {
cacheMetadata = countInvocation(event, context.invocationId());
}
if (previousTokenCount == null) {
previousTokenCount =
event
.usageMetadata()
.flatMap(GenerateContentResponseUsageMetadata::promptTokenCount)
.orElse(null);
}
if (cacheMetadata != null && previousTokenCount != null) {
break;
}
}
if (cacheMetadata != null) {
logger.debug("Found cache metadata for agent {}: {}", agentName, cacheMetadata);
}
if (previousTokenCount != null) {
logger.debug(
"Found previous prompt token count for agent {}: {}", agentName, previousTokenCount);
}
logger.debug("Context caching enabled for agent {}", agentName);

LlmRequest updatedRequest =
request.toBuilder()
.cacheConfig(cacheConfig.get())
.cacheMetadata(cacheMetadata)
.cacheableContentsTokenCount(previousTokenCount)
.build();
return Single.just(RequestProcessingResult.create(updatedRequest, ImmutableList.of()));
}

/**
* Returns the event's cache metadata, counting one more use when it names an active cache from an
* earlier invocation.
*/
private static CacheMetadata countInvocation(Event event, String invocationId) {
CacheMetadata metadata = event.cacheMetadata().get();
if (Strings.isNullOrEmpty(event.invocationId())
|| event.invocationId().equals(invocationId)
|| metadata.cacheName().isEmpty()) {
return metadata;
}
return metadata.toBuilder().invocationsUsed(metadata.invocationsUsed().get() + 1).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SingleFlow extends BaseLlmFlow {
new Identity(),
new Compaction(),
new Contents(),
new ContextCacheRequestProcessor(),
CodeExecution.requestProcessor);

protected static final ImmutableList<ResponseProcessor> RESPONSE_PROCESSORS =
Expand Down
Loading
Loading