Skip to content

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

Closed

Description

@inureyes

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 (bf4 above) 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 cch checksum with fffff; 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 Anthropic system text. It pins both cch and the session part of cc_version to f characters of the same length, touches the text only when it starts with x-anthropic-billing-header:, only within its first 160 characters, and only values of bounded length (cch at most 16 characters, cc_version at 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

Proposed solution

  1. Add pin_billing_stamp(system: &str) -> Cow<str> next to the translator (or in src/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 of f characters, 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 with f, keeping its length.
    • Keep everything else byte for byte, including the header line itself, so the model sees the same structure.
  2. Apply it in anthropic_request_to_chat to the flattened top-level system text, before it becomes the leading system message. Apply it to both the string and block-array forms of system, since to_text already joins blocks.
  3. Default on. Add a kill switch, for example MLXCEL_ANTHROPIC_PIN_BILLING_STAMP=0, read once per process like the other prompt-cache switches in src/server/prompt_cache/policy.rs, for anyone who needs the exact text passed through.
  4. Document it in docs/server-features.md (Anthropic Messages section) and docs/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 the cc_version session part) produce identical output.
    • Text without the exact prefix, a header found after the first 160 characters, an over-long cch value, a cc_version with 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).
  • Translator tests: both system forms (string and block array) are pinned; a system-role turn inside messages is not.
  • Integration test (or a recorded manual run) against mlxcel-server with two consecutive Claude Code-shaped requests whose stamps differ: the second request's /v1/cache/stats reuse 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/stats

Generated by Claude Code

Activity

  1. added
    status:ready
    type:bug
    priority:high
    area:inference
    status:in-progress
    status:review
    status:done
    and removed
    status:ready
    status:in-progress
    status:review
    on Nov 8, 2026
  2. added a commit that references this issue on Nov 9, 2026
    cf23519
  3. added this to the 0.9 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:inferencepriority:highstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions