Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b7N4ZDbUtA3ofqAqRV
22 lines
630 B
Java
22 lines
630 B
Java
package com.ankurm.aimigration;
|
|
|
|
import org.springframework.ai.chat.metadata.Usage;
|
|
|
|
/**
|
|
* Since Spring AI 2.0.3 (and so in 2.1.0) an Anthropic or Bedrock Converse prompt-token count
|
|
* includes the cache reads and writes. Subtract them to get the full-price input; never add them.
|
|
*/
|
|
public final class UncachedTokens {
|
|
|
|
private UncachedTokens() {
|
|
}
|
|
|
|
public static long of(Usage usage) {
|
|
return usage.getPromptTokens() - orZero(usage.getCacheReadInputTokens()) - orZero(usage.getCacheWriteInputTokens());
|
|
}
|
|
|
|
private static long orZero(Long value) {
|
|
return value == null ? 0 : value;
|
|
}
|
|
}
|