[FEATURE] Add 5-minute and 1-hour cache-write token counts to the OTel api_request event
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
The claude_code.api_request OTel event gives one cache-write count: cache_creation_tokens. The API response gives more data. Its usage.cache_creation object divides cache writes into ephemeral_5m_input_tokens and ephemeral_1h_input_tokens (see 1-hour cache duration).
The two durations have different prices. For Claude Opus 5.5, a 5-minute write costs $5 per million tokens. A 1-hour write costs $8 per million tokens.
Without the split, an OTel consumer cannot calculate the cost of a request from its token counts. When a session uses both durations, one rate for all cache writes gives an incorrect result.
Sessions with both durations are frequent. The agent often starts subagents, and subagents usually write with a different TTL than the main conversation.
The consumer also cannot find the duration from other attributes. The main conversation uses the 1-hour TTL. Subagents request the 5-minute TTL, but #84289 shows that the effective TTL can be different. Thus query_source does not identify the duration.
Proposed Solution
Add two integer attributes to the claude_code.api_request event:
| Attribute | Value |
|---|---|
| cache_creation_5m_tokens | usage.cache_creation.ephemeral_5m_input_tokens from the API response |
| cache_creation_1h_tokens | usage.cache_creation.ephemeral_1h_input_tokens from the API response |
Rules:
- Keep
cache_creation_tokenswith no change. Existing dashboards and collectors continue to work. - Send the two attributes only when the API response includes
usage.cache_creation. Do not send zero for a count that the API did not report. - When the event has the two attributes, their sum equals
cache_creation_tokens. The API documentation gives the same rule for its fields. - Add the two attributes to the
api_requesttable on the monitoring page.
Priority
Medium - Would be very helpful
Feature Category
Other
Use Case Example
- I collect Claude Code OTel events with a local tool. The tool calculates a reference cost from the published API rates.
- I used
claude-opus-5-5for one session on Claude Code 2.1.290. The session had subagents. The events gave these totals:
| Category | Tokens |
|---|---|
| Input | 4,780 |
| Cache read | 77,498,782 |
| Cache write | 915,534 |
| Output | 238,758 |
/costreported a total of $26.30.- My tool uses one rate for all 915,534 cache-write tokens. With the 1-hour rate, the result is $27.62. With the 5-minute rate, the result is $24.87. Neither result agrees with
/cost. - The main conversation wrote 474,905 of the cache-write tokens. Other requests wrote 440,629. When I price the first group at the 1-hour rate and the second group at the 5-minute rate, the result is $26.30. This agrees with
/cost. - The transcripts for this project show the same pattern. All main-conversation writes are 1-hour. All subagent writes are 5-minute.
- With the proposed attributes, the tool can use the correct rate for each request. It does not need an assumption about the duration.
Additional Context
- #68771 asks for the standard
gen_ai.usage.*attributes. The OTel GenAI conventions for Anthropic define onlygen_ai.usage.cache_write.input_tokens, with no TTL split. Thus #68771 does not supply the split, and this request does not depend on it. - Rates are the Claude Opus 5.5 rates from the Claude price list, in USD per million tokens: input $4, cache read $0.20, 5-minute cache write $5, 1-hour cache write $8, output $20.
- The two lines below are excerpts from my local Claude Code session transcripts. Both records are from one project and use
claude-opus-5-5. I usedjqto select two fields frommessage.usageof each assistant record:
| Line | Transcript file | Claude Code version | Source of request |
|---|---|---|---|
| 1 | ~/.claude/projects/<project>/<session>.jsonl | 2.1.290 | Main conversation |
| 2 | ~/.claude/projects/<project>/<session>/subagents/agent-<id>.jsonl | 2.1.284 | Subagent |
``json``
{"cache_creation_input_tokens": 17710, "cache_creation": {"ephemeral_1h_input_tokens": 17710, "ephemeral_5m_input_tokens": 0}}
{"cache_creation_input_tokens": 31523, "cache_creation": {"ephemeral_5m_input_tokens": 31523, "ephemeral_1h_input_tokens": 0}}
The api_request event has only cache_creation_tokens. Thus the events for these two records do not show that their cache writes have different prices.