Repository navigation
Description
Summary
Claude Code puts a billing stamp at the very start of its system prompt, and part of that stamp changes on every request. When Claude Code talks to mlxcel-server through the Anthropic Messages API, the rendered prompt therefore differs from the previous turn at a position thousands of tokens in, so the prompt cache can only reuse the prefix up to that point and re-reads the rest of the system prompt and history on every turn.
Pin the volatile parts of the stamp to fixed placeholder characters before the prompt is rendered and tokenized, as llama.cpp and Strata already do.
The stamp
Claude Code sends a system prompt beginning with a line like:
x-anthropic-billing-header: cc_version=2.1.170.bf4; cc_entrypoint=sdk-cli; cch=b145e;
cch=is a short hex checksum that changes on every request.- The fourth dot-separated part of
cc_version(bf4above) changes on every session.
The first line of the system prompt is not what the cache sees first, though: depending on the chat template, the tool definitions are rendered before the system text. Strata measured the divergence about 22K tokens into the prompt, after the tool list, on every turn.
Prior art
- llama.cpp: server: (anthropic API) fix prefix caching ggml-org/llama.cpp#21793, "server: (anthropic API) fix prefix caching" (release b8906). The author measured only about 18.6K tokens of a 60K+ Claude Code context being reused. The fix replaces the changing
cchchecksum withfffff; the header itself is kept. - Strata (MIT, https://github.com/Niko1221/Strata, read at
146df3c):serve/frontend.py,pin_billing_stamp(around lines 621-650), applied to the Anthropicsystemtext. It pins bothcchand the session part ofcc_versiontofcharacters of the same length, touches the text only when it starts withx-anthropic-billing-header:, only within its first 160 characters, and only values of bounded length (cchat most 16 characters,cc_versionat most 64). Its docstring records the effect: before the fix "half of every agent prompt was read again". - Strata documents the behaviour in
docs/DETAILS.md, "Conversation cache": "Claude Code stamps its system prompt with a billing header that changes on every request; the server pins both stamps so the prompt stays the same."
Current mlxcel state
src/server/anthropic_translator.rs,anthropic_request_to_chat(around line 80): the top-levelsystemfield is flattened byAnthropicSystem::to_text(src/server/types/anthropic_request.rs:86-115) and pushed unchanged as the leading system message.- Mid-conversation
role: "system"turns (Claude Code >= 2.1.156, issue fix(server): /v1/messages returns HTTP 422 for Claude Code >= 2.1.156 (system-role turns inside the messages array) #349) are folded byfold_system_messages; they do not carry the stamp, but the pinning should apply only to the leading system text either way. - No code in
src/handlesx-anthropic-billing-headerorcch=today. - The prompt cache (
src/server/prompt_cache/, including the history-boundary snapshot from feat: Donate a history-boundary snapshot during prefill for snapshot-only families #1143) keys on the rendered tokens, so any byte that changes early in the system prompt caps reuse at that position.
Proposed solution
- Add
pin_billing_stamp(system: &str) -> Cow<str>next to the translator (or insrc/server/types/anthropic_request.rs):- Act only when the text starts with
x-anthropic-billing-header:(exact prefix, case-sensitive). Otherwise return the input unchanged. - Search only within the first 160 characters.
- Replace the value after
cch=up to the next;with the same number offcharacters, when that value is 1 to 16 characters long. - In the value after
cc_version=up to the next;, when it is at most 64 characters and has more than three dot-separated parts, replace every character after the third dot withf, keeping its length. - Keep everything else byte for byte, including the header line itself, so the model sees the same structure.
- Act only when the text starts with
- Apply it in
anthropic_request_to_chatto the flattened top-level system text, before it becomes the leading system message. Apply it to both the string and block-array forms ofsystem, sinceto_textalready joins blocks. - Default on. Add a kill switch, for example
MLXCEL_ANTHROPIC_PIN_BILLING_STAMP=0, read once per process like the other prompt-cache switches insrc/server/prompt_cache/policy.rs, for anyone who needs the exact text passed through. - Document it in
docs/server-features.md(Anthropic Messages section) anddocs/environment-variables.md.
Not in scope: rewriting any other part of client prompts, or the OpenAI-compatible routes (Codex and others do not send this stamp). If another client is later found to stamp prompts in a similar way, it should get its own explicit, documented rule rather than a generic normaliser.
Acceptance criteria
- Unit tests for
pin_billing_stamp:- The example header above becomes
x-anthropic-billing-header: cc_version=2.1.170.fff; cc_entrypoint=sdk-cli; cch=fffff;and the rest of the text is unchanged. - Two inputs differing only in
cch(and only in thecc_versionsession part) produce identical output. - Text without the exact prefix, a header found after the first 160 characters, an over-long
cchvalue, acc_versionwith three or fewer parts, and a missing trailing;are all left unchanged. - Non-ASCII text after the header is untouched (operate on byte offsets of ASCII matches only).
- The example header above becomes
- Translator tests: both
systemforms (string and block array) are pinned; a system-role turn insidemessagesis not. - Integration test (or a recorded manual run) against
mlxcel-serverwith two consecutive Claude Code-shaped requests whose stamps differ: the second request's/v1/cache/statsreuse covers the whole previous prompt rather than stopping at the stamp's position. Record reused tokens before and after the change. - The kill switch restores pass-through behaviour.
Verification
cargo test --workspace --profile test-fast --features metal,accelerate anthropic
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
./target/release/mlxcel-server -m <model> --port 8080 &
ANTHROPIC_BASE_URL=http://127.0.0.1:8080 claude -p "Summarize README.md" # twice, then:
curl -s http://127.0.0.1:8080/v1/cache/statsGenerated by Claude Code
Activity
fix(server): pin Claude Code billing stamp in Anthropic system text (l…