Class TokenUsage
java.lang.Object
dev.langchain4j.model.output.TokenUsage
- Direct Known Subclasses:
AnthropicTokenUsage, OpenAiOfficialTokenUsage, OpenAiTokenUsage
Represents the token usage of a response.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newTokenUsageinstance with all fields set to null.TokenUsage(Integer inputTokenCount) Creates a newTokenUsageinstance with the given input token count.TokenUsage(Integer inputTokenCount, Integer outputTokenCount) Creates a newTokenUsageinstance with the given input and output token counts.TokenUsage(Integer inputTokenCount, Integer outputTokenCount, Integer totalTokenCount) Creates a newTokenUsageinstance with the given input, output and total token counts. -
Method Summary
Modifier and TypeMethodDescriptionadd(TokenUsage that) Adds the token usage of two responses together.booleaninthashCode()Returns the input token count, or null if unknown.Returns the output token count, or null if unknown.static TokenUsagesum(TokenUsage first, TokenUsage second) Adds two token usages.protected static IntegerSum two integers, returning null if both are null.toString()Returns the total token count, or null if unknown.
-
Constructor Details
-
TokenUsage
public TokenUsage()Creates a newTokenUsageinstance with all fields set to null. -
TokenUsage
Creates a newTokenUsageinstance with the given input token count.- Parameters:
inputTokenCount- The input token count.
-
TokenUsage
Creates a newTokenUsageinstance with the given input and output token counts.- Parameters:
inputTokenCount- The input token count, or null if unknown.outputTokenCount- The output token count, or null if unknown.
-
TokenUsage
Creates a newTokenUsageinstance with the given input, output and total token counts.- Parameters:
inputTokenCount- The input token count, or null if unknown.outputTokenCount- The output token count, or null if unknown.totalTokenCount- The total token count, or null if unknown.
-
-
Method Details
-
inputTokenCount
Returns the input token count, or null if unknown.- Returns:
- the input token count, or null if unknown.
-
outputTokenCount
Returns the output token count, or null if unknown.- Returns:
- the output token count, or null if unknown.
-
totalTokenCount
Returns the total token count, or null if unknown.- Returns:
- the total token count, or null if unknown.
-
sum
Adds two token usages.
If one of the token usages is null, the other is returned without changes.
Fields which are null in both responses will be null in the result.- Parameters:
first- The first token usage to add.second- The second token usage to add.- Returns:
- a new
TokenUsageinstance with the sum of token usages.
-
add
Adds the token usage of two responses together.Fields which are null in both responses will be null in the result.
- Parameters:
that- The token usage to add to this one.- Returns:
- a new
TokenUsageinstance with the token usage of both responses added together.
-
sum
-
equals
-
hashCode
-
toString
-