Class GeminiCaches
java.lang.Object
dev.langchain4j.model.googleai.GeminiCaches
Service for creating and managing Gemini
context caches.
Context caching stores large, frequently reused context (a system instruction, long documents, PDFs) on Google's servers once, so subsequent requests can reference it by name instead of resending it, reducing input-token cost and latency.
The name of a created cache (GeminiCaches.GeminiCachedContent.name()) can be supplied as the
cachedContentName of a GoogleAiGeminiChatModel (or its streaming counterpart) to
consume the cached context.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic final recordRepresents a Gemini context cache, documentation -
Method Summary
Modifier and TypeMethodDescriptionstatic GeminiCaches.Builderbuilder()createCache(String modelName, List<ChatMessage> messages) Creates a context cache for the given model from the supplied messages, using the API's default expiration (currently 1 hour).createCache(String modelName, List<ChatMessage> messages, Duration ttl) Creates a context cache for the given model from the supplied messages, expiring after the given time-to-live.createCache(String modelName, List<ChatMessage> messages, Instant expireTime) Creates a context cache for the given model from the supplied messages, expiring at the given instant.voiddeleteCache(String name) Deletes a context cache by name.Retrieves a cache by name (e.g.Lists all context caches for the current project.
-
Method Details
-
builder
-
createCache
Creates a context cache for the given model from the supplied messages, using the API's default expiration (currently 1 hour).- Parameters:
modelName- the model the cache is bound to (e.g."gemini-2.5-flash")messages- the messages to cache; aSystemMessagebecomes the cached system instruction, the rest become cached contents- Returns:
- the created cache, whose
GeminiCaches.GeminiCachedContent.name()is used to consume it
-
createCache
public GeminiCaches.GeminiCachedContent createCache(String modelName, List<ChatMessage> messages, Duration ttl) Creates a context cache for the given model from the supplied messages, expiring after the given time-to-live.- Parameters:
modelName- the model the cache is bound to (e.g."gemini-2.5-flash")messages- the messages to cache; aSystemMessagebecomes the cached system instruction, the rest become cached contentsttl- the relative time-to-live for the cache (e.g.Duration.ofHours(1))- Returns:
- the created cache, whose
GeminiCaches.GeminiCachedContent.name()is used to consume it
-
createCache
public GeminiCaches.GeminiCachedContent createCache(String modelName, List<ChatMessage> messages, Instant expireTime) Creates a context cache for the given model from the supplied messages, expiring at the given instant.- Parameters:
modelName- the model the cache is bound to (e.g."gemini-2.5-flash")messages- the messages to cache; aSystemMessagebecomes the cached system instruction, the rest become cached contentsexpireTime- the instant at which the cache expires- Returns:
- the created cache, whose
GeminiCaches.GeminiCachedContent.name()is used to consume it
-
getCache
Retrieves a cache by name (e.g."cachedContents/abc123"). -
listCaches
Lists all context caches for the current project. -
deleteCache
Deletes a context cache by name.
-