Class TokenUsage

java.lang.Object
dev.langchain4j.model.output.TokenUsage
Direct Known Subclasses:
AnthropicTokenUsage

public class TokenUsage extends Object
Represents the token usage of a response.
  • Constructor Details

    • TokenUsage

      public TokenUsage()
      Creates a new TokenUsage instance with all fields set to null.
    • TokenUsage

      public TokenUsage(Integer inputTokenCount)
      Creates a new TokenUsage instance with the given input token count.
      Parameters:
      inputTokenCount - The input token count.
    • TokenUsage

      public TokenUsage(Integer inputTokenCount, Integer outputTokenCount)
      Creates a new TokenUsage instance 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

      public TokenUsage(Integer inputTokenCount, Integer outputTokenCount, Integer totalTokenCount)
      Creates a new TokenUsage instance 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

      public Integer inputTokenCount()
      Returns the input token count, or null if unknown.
      Returns:
      the input token count, or null if unknown.
    • outputTokenCount

      public Integer outputTokenCount()
      Returns the output token count, or null if unknown.
      Returns:
      the output token count, or null if unknown.
    • totalTokenCount

      public Integer totalTokenCount()
      Returns the total token count, or null if unknown.
      Returns:
      the total token count, or null if unknown.
    • sum

      public static TokenUsage sum(TokenUsage first, TokenUsage second)
      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 TokenUsage instance with the sum of token usages.
    • add

      public TokenUsage add(TokenUsage that)
      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 TokenUsage instance with the token usage of both responses added together.
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object