feat(anthropic): add prompt caching support and cached tokens in usage (#2223)#2350
feat(anthropic): add prompt caching support and cached tokens in usage (#2223)#2350Leeaoyin 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
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! |
oss-maintainer
left a comment
There was a problem hiding this comment.
LGTM. Looks good.
Automated review by "github-manager-bot"
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)