Class GoogleGenAiTokenUsage
java.lang.Object
dev.langchain4j.model.output.TokenUsage
dev.langchain4j.model.google.genai.GoogleGenAiTokenUsage
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();
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionadd(TokenUsage that) Adds the token usage of two responses together.builder()Returns the number of tokens read from the cached content, ornullif the model did not report it.booleaninthashCode()Returns the number of tokens the model generated while thinking, ornullif the model did not report it.Returns the number of tokens in the tool results that were fed back to the model as input, ornullif the model did not report it.toString()Methods inherited from class TokenUsage
inputTokenCount, outputTokenCount, sum, sum, totalTokenCount
-
Method Details
-
cachedContentTokenCount
Returns the number of tokens read from the cached content, ornullif the model did not report it. These tokens are already included inTokenUsage.inputTokenCount().- Returns:
- the cached content token count
-
thoughtsTokenCount
Returns the number of tokens the model generated while thinking, ornullif the model did not report it. These tokens are generated and billed in addition toTokenUsage.outputTokenCount().- Returns:
- the thoughts token count
-
toolUsePromptTokenCount
Returns the number of tokens in the tool results that were fed back to the model as input, ornullif the model did not report it. These tokens are not included inTokenUsage.inputTokenCount(); Gemini reports them as a separate part of the total.- Returns:
- the tool use prompt token count
-
add
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
thatis aGoogleGenAiTokenUsageas well; otherwise the counts of this instance are kept.- Overrides:
addin classTokenUsage- Parameters:
that- The token usage to add to this one.- Returns:
- a new
TokenUsageinstance with the token usage of both responses added together.
-
equals
- Overrides:
equalsin classTokenUsage
-
hashCode
public int hashCode()- Overrides:
hashCodein classTokenUsage
-
toString
- Overrides:
toStringin classTokenUsage
-
builder
-