-
Notifications
You must be signed in to change notification settings - Fork 2k
Anthropic UsageAccessor #2068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Anthropic UsageAccessor #2068
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2025-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.ai.anthropic.metadata; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.springframework.ai.chat.metadata.Usage; | ||
| import org.springframework.ai.chat.metadata.UsageUtils; | ||
| import org.springframework.util.Assert; | ||
|
|
||
| /** | ||
| * Anthropic Usage accessor class which provides access to the usage metadata. | ||
| * | ||
| * @author Ilayaperumal Gopinathan | ||
| */ | ||
| public record AnthropicUsageAccessor(Map<String, Object> usage) implements Usage { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the extensibility let's not make this record |
||
|
|
||
| public static final String INPUT_TOKENS = "input_tokens"; | ||
|
|
||
| public static final String OUTPUT_TOKENS = "output_tokens"; | ||
|
|
||
| public static final String CACHE_CREATION_INPUT_TOKENS = "cache_creation_input_tokens"; | ||
|
|
||
| public static final String CACHE_READ_INPUT_TOKENS = "cache_read_input_tokens"; | ||
|
|
||
| public AnthropicUsageAccessor { | ||
| Assert.notNull(usage, "usage must not be null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Long getPromptTokens() { | ||
| return UsageUtils.parseLong(this.usage.get(INPUT_TOKENS)); | ||
| } | ||
|
|
||
| @Override | ||
| public Long getGenerationTokens() { | ||
| return UsageUtils.parseLong(this.usage.get(OUTPUT_TOKENS)); | ||
| } | ||
|
|
||
| public Long getCacheCreationInputTokens() { | ||
| return UsageUtils.parseLong(this.usage.get(CACHE_CREATION_INPUT_TOKENS)); | ||
| } | ||
|
|
||
| public Long getCacheReadInputTokens() { | ||
| return UsageUtils.parseLong(this.usage.get(CACHE_READ_INPUT_TOKENS)); | ||
| } | ||
|
|
||
| public Map<String, Object> getUsage() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this to getUsageData() |
||
| return this.usage; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be removed as this is implicit. |
||
| return this.usage.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright 2023-2024 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| @NonNullApi | ||
| @NonNullFields | ||
| package org.springframework.ai.anthropic.metadata; | ||
|
|
||
| import org.springframework.lang.NonNullApi; | ||
| import org.springframework.lang.NonNullFields; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package org.springframework.ai.anthropic.metadata; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| class AnthropicUsageAccessorTests { | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw exception when usage map is null") | ||
| void constructorShouldThrowExceptionWhenUsageMapIsNull() { | ||
| assertThrows(IllegalArgumentException.class, () -> new AnthropicUsageAccessor(null)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should return correct token counts for all fields") | ||
| void shouldReturnCorrectTokenCounts() { | ||
| Map<String, Object> usageMap = new HashMap<>(); | ||
| usageMap.put("input_tokens", 100L); | ||
| usageMap.put("output_tokens", 50L); | ||
| usageMap.put("cache_creation_input_tokens", 25L); | ||
| usageMap.put("cache_read_input_tokens", 75L); | ||
|
|
||
| AnthropicUsageAccessor accessor = new AnthropicUsageAccessor(usageMap); | ||
|
|
||
| assertThat(accessor.getPromptTokens()).isEqualTo(100L); | ||
| assertThat(accessor.getGenerationTokens()).isEqualTo(50L); | ||
| assertThat(accessor.getCacheCreationInputTokens()).isEqualTo(25L); | ||
| assertThat(accessor.getCacheReadInputTokens()).isEqualTo(75L); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should handle missing values in usage map") | ||
| void shouldHandleMissingValues() { | ||
| Map<String, Object> usageMap = new HashMap<>(); | ||
| usageMap.put("input_tokens", 100L); | ||
|
|
||
| AnthropicUsageAccessor accessor = new AnthropicUsageAccessor(usageMap); | ||
|
|
||
| assertThat(accessor.getPromptTokens()).isEqualTo(100L); | ||
| assertThat(accessor.getGenerationTokens()).isNull(); | ||
| assertThat(accessor.getCacheCreationInputTokens()).isNull(); | ||
| assertThat(accessor.getCacheReadInputTokens()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should handle empty usage map") | ||
| void shouldHandleEmptyUsageMap() { | ||
| Map<String, Object> usageMap = new HashMap<>(); | ||
|
|
||
| AnthropicUsageAccessor accessor = new AnthropicUsageAccessor(usageMap); | ||
|
|
||
| assertThat(accessor.getPromptTokens()).isNull(); | ||
| assertThat(accessor.getGenerationTokens()).isNull(); | ||
| assertThat(accessor.getCacheCreationInputTokens()).isNull(); | ||
| assertThat(accessor.getCacheReadInputTokens()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should handle maximum token values") | ||
| void shouldHandleMaximumTokenValues() { | ||
| Map<String, Object> usageMap = new HashMap<>(); | ||
| usageMap.put("input_tokens", Long.MAX_VALUE); | ||
| usageMap.put("output_tokens", Long.MAX_VALUE); | ||
| usageMap.put("cache_creation_input_tokens", Long.MAX_VALUE); | ||
| usageMap.put("cache_read_input_tokens", Long.MAX_VALUE); | ||
|
|
||
| AnthropicUsageAccessor accessor = new AnthropicUsageAccessor(usageMap); | ||
|
|
||
| assertThat(accessor.getPromptTokens()).isEqualTo(Long.MAX_VALUE); | ||
| assertThat(accessor.getGenerationTokens()).isEqualTo(Long.MAX_VALUE); | ||
| assertThat(accessor.getCacheCreationInputTokens()).isEqualTo(Long.MAX_VALUE); | ||
| assertThat(accessor.getCacheReadInputTokens()).isEqualTo(Long.MAX_VALUE); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should return usage metadata") | ||
| void shouldReturnUsageMetadata() { | ||
| Map<String, Object> usageMap = new HashMap<>(); | ||
| usageMap.put("input_tokens", 100L); | ||
| usageMap.put("output_tokens", 50L); | ||
| usageMap.put("cache_creation_input_tokens", 25L); | ||
| usageMap.put("cache_read_input_tokens", 75L); | ||
|
|
||
| AnthropicUsageAccessor accessor = new AnthropicUsageAccessor(usageMap); | ||
|
|
||
| assertThat(accessor.getUsage()).isEqualTo(usageMap); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,4 +79,30 @@ else if (usage != null && usage.getTotalTokens() == 0L) { | |
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Parse the usage metadata value object into Long. | ||
| * @param value the value object. | ||
| * @return the Long value. | ||
| */ | ||
| public static Long parseLong(Object value) { | ||
| if (value == null) { | ||
| return null; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If null, return -1 |
||
| } | ||
| if (value instanceof Long) { | ||
| return (Long) value; | ||
| } | ||
| else if (value instanceof String) { | ||
| return Long.parseLong((String) value); | ||
| } | ||
| else if (value instanceof Number) { | ||
| return ((Number) value).longValue(); | ||
| } | ||
| else if (value instanceof Integer) { | ||
| return ((Integer) value).longValue(); | ||
| } | ||
| else { | ||
| throw new IllegalArgumentException("Unsupported value type: " + value.getClass()); | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be simplified to
AnthropicUsage