Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: stop the Claude CLI billing line from breaking prompt caching (#15845)

Nilay453b7880d33b
diff --git a/studio/backend/core/inference/anthropic_compat.py b/studio/backend/core/inference/anthropic_compat.py
index 09a7bf0201c..ce120e238df 100644
--- a/studio/backend/core/inference/anthropic_compat.py
+++ b/studio/backend/core/inference/anthropic_compat.py
@@ -34,6 +34,8 @@ def anthropic_tool_use_id(upstream_id = None, prefix = "toolu_") -> str:
_WEB_SEARCH_HIT = re.compile(r"^Title: (.*)\nURL: (\S+)", re.MULTILINE)
+# Claude Code's billing line varies per request and would break prefix caching.
+_BILLING_LINE = re.compile(r"\Ax-anthropic-billing-header:[^\n]*(?:\n|\Z)")
def web_search_tool_result_content(
@@ -172,7 +174,8 @@ def anthropic_messages_to_openai(
if system:
if isinstance(system, str):
- result.append({"role": "system", "content": system})
+ if system := _BILLING_LINE.sub("", system, count = 1):
+ result.append({"role": "system", "content": system})
elif isinstance(system, list):
parts = []
for block in system:
@@ -180,6 +183,11 @@ def anthropic_messages_to_openai(
parts.append(block["text"])
elif isinstance(block, str):
parts.append(block)
+ parts = [
+ kept
+ for part in parts
+ if (kept := _BILLING_LINE.sub("", part, count = 1)) or kept == part
+ ]
if parts:
result.append({"role": "system", "content": "\n".join(parts)})
diff --git a/studio/backend/tests/test_claude_style_reminder_cache.py b/studio/backend/tests/test_claude_style_reminder_cache.py
index 059ac2fda54..aeefcab2e45 100644
--- a/studio/backend/tests/test_claude_style_reminder_cache.py
+++ b/studio/backend/tests/test_claude_style_reminder_cache.py
@@ -132,3 +132,50 @@ def test_changed_style_definition_is_preserved():
)
assert result[0]["content"] == STYLE
assert result[1] == {"role": "user", "content": f"Hello\n{note(updated)}"}
+
+
+def claude_code_system(billing):
+ return [
+ {"type": "text", "text": f"x-anthropic-billing-header: {billing}"},
+ {"type": "text", "text": "You are Claude Code.", "cache_control": {"type": "ephemeral"}},
+ {"type": "text", "text": "Base instructions.", "cache_control": {"type": "ephemeral"}},
+ ]
+
+
+ "first,second",
+ [
+ (
+ "cc_version=2.1.312.492; cc_entrypoint=cli;",
+ "cc_version=2.1.312.9cc; cc_entrypoint=cli;",
+ ),
+ (
+ "cc_version=2.1.30.abc; cc_entrypoint=cli; cch=a1b2c;",
+ "cc_version=2.1.30.abc; cc_entrypoint=cli; cch=f9e8d;",
+ ),
+ ],
+)
+def test_claude_code_billing_header_does_not_reach_the_model(first, second):
+ messages = [{"role": "user", "content": "Hello"}]
+ before = converted({"system": claude_code_system(first), "messages": messages})
+ after = converted({"system": claude_code_system(second), "messages": messages})
+ assert before == after
+ assert before[0] == {"role": "system", "content": "You are Claude Code.\nBase instructions."}
+ header_only = converted({"system": claude_code_system(first)[:1], "messages": messages})
+ assert header_only == converted({"messages": messages})
+ flat = f"x-anthropic-billing-header: {first}\nYou are Claude Code.\nBase instructions."
+ expected = {"role": "system", "content": "You are Claude Code.\nBase instructions."}
+ assert converted({"system": flat, "messages": messages})[0] == expected
+ one_block = [{"type": "text", "text": flat}]
+ assert converted({"system": one_block, "messages": messages})[0] == expected
+
+
+def test_billing_text_later_in_the_prompt_is_kept():
+ text = "Quote: x-anthropic-billing-header: is a Claude Code line."
+ result = converted(
+ {
+ "system": [{"type": "text", "text": text}],
+ "messages": [{"role": "user", "content": "Hi"}],
+ }
+ )
+ assert result[0] == {"role": "system", "content": text}