Class DefaultUsage

java.lang.Object
org.springframework.ai.chat.metadata.DefaultUsage
All Implemented Interfaces:
Usage
Direct Known Subclasses:
GoogleGenAiUsage

public class DefaultUsage extends Object implements Usage
Default implementation of the Usage interface.
Since:
1.0.0
Author:
Mark Pollack, Ilayaperumal Gopinathan
  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultUsage(@Nullable Integer promptTokens, @Nullable Integer completionTokens, @Nullable Integer totalTokens, @Nullable Object nativeUsage)
    Create a new DefaultUsage with promptTokens, completionTokens, totalTokens and native Usage object.
    DefaultUsage(@Nullable Integer promptTokens, @Nullable Integer completionTokens, @Nullable Integer totalTokens, @Nullable Object nativeUsage, @Nullable Long cacheReadInputTokens, @Nullable Long cacheWriteInputTokens)
    Create a new DefaultUsage with all fields including prompt cache metrics.
    DefaultUsage(Integer promptTokens, Integer completionTokens)
    Create a new DefaultUsage with promptTokens and completionTokens.
    DefaultUsage(Integer promptTokens, Integer completionTokens, Integer totalTokens)
    Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    fromJson(Integer promptTokens, Integer completionTokens, Integer totalTokens, Object nativeUsage, @Nullable Long cacheReadInputTokens, @Nullable Long cacheWriteInputTokens)
    Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.
    @Nullable Long
    Returns the number of input tokens read from the prompt cache, if the provider supports prompt caching.
    @Nullable Long
    Returns the number of input tokens written to the prompt cache, if the provider supports prompt caching.
    Returns the number of tokens returned in the generation (aka completion) of the AI's response.
    @Nullable Object
    Return the usage data from the underlying model API response.
    Returns the number of tokens used in the prompt of the AI request.
    Return the total number of tokens from both the prompt of an AI request and generation of the AI's response.
    int
     
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • DefaultUsage

      public DefaultUsage(@Nullable Integer promptTokens, @Nullable Integer completionTokens, @Nullable Integer totalTokens, @Nullable Object nativeUsage)
      Create a new DefaultUsage with promptTokens, completionTokens, totalTokens and native Usage object.
      Parameters:
      promptTokens - the number of tokens in the prompt, or null if not available
      completionTokens - the number of tokens in the generation, or null if not available
      totalTokens - the total number of tokens, or null to calculate from promptTokens and completionTokens
      nativeUsage - the native usage object returned by the model provider, or null to return the map of prompt, completion and total tokens.
    • DefaultUsage

      public DefaultUsage(@Nullable Integer promptTokens, @Nullable Integer completionTokens, @Nullable Integer totalTokens, @Nullable Object nativeUsage, @Nullable Long cacheReadInputTokens, @Nullable Long cacheWriteInputTokens)
      Create a new DefaultUsage with all fields including prompt cache metrics.
      Parameters:
      promptTokens - the number of tokens in the prompt, or null if not available
      completionTokens - the number of tokens in the generation, or null if not available
      totalTokens - the total number of tokens, or null to calculate from promptTokens and completionTokens
      nativeUsage - the native usage object returned by the model provider, or null to return the map of prompt, completion and total tokens.
      cacheReadInputTokens - the number of input tokens read from prompt cache, or null if not available
      cacheWriteInputTokens - the number of input tokens written to prompt cache, or null if not available
      Since:
      2.0.0
    • DefaultUsage

      public DefaultUsage(Integer promptTokens, Integer completionTokens)
      Create a new DefaultUsage with promptTokens and completionTokens.
      Parameters:
      promptTokens - the number of tokens in the prompt, or null if not available
      completionTokens - the number of tokens in the generation, or null if not available
    • DefaultUsage

      public DefaultUsage(Integer promptTokens, Integer completionTokens, Integer totalTokens)
      Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.
      Parameters:
      promptTokens - the number of tokens in the prompt, or null if not available
      completionTokens - the number of tokens in the generation, or null if not available
      totalTokens - the total number of tokens, or null to calculate from promptTokens and completionTokens
  • Method Details

    • fromJson

      public static DefaultUsage fromJson(Integer promptTokens, Integer completionTokens, Integer totalTokens, Object nativeUsage, @Nullable Long cacheReadInputTokens, @Nullable Long cacheWriteInputTokens)
      Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens. This constructor is used for JSON deserialization and handles both the new format with completionTokens and the legacy format with generationTokens.
      Parameters:
      promptTokens - the number of tokens in the prompt
      completionTokens - the number of tokens in the completion (new format)
      totalTokens - the total number of tokens
      nativeUsage - the native usage object
      Returns:
      a new DefaultUsage instance
    • getPromptTokens

      public Integer getPromptTokens()
      Description copied from interface: Usage
      Returns the number of tokens used in the prompt of the AI request.
      Specified by:
      getPromptTokens in interface Usage
      Returns:
      an Integer with the number of tokens used in the prompt of the AI request.
      See Also:
    • getCompletionTokens

      public Integer getCompletionTokens()
      Description copied from interface: Usage
      Returns the number of tokens returned in the generation (aka completion) of the AI's response.
      Specified by:
      getCompletionTokens in interface Usage
      Returns:
      an Integer with the number of tokens returned in the generation (aka completion) of the AI's response.
      See Also:
    • getTotalTokens

      public Integer getTotalTokens()
      Description copied from interface: Usage
      Return the total number of tokens from both the prompt of an AI request and generation of the AI's response.
      Specified by:
      getTotalTokens in interface Usage
      Returns:
      the total number of tokens from both the prompt of an AI request and generation of the AI's response.
      See Also:
    • getNativeUsage

      public @Nullable Object getNativeUsage()
      Description copied from interface: Usage
      Return the usage data from the underlying model API response.
      Specified by:
      getNativeUsage in interface Usage
      Returns:
      the object of type inferred by the API response.
    • getCacheReadInputTokens

      public @Nullable Long getCacheReadInputTokens()
      Description copied from interface: Usage
      Returns the number of input tokens read from the prompt cache, if the provider supports prompt caching. Cached tokens are tokens that were previously processed and stored by the provider, reducing cost and latency for repeated prompt prefixes.
      Specified by:
      getCacheReadInputTokens in interface Usage
      Returns:
      the number of cached input tokens read, or null if the provider does not support prompt caching or no cache hit occurred.
    • getCacheWriteInputTokens

      public @Nullable Long getCacheWriteInputTokens()
      Description copied from interface: Usage
      Returns the number of input tokens written to the prompt cache, if the provider supports prompt caching. Cache writes occur when new prompt content is cached for the first time.
      Specified by:
      getCacheWriteInputTokens in interface Usage
      Returns:
      the number of input tokens written to cache, or null if the provider does not support prompt caching or no cache write occurred.
    • 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