Class GoogleGenAiTokenUsage

java.lang.Object
dev.langchain4j.model.output.TokenUsage
dev.langchain4j.model.google.genai.GoogleGenAiTokenUsage

public class GoogleGenAiTokenUsage extends TokenUsage
Google GenAI-specific TokenUsage that additionally exposes the token counts reported by Gemini for cached content, for thinking and for tool results fed back to the model.

cachedContentTokenCount() is a subset of TokenUsage.inputTokenCount(), whereas toolUsePromptTokenCount() and thoughtsTokenCount() are counted on top of it: Gemini defines the total as input + output + tool use prompt + thoughts. None of the three can be derived from the inherited counts. See thinking and context caching.

Instances are returned as the tokenUsage of GoogleGenAiChatResponseMetadata:

GoogleGenAiTokenUsage tokenUsage = (GoogleGenAiTokenUsage) chatResponse.metadata().tokenUsage();
Integer thoughtsTokenCount = tokenUsage.thoughtsTokenCount();
  • Method Details

    • cachedContentTokenCount

      public Integer cachedContentTokenCount()
      Returns the number of tokens read from the cached content, or null if the model did not report it. These tokens are already included in TokenUsage.inputTokenCount().
      Returns:
      the cached content token count
    • thoughtsTokenCount

      public Integer thoughtsTokenCount()
      Returns the number of tokens the model generated while thinking, or null if the model did not report it. These tokens are generated and billed in addition to TokenUsage.outputTokenCount().
      Returns:
      the thoughts token count
    • toolUsePromptTokenCount

      public Integer toolUsePromptTokenCount()
      Returns the number of tokens in the tool results that were fed back to the model as input, or null if the model did not report it. These tokens are not included in TokenUsage.inputTokenCount(); Gemini reports them as a separate part of the total.
      Returns:
      the tool use prompt token count
    • add

      public GoogleGenAiTokenUsage add(TokenUsage that)
      Adds the token usage of two responses together.

      Fields which are null in both responses will be null in the result.

      The cached content, thoughts and tool use prompt token counts are summed only when that is a GoogleGenAiTokenUsage as well; otherwise the counts of this instance are kept.

      Overrides:
      add in class TokenUsage
      Parameters:
      that - The token usage to add to this one.
      Returns:
      a new TokenUsage instance with the token usage of both responses added together.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class TokenUsage
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class TokenUsage
    • toString

      public String toString()
      Overrides:
      toString in class TokenUsage
    • builder

      public static GoogleGenAiTokenUsage.Builder builder()