Skip to content

fix(server): pin Claude Code billing stamp in Anthropic system text - #2904

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2884-pin-billing-stamp
Nov 9, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2884-pin-billing-stamp

Conversation

@inureyes

@inureyes inureyes commented Nov 9, 2026

Copy link
Copy Markdown
Member

Claude Code starts its system prompt with an x-anthropic-billing-header stamp whose cch value changes on every request and whose session part of cc_version changes on every session. Those bytes made every turn diverge from the previous prompt, so the prompt cache could not reuse it.

The Anthropic translator now replaces those two values with f characters of the same length in the flattened top-level system text, for both the string and block-array forms. Text without the exact header, values beyond the first line or first 160 bytes, and unterminated, over-long, or non-ASCII values pass through byte for byte, as do system turns inside messages. MLXCEL_ANTHROPIC_PIN_BILLING_STAMP=0 restores pass-through; it is read once per process and documented.

Validation

Recorded manual run on this branch's release binary: two consecutive Claude Code-shaped /v1/messages requests (block-array system, 12 tools, follow-up turn) whose stamps differ, comparing the kill switch against the default. The switch-off arm has the same request path as before this change. cached is the server's prompt-cache: request completed figure, and /v1/cache/stats recorded the matching hit (snapshot_hits/hits +1) only in the pinned arm.

Model Turn 2 cached, =0 (before) Turn 2 cached, default (after)
qwen3.8-27b-4bit (snapshot reuse) 0/3825 3789/3821
gemma-3n-e2b-it-4bit (KV reuse) 0/2012 1984/2007

Qwen reuse stops at the history boundary, 5 tokens short of the 3794-token first prompt; Gemma covers its whole first prompt. With these templates the stamp is rendered near the start, so the before arm shows a full miss rather than a partial reuse. The run was shaped like Claude Code but did not use the real claude CLI.

Unit and translator tests pass (cargo test --lib anthropic, 111 tests), as do cargo clippy --lib --tests -- -D warnings, cargo fmt --check, and make verify-env-var-docs. The workspace-wide test and clippy runs from the issue were not run locally; CI covers them.

Closes #2884

Claude Code starts its system prompt with an x-anthropic-billing-header line whose cch value changes on every request and whose fourth cc_version part changes on every session. Those bytes made each turn diverge from the previous prompt, so the prompt cache could not reuse it.

The Anthropic translator now overwrites just those two values with f characters of the same length in the flattened top-level system text. Text without the exact header, values outside the first line or 160 bytes, and unterminated, over-long, or non-ASCII values pass through unchanged. MLXCEL_ANTHROPIC_PIN_BILLING_STAMP=0 restores pass-through.

Two-turn runs with differing stamps, cached tokens on turn 2 with the switch off versus on: qwen3.8-27b-4bit 0/3825 versus 3789/3821, gemma-3n-e2b-it-4bit 0/2012 versus 1984/2007.

Closes #2884
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 9, 2026
@inureyes

inureyes commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Pin the volatile cch and cc_version session values in the leading Claude Code billing stamp so Anthropic Messages prompts keep their cached prefix, with a documented kill switch.

Findings Addressed

No CRITICAL or HIGH findings, so no code changed.

Remaining Items

  • The 160 limit is counted in bytes where fix(server): pin Claude Code's per-request billing stamp so Anthropic Messages prompts keep their cached prefix #2884 says characters. They differ only when the first line has non-ASCII text before byte 160, and the byte count is the stricter of the two. (LOW)
  • The manual run used Claude Code-shaped requests, not the real claude CLI. The acceptance criteria allow this and the PR body states it. (LOW)
  • Qwen turn 2 reuse stops 5 tokens short of the first prompt, so "covers the whole previous prompt" holds for Gemma only. The PR body attributes this to the history boundary. (LOW)

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow (/v1/messages and /v1/messages/count_tokens share the translator)
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass (anthropic selector, 111 tests; workspace run left to CI)

@inureyes

inureyes commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Security and performance review: no CRITICAL, HIGH, or MEDIUM findings, so this review made no code changes.

Panics: none found. All slicing is on bytes, and replace_range only receives ranges bounded by ASCII bytes inside an ASCII-checked value. A standalone harness ran 3,000,000 hostile inputs with debug assertions and overflow checks on, with multi-byte characters placed around byte 160. It saw no panic, output length and UTF-8 validity held, and only ASCII bytes in the first 160 bytes changed.

Cost: 5 ns on text without the prefix and about 280 us on a 16 MiB stamped prompt, bounded by the main JSON body limit. Both /v1/messages and count_tokens pin before rendering, so the cached tokens match what the model sees.

Two LOW items, left unfixed:

  • A cc_version or cch value may contain spaces or another field's key, as in cc_version=1.2.3.4 cch=ab;. Edit ranges then overlap and pinning is not idempotent (about 1.8% of the adversarial corpus). Replacements keep their length, so this cannot panic, and it only changes the sender's own prompt. Rejecting values that contain a space or = would make the ranges disjoint.
  • A stamped request copies the whole system text a second time. Pinning the owned String in place would avoid that, but the cost is negligible next to tokenization.

Kill switch: it behaves as documented. Only the exact value 0 disables pinning, so false or off leave it on. It is read once per process and request input cannot reach it.

cargo test --lib server::anthropic (57 passed), cargo fmt --check, and check_env_var_docs.py pass.

@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 9, 2026
@inureyes
inureyes marked this pull request as ready for review November 9, 2026 14:50
@inureyes
inureyes merged commit cf23519 into main Nov 9, 2026
1 check passed
@inureyes
inureyes deleted the fix/issue-2884-pin-billing-stamp branch November 9, 2026 14:51
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(server): pin Claude Code's per-request billing stamp so Anthropic Messages prompts keep their cached prefix

1 participant