[PR #2290] [CLOSED] [Engine] Anthropic API: cache_creation_input_tokens, the prompt tokens read now and kept for the next request #3965

Closed
opened 2026-11-07 15:32:04 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2290
Author: @xrip
Created: 10/13/2026
Status: ❌ Closed

Base: main ← Head: pr/anthropic-cache-creation


📝 Commits (1)

  • be7e591 Anthropic API: cache_creation_input_tokens, the prompt tokens read now and kept for the next request

📊 Changes

4 files changed (+13 additions, -10 deletions)

View changed files

📝 serve/server.py (+5 -5)
📝 serve/test_parallel_yield.py (+1 -1)
📝 serve/test_request_accounting.py (+2 -2)
📝 serve/test_server.py (+5 -2)

📄 Description

Problem and result

/v1/messages never reports cache_creation_input_tokens. Every token a request reads stays in the session (and in the conversation cache) for the next request, which is what Anthropic calls a cache write. Clients that show cache writes, such as Claude Code, always see none: their status line and cost estimate count every newly read token as uncached input.

field before after
cache_read_input_tokens the reused prefix the same
cache_creation_input_tokens absent the tokens read now, but the last
input_tokens the tokens read now 1, the last prompt token

The three fields still add up to the prompt, so a client's context fill does not change.

Fixes #2289 (re-tested on 0.2.0-beta there: still present).

Approach

Only anthropic_events' message_delta usage changes; the OpenAI and Responses paths are untouched. input_tokens stays at least 1: Claude Code takes 0 as "not given" and keeps message_start's whole prompt beside the cache counts, which counts the prompt twice (#1013). The usage assertions in the tests now check the three-way split and input_tokens >= 1.

Validation

Passed:

  • Rebased onto 0.2.1 (upstream main 90767252) as be7e591c; 0.2.1 split serve/test_parallel.py, so the changed assertion is now in serve/test_parallel_yield.py. python -m pytest -q serve/test_server.py serve/test_parallel.py serve/test_parallel_yield.py serve/test_request_accounting.py: 392 passed, 3 skipped, 444 subtests passed (Windows 11, Python 3.14).
  • The changed tests against 0.2.1's own serve/server.py (this change taken out): 4 fail, among them UsageAndStatus.test_anthropic and the two RequestAccounting.test_anthropic_*_classifies_only_original_input_as_cached. With the change they pass.
  • In daily use with Claude Code on my box (2x CMP 50HX, Ubuntu 24.04.4) since 2026-10-13, in my fork, which is now on 0.2.0-beta (342185bd) with this change. Claude Code shows the cache writes.

Failed: none observed.

Not run: this exact branch against a GPU engine (the change is in the usage fields; the tests use the server's stub engine).

Limits

Display only: the totals were already right.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2290 **Author:** [@xrip](https://github.com/xrip) **Created:** 10/13/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `pr/anthropic-cache-creation` --- ### 📝 Commits (1) - [`be7e591`](https://github.com/Niko1221/Strata/commit/be7e591c6a2fa1caac541ba8e8cde092f2180f8c) Anthropic API: cache_creation_input_tokens, the prompt tokens read now and kept for the next request ### 📊 Changes **4 files changed** (+13 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+5 -5) 📝 `serve/test_parallel_yield.py` (+1 -1) 📝 `serve/test_request_accounting.py` (+2 -2) 📝 `serve/test_server.py` (+5 -2) </details> ### 📄 Description ## Problem and result `/v1/messages` never reports `cache_creation_input_tokens`. Every token a request reads stays in the session (and in the conversation cache) for the next request, which is what Anthropic calls a cache write. Clients that show cache writes, such as Claude Code, always see none: their status line and cost estimate count every newly read token as uncached input. | field | before | after | |---|---|---| | `cache_read_input_tokens` | the reused prefix | the same | | `cache_creation_input_tokens` | absent | the tokens read now, but the last | | `input_tokens` | the tokens read now | 1, the last prompt token | The three fields still add up to the prompt, so a client's context fill does not change. ## Related discussion Fixes #2289 (re-tested on 0.2.0-beta there: still present). ## Approach Only `anthropic_events`' `message_delta` usage changes; the OpenAI and Responses paths are untouched. `input_tokens` stays at least 1: Claude Code takes 0 as "not given" and keeps `message_start`'s whole prompt beside the cache counts, which counts the prompt twice (#1013). The usage assertions in the tests now check the three-way split and `input_tokens >= 1`. ## Validation **Passed:** - Rebased onto 0.2.1 (upstream main `90767252`) as `be7e591c`; 0.2.1 split `serve/test_parallel.py`, so the changed assertion is now in `serve/test_parallel_yield.py`. `python -m pytest -q serve/test_server.py serve/test_parallel.py serve/test_parallel_yield.py serve/test_request_accounting.py`: 392 passed, 3 skipped, 444 subtests passed (Windows 11, Python 3.14). - The changed tests against 0.2.1's own `serve/server.py` (this change taken out): 4 fail, among them `UsageAndStatus.test_anthropic` and the two `RequestAccounting.test_anthropic_*_classifies_only_original_input_as_cached`. With the change they pass. - In daily use with Claude Code on my box (2x CMP 50HX, Ubuntu 24.04.4) since 2026-10-13, in my fork, which is now on 0.2.0-beta (`342185bd`) with this change. Claude Code shows the cache writes. **Failed:** none observed. **Not run:** this exact branch against a GPU engine (the change is in the usage fields; the tests use the server's stub engine). ## Limits Display only: the totals were already right. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:32:04 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3965
No description provided.