Class DefaultUsage
java.lang.Object
org.springframework.ai.chat.metadata.DefaultUsage
- All Implemented Interfaces:
Usage
- Direct Known Subclasses:
GoogleGenAiUsage
Default implementation of the
Usage interface.- Since:
- 1.0.0
- Author:
- Mark Pollack, Ilayaperumal Gopinathan
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultUsage(@Nullable Integer promptTokens, @Nullable Integer completionTokens, @Nullable Integer totalTokens, @Nullable Object nativeUsage) Create a new DefaultUsage with promptTokens, completionTokens, totalTokens and nativeUsageobject.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 TypeMethodDescriptionbooleanstatic DefaultUsagefromJson(Integer promptTokens, Integer completionTokens, Integer totalTokens, Object nativeUsage, @Nullable Long cacheReadInputTokens, @Nullable Long cacheWriteInputTokens) Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.@Nullable LongReturns the number of input tokens read from the prompt cache, if the provider supports prompt caching.@Nullable LongReturns 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 ObjectReturn 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.inthashCode()toString()
-
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 nativeUsageobject.- Parameters:
promptTokens- the number of tokens in the prompt, ornullif not availablecompletionTokens- the number of tokens in the generation, ornullif not availabletotalTokens- the total number of tokens, ornullto calculate from promptTokens and completionTokensnativeUsage- the native usage object returned by the model provider, ornullto 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, ornullif not availablecompletionTokens- the number of tokens in the generation, ornullif not availabletotalTokens- the total number of tokens, ornullto calculate from promptTokens and completionTokensnativeUsage- the native usage object returned by the model provider, ornullto return the map of prompt, completion and total tokens.cacheReadInputTokens- the number of input tokens read from prompt cache, ornullif not availablecacheWriteInputTokens- the number of input tokens written to prompt cache, ornullif not available- Since:
- 2.0.0
-
DefaultUsage
Create a new DefaultUsage with promptTokens and completionTokens.- Parameters:
promptTokens- the number of tokens in the prompt, ornullif not availablecompletionTokens- the number of tokens in the generation, ornullif not available
-
DefaultUsage
Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.- Parameters:
promptTokens- the number of tokens in the prompt, ornullif not availablecompletionTokens- the number of tokens in the generation, ornullif not availabletotalTokens- the total number of tokens, ornullto 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 promptcompletionTokens- the number of tokens in the completion (new format)totalTokens- the total number of tokensnativeUsage- the native usage object- Returns:
- a new DefaultUsage instance
-
getPromptTokens
Description copied from interface:UsageReturns the number of tokens used in the prompt of the AI request.- Specified by:
getPromptTokensin interfaceUsage- Returns:
- an
Integerwith the number of tokens used in the prompt of the AI request. - See Also:
-
getCompletionTokens
Description copied from interface:UsageReturns the number of tokens returned in the generation (aka completion) of the AI's response.- Specified by:
getCompletionTokensin interfaceUsage- Returns:
- an
Integerwith the number of tokens returned in the generation (aka completion) of the AI's response. - See Also:
-
getTotalTokens
Description copied from interface:UsageReturn the total number of tokens from both the prompt of an AI request and generation of the AI's response.- Specified by:
getTotalTokensin interfaceUsage- Returns:
- the total number of tokens from both the prompt of an AI request and generation of the AI's response.
- See Also:
-
getNativeUsage
Description copied from interface:UsageReturn the usage data from the underlying model API response.- Specified by:
getNativeUsagein interfaceUsage- Returns:
- the object of type inferred by the API response.
-
getCacheReadInputTokens
Description copied from interface:UsageReturns 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:
getCacheReadInputTokensin interfaceUsage- Returns:
- the number of cached input tokens read, or
nullif the provider does not support prompt caching or no cache hit occurred.
-
getCacheWriteInputTokens
Description copied from interface:UsageReturns 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:
getCacheWriteInputTokensin interfaceUsage- Returns:
- the number of input tokens written to cache, or
nullif the provider does not support prompt caching or no cache write occurred.
-
equals
-
hashCode
public int hashCode() -
toString
-