Package org.springframework.ai.support
Class UsageCalculator
java.lang.Object
org.springframework.ai.support.UsageCalculator
A utility class to provide support methods handling
Usage.- Author:
- Ilayaperumal Gopinathan, Jewoo Shin
-
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable ChatResponseaccumulateResponseUsage(@Nullable ChatResponse currentChatResponse, @Nullable ChatResponse accumulatedChatResponse) Folds the usage of the current chat response into the previously accumulatedChatResponse.static UsagegetCumulativeUsage(Usage currentUsage, @Nullable ChatResponse previousChatResponse) Accumulate usage tokens from the previous chat response to the current usage tokens.static booleanCheck if theUsageis empty.static ChatResponsewithUsage(ChatResponse chatResponse, Usage usage) Returns a copy of the given chat response with its usage replaced by the providedUsage, preserving all other response metadata.
-
Method Details
-
getCumulativeUsage
public static Usage getCumulativeUsage(Usage currentUsage, @Nullable ChatResponse previousChatResponse) Accumulate usage tokens from the previous chat response to the current usage tokens.Note: when the two usages are actually summed, the result is a plain
DefaultUsageand the provider-specificnative usageobject is not preserved (it cannot be merged across responses). Only the token counts and cache metrics carry over. The originalcurrentUsage(native usage included) is returned unchanged when there is nothing to accumulate.- Parameters:
currentUsage- the current usage.previousChatResponse- the previous chat response.- Returns:
- accumulated usage.
-
isEmpty
Check if theUsageis empty. Returns true when theUsageis null. Returns true when theUsagehas zero tokens.- Parameters:
usage- the usage to check against.- Returns:
- the boolean value to represent if it is empty.
-
accumulateResponseUsage
public static @Nullable ChatResponse accumulateResponseUsage(@Nullable ChatResponse currentChatResponse, @Nullable ChatResponse accumulatedChatResponse) Folds the usage of the current chat response into the previously accumulatedChatResponse. The returnedChatResponsecarries the cumulative usage (current plus previously accumulated). This is the building block for accumulating usage across the iterations of a recursive flow (for example a tool-calling loop or a validation retry loop).- Parameters:
currentChatResponse- the chat response produced by the current iteration, ornullaccumulatedChatResponse- the chat response carrying the cumulative usage from previous iterations, ornullif none yet- Returns:
- a chat response carrying the cumulative usage, the previously accumulated
response unchanged when the current response is
null, ornullwhen no usage has been reported - Since:
- 2.0.0
-
withUsage
Returns a copy of the given chat response with its usage replaced by the providedUsage, preserving all other response metadata.- Parameters:
chatResponse- the chat response to copyusage- the usage to set on the copy- Returns:
- a new chat response carrying the given usage
- Since:
- 2.0.0
-