Skip to content

feat(anthropic): add prompt caching support and cached tokens in usage (#2223)#2350

Open
Leeaoyin wants to merge 1 commit into
agentscope-ai:mainfrom
Leeaoyin:feat/issue-2223-anthropic-prompt-caching
Open

feat(anthropic): add prompt caching support and cached tokens in usage (#2223)#2350
Leeaoyin wants to merge 1 commit into
agentscope-ai:mainfrom
Leeaoyin:feat/issue-2223-anthropic-prompt-caching

Conversation

@Leeaoyin

@Leeaoyin Leeaoyin commented Jul 22, 2026

Copy link
Copy Markdown

AgentScope-Java Version

2.0.1-SNAPSHOT

Description

Fixes #2223

The Anthropic formatter did not add cache_control to system messages, conversation
messages, 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 breakpoints are set with cache_control: {"type": "ephemeral"} on content blocks
  • The cache prefix order is tools -> system -> messages, so marking the last tool
    definition, the system block, and the last message caches the full conversation prefix
  • Cached token counts come from cache_read_input_tokens and cache_creation_input_tokens
    in the API response usage

Changes:

  • AnthropicBaseFormatter: send system message as a text block with cache_control
    when caching is enabled; add applyCacheControl that marks the last cacheable
    content block of the last message so the conversation prefix is cached
  • AnthropicToolsHelper: mark the last tool definition with cache_control
  • AnthropicChatModel: wire the steps above when GenerateOptions.cacheControl is enabled
  • AnthropicResponseParser: fill cachedTokens in usage from
    cache_read_input_tokens / cache_creation_input_tokens for both streaming
    and 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 to
AnthropicResponseParserTest and AnthropicChatModelTest)

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Code is ready for review

- 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
Copilot AI review requested due to automatic review settings July 22, 2026 07:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_control support 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_tokens and incorporate them into ChatUsage (including streaming aggregation via message_start + message_delta combination).
  • 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_start events, messageId is typically null. Now that message_delta responses are emitted for usage, ensure the response id falls back to the id captured from message_start so the final usage chunk can be associated with the same message.
        }

        return ChatResponse.builder().id(messageId).content(contentBlocks).usage(usage).build();

@codecov

codecov Bot commented Jul 22, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Anthropic 请求未走 Prompt caching

2 participants