Class GoogleGenAiCaches
java.lang.Object
dev.langchain4j.model.google.genai.GoogleGenAiCaches
Service for creating and managing Gemini
context caches using the official
com.google.genai SDK.
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 (CachedContent.name()) can be supplied as the
cachedContent of a GoogleGenAiChatModel (or its streaming/batch counterparts) to
consume the cached context.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic GoogleGenAiCaches.Builderbuilder()com.google.genai.types.CachedContentcreateCache(String modelName, List<ChatMessage> messages, Duration ttl) Creates a context cache for the given model from the supplied messages.voiddeleteCache(String name) Deletes a context cache by name.com.google.genai.types.CachedContentRetrieves a cache by name (e.g.List<com.google.genai.types.CachedContent> Lists all context caches for the current project.com.google.genai.types.CachedContentupdateCacheTtl(String name, Duration ttl) Updates a cache's time-to-live and returns the updated cache.
-
Method Details
-
builder
-
createCache
public com.google.genai.types.CachedContent createCache(String modelName, List<ChatMessage> messages, Duration ttl) Creates a context cache for the given model from the supplied messages.- 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- optional time-to-live for the cache (e.g.Duration.ofHours(1)); may benull, in which case the API default is used- Returns:
- the created cache, whose
CachedContent.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. -
updateCacheTtl
-
deleteCache
Deletes a context cache by name.
-