Class GeminiCaches

java.lang.Object
dev.langchain4j.model.googleai.GeminiCaches

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

  • Method Details

    • builder

      public static GeminiCaches.Builder builder()
    • createCache

      public GeminiCaches.GeminiCachedContent 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).
      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
      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; a SystemMessage becomes the cached system instruction, the rest become cached contents
      ttl - 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; a SystemMessage becomes the cached system instruction, the rest become cached contents
      expireTime - the instant at which the cache expires
      Returns:
      the created cache, whose GeminiCaches.GeminiCachedContent.name() is used to consume it
    • getCache

      public GeminiCaches.GeminiCachedContent getCache(String name)
      Retrieves a cache by name (e.g. "cachedContents/abc123").
    • listCaches

      public List<GeminiCaches.GeminiCachedContent> listCaches()
      Lists all context caches for the current project.
    • deleteCache

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