Class GoogleGenAiCaches

java.lang.Object
dev.langchain4j.model.google.genai.GoogleGenAiCaches

public final class GoogleGenAiCaches extends Object
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.

  • Method Details

    • builder

      public static GoogleGenAiCaches.Builder 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; a SystemMessage becomes the cached system instruction, the rest become cached contents
      ttl - optional time-to-live for the cache (e.g. Duration.ofHours(1)); may be null, in which case the API default is used
      Returns:
      the created cache, whose CachedContent.name() is used to consume it
    • getCache

      public com.google.genai.types.CachedContent getCache(String name)
      Retrieves a cache by name (e.g. "cachedContents/abc123").
    • listCaches

      public List<com.google.genai.types.CachedContent> listCaches()
      Lists all context caches for the current project.
    • updateCacheTtl

      public com.google.genai.types.CachedContent updateCacheTtl(String name, Duration ttl)
      Updates a cache's time-to-live and returns the updated cache.
    • deleteCache

      public void deleteCache(String name)
      Deletes a context cache by name.