Repository navigation
fix(server): pin Claude Code billing stamp in Anthropic system text - #2904
Conversation
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
Implementation Review SummaryIntent
Findings AddressedNo CRITICAL or HIGH findings, so no code changed. Remaining Items
Verification
|
|
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 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 Two LOW items, left unfixed:
Kill switch: it behaves as documented. Only the exact value
|
Claude Code starts its system prompt with an
x-anthropic-billing-headerstamp whosecchvalue changes on every request and whose session part ofcc_versionchanges 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
fcharacters of the same length in the flattened top-levelsystemtext, 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 dosystemturns insidemessages.MLXCEL_ANTHROPIC_PIN_BILLING_STAMP=0restores 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/messagesrequests (block-arraysystem, 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.cachedis the server'sprompt-cache: request completedfigure, and/v1/cache/statsrecorded the matching hit (snapshot_hits/hits+1) only in the pinned arm.=0(before)qwen3.8-27b-4bit(snapshot reuse)gemma-3n-e2b-it-4bit(KV reuse)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
claudeCLI.Unit and translator tests pass (
cargo test --lib anthropic, 111 tests), as docargo clippy --lib --tests -- -D warnings,cargo fmt --check, andmake verify-env-var-docs. The workspace-wide test and clippy runs from the issue were not run locally; CI covers them.Closes #2884