Skip to content

feat(prompt): expose cached input token counts in ResponseMetaInfo#1703

Open
mvanhorn wants to merge 1 commit intoJetBrains:developfrom
mvanhorn:osc/1275-cached-token-counts
Open

feat(prompt): expose cached input token counts in ResponseMetaInfo#1703
mvanhorn wants to merge 1 commit intoJetBrains:developfrom
mvanhorn:osc/1275-cached-token-counts

Conversation

@mvanhorn
Copy link
Copy Markdown

ResponseMetaInfo does not include cached input token counts, even though providers return this data. This adds a cachedInputTokensCount: Int? field (default null for backward compatibility) and populates it from each provider's response:

  • Anthropic: Sums cache_creation_input_tokens and cache_read_input_tokens from the usage object
  • OpenAI: Reads prompt_tokens_details.cached_tokens
  • Bedrock (Anthropic/Nova): Sums cache_read_input_tokens and cache_creation_input_tokens

Also adds a cachedInputTokensCount() setter to the Java ResponseMetaInfoBuilder.

Files changed:

  • prompt/prompt-model/.../Message.kt - New field on ResponseMetaInfo
  • prompt/prompt-model/.../MessageBuilder.kt - Java builder setter
  • prompt/prompt-executor/.../AnthropicLLMClient.kt - Parse cached tokens from Anthropic
  • prompt/prompt-executor/.../OpenAILLMClient.kt - Parse cached tokens from OpenAI
  • prompt/prompt-executor/.../BedrockAnthropicClaudeSerialization.kt - Parse cached tokens from Bedrock

Fixes #1275

This contribution was developed with AI assistance (Claude Code + Codex).

Add cachedInputTokensCount field to ResponseMetaInfo so users can track
cached vs uncached token usage for billing and monitoring. Providers now
parse cached token data from API responses:

- Anthropic: cache_creation_input_tokens + cache_read_input_tokens
- OpenAI: prompt_tokens_details.cached_tokens
- Bedrock (Anthropic): cache_read_input_tokens + cache_creation_input_tokens
- Bedrock (Amazon Nova): cache_read_input_tokens + cache_creation_input_tokens

Also updates the Java ResponseMetaInfoBuilder with a cachedInputTokensCount
setter.

Fixes JetBrains#1275
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.

Expose cached token counts in ResponseMetaInfo

1 participant