feat(anthropic): add prompt caching support and cached tokens in usage (#2223)#2350
Open
Leeaoyin wants to merge 1 commit into
Open
feat(anthropic): add prompt caching support and cached tokens in usage (#2223)#2350Leeaoyin wants to merge 1 commit into
Leeaoyin wants to merge 1 commit into
Conversation
- Apply cache_control to system message, last message, and tool definitions when enabled - Fill cachedTokens in usage for both streaming and non-streaming responses - Add unit tests
Contributor
There was a problem hiding this comment.
Pull request overview
Adds Anthropic prompt caching support (cache_control) across system prompts, conversation messages, and tool definitions, and extends usage parsing to include cached token accounting for both streaming and non-streaming responses (fixes #2223).
Changes:
- Add
cache_controlsupport to system messages, message content blocks (automatic “mark last cacheable block” strategy), and the last tool definition when caching is enabled. - Parse
cache_read_input_tokens/cache_creation_input_tokensand incorporate them intoChatUsage(including streaming aggregation viamessage_start+message_deltacombination). - Add/extend tests covering cache control formatting and cached token usage parsing.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicBaseFormatter.java | Adds cache-control aware system prompt handling and automatic message cache-control marking. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicToolsHelper.java | Marks the last tool definition with cache_control when caching is enabled. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/AnthropicChatModel.java | Wires cache control behavior into request building when GenerateOptions.cacheControl is enabled. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicResponseParser.java | Adds cached-token accounting and combines streaming start/delta usage. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicResponseParserTest.java | Extends tests for cached token parsing and streaming usage combination. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicCacheControlTest.java | New tests validating cache-control placement for messages, system prompt, and tools. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/AnthropicChatModelTest.java | Adds request-building smoke tests around cacheControl toggles (but currently not asserting request contents). |
Comments suppressed due to low confidence (1)
agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicResponseParser.java:251
- For non-
message_startevents,messageIdis typically null. Now thatmessage_deltaresponses are emitted for usage, ensure the response id falls back to the id captured frommessage_startso the final usage chunk can be associated with the same message.
}
return ChatResponse.builder().id(messageId).content(contentBlocks).usage(usage).build();
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentScope-Java Version
2.0.1-SNAPSHOT
Description
Fixes #2223
The Anthropic formatter did not add
cache_controlto system messages, conversationmessages, or tool definitions, and usage did not include cached tokens. Long system
prompts, large tool definitions, and multi-turn conversations could not benefit from
Anthropic prompt caching.
Implementation follows the official Anthropic prompt caching documentation:
https://platform.claude.com/docs/zh-CN/build-with-claude/prompt-caching
cache_control: {"type": "ephemeral"}on content blockstools->system->messages, so marking the last tooldefinition, the system block, and the last message caches the full conversation prefix
cache_read_input_tokensandcache_creation_input_tokensin the API response usage
Changes:
AnthropicBaseFormatter: send system message as a text block withcache_controlwhen caching is enabled; add
applyCacheControlthat marks the last cacheablecontent block of the last message so the conversation prefix is cached
AnthropicToolsHelper: mark the last tool definition withcache_controlAnthropicChatModel: wire the steps above whenGenerateOptions.cacheControlis enabledAnthropicResponseParser: fillcachedTokensin usage fromcache_read_input_tokens/cache_creation_input_tokensfor both streamingand non-streaming responses
How to test:
mvn test -pl agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic -am(171 tests passing; new tests in
AnthropicCacheControlTest, plus additions toAnthropicResponseParserTestandAnthropicChatModelTest)Checklist
mvn spotless:applymvn test)