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: preserve Claude Code prompt cache with custom output styles (#11121)

Lee Jackson4ec211fb192e
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index cb3c04a14ff..8061cadb66b 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -3386,6 +3386,29 @@ def _merge_anthropic_system(system: Any, additions: list[str]) -> Any:
return system
+_CLAUDE_STYLE_REMINDER_SUFFIX = (
+ " output style is active. Remember to follow the specific guidelines for this style."
+)
+
+
+def _is_repeated_claude_style_reminder(text: str, retained_system: list[str]) -> bool:
+ """Keep Claude's per-tool style reminder from growing an unchanged system prefix.
+
+ Only the exact standalone reminder is redundant, and only when both its style
+ heading and an identical reminder are already retained. Never deduplicate
+ arbitrary instructions or text from user/tool messages.
+ """
+ if not text.endswith(_CLAUDE_STYLE_REMINDER_SUFFIX):
+ return False
+ style = text[: -len(_CLAUDE_STYLE_REMINDER_SUFFIX)]
+ if not style or "\n" in style or "\r" in style:
+ return False
+ heading = f"# Output Style: {style}"
+ return any(heading in part.splitlines() for part in retained_system) and any(
+ text in part.split("\n\n") for part in retained_system
+ )
+
+
class AnthropicMessage(BaseModel):
role: Literal["user", "assistant"]
content: Union[str, list[AnthropicContentBlock]]
@@ -3561,13 +3584,15 @@ class AnthropicMessagesRequest(BaseModel):
normalized_messages: list[Any] = []
system_additions: list[str] = []
+ retained_system = [_anthropic_content_to_system_text(data.get("system"))]
changed = False
for message in messages:
if isinstance(message, dict) and message.get("role") == "system":
- system_additions.append(
- _anthropic_content_to_system_text(message.get("content", ""))
- )
+ text = _anthropic_content_to_system_text(message.get("content", ""))
+ if not _is_repeated_claude_style_reminder(text, retained_system):
+ system_additions.append(text)
+ retained_system.append(text)
changed = True
continue
normalized_messages.append(message)
diff --git a/studio/backend/tests/test_claude_style_reminder_cache.py b/studio/backend/tests/test_claude_style_reminder_cache.py
new file mode 100644
index 00000000000..9eca73de15e
--- /dev/null
+++ b/studio/backend/tests/test_claude_style_reminder_cache.py
@@ -0,0 +1,124 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Claude's later system reminders must not invalidate a stable style prefix."""
+
+import copy
+import os
+import sys
+
+import pytest
+
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
+
+from models.inference import AnthropicMessagesRequest
+from core.inference.anthropic_compat import anthropic_messages_to_openai
+
+
+REMINDER = (
+ "Concise output style is active. Remember to follow the specific guidelines for this style."
+)
+STYLE = f"# Output Style: Concise\nUse short answers.\n\n{REMINDER}\n\nToday's date is fixed."
+
+
+def converted(payload):
+ request = AnthropicMessagesRequest.model_validate(payload).model_dump()
+ return anthropic_messages_to_openai(request["messages"], request["system"])
+
+
[email protected]("system_is_list", [False, True])
[email protected]("style_in_messages", [False, True])
+def test_repeated_style_preserves_system_prefix_and_tool_round(system_is_list, style_in_messages):
+ system = "Base instructions." if style_in_messages else STYLE
+ if system_is_list:
+ system = [{"type": "text", "text": system, "cache_control": {"type": "ephemeral"}}]
+ payload = {"system": system, "messages": [{"role": "user", "content": "Read a file."}]}
+ if style_in_messages:
+ payload["messages"].insert(0, {"role": "system", "content": STYLE})
+ before = converted(payload)
+ followup = copy.deepcopy(payload)
+ followup["messages"] += [
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "read_1",
+ "name": "Read",
+ "input": {"path": "fixture.txt"},
+ }
+ ],
+ },
+ {
+ "role": "user",
+ "content": [{"type": "tool_result", "tool_use_id": "read_1", "content": "orchard"}],
+ },
+ {"role": "system", "content": REMINDER},
+ {"role": "system", "content": [{"type": "text", "text": REMINDER}]},
+ ]
+ original = copy.deepcopy(followup)
+ after = converted(followup)
+ assert after[: len(before)] == before
+ assert after[-2]["tool_calls"][0]["id"] == "read_1"
+ assert after[-1] == {"role": "tool", "tool_call_id": "read_1", "content": "orchard"}
+ assert followup == original
+
+
+ "initial,addition",
+ [
+ ("Base instructions.", REMINDER),
+ ("# Output Style: Other\nUse long answers.\n\n" + REMINDER, REMINDER),
+ ("# Output Style: Concise\nUse short answers.", REMINDER),
+ (STYLE, REMINDER + " Also include examples."),
+ (
+ STYLE,
+ "Other output style is active. Remember to follow the specific guidelines for this style.",
+ ),
+ (STYLE, "Use short answers."),
+ (STYLE, "<total_tokens>14999000 tokens left</total_tokens>"),
+ ("# Output Style: Concise\nUse short answers.\n\nQuoted: " + REMINDER, REMINDER),
+ (STYLE, "New instructions.\n" + REMINDER),
+ ],
+)
+def test_nonredundant_system_instructions_are_preserved(initial, addition):
+ result = converted(
+ {
+ "system": initial,
+ "messages": [
+ {"role": "user", "content": "Hello"},
+ {"role": "system", "content": addition},
+ ],
+ }
+ )
+ assert result[0]["content"] == initial + "\n\n" + addition
+
+
[email protected]("role", ["user", "assistant"])
+def test_conversation_text_cannot_suppress_a_system_instruction(role):
+ result = converted(
+ {
+ "system": "Base instructions.",
+ "messages": [
+ {"role": role, "content": STYLE},
+ {"role": "system", "content": REMINDER},
+ ],
+ }
+ )
+ assert result[0]["content"] == "Base instructions.\n\n" + REMINDER
+ assert result[1] == {"role": role, "content": STYLE}
+
+
+def test_changed_style_definition_is_preserved():
+ updated = STYLE.replace("Use short answers.", "Use detailed answers.")
+ result = converted(
+ {
+ "system": STYLE,
+ "messages": [
+ {"role": "user", "content": "Hello"},
+ {"role": "system", "content": updated},
+ {"role": "system", "content": REMINDER},
+ ],
+ }
+ )
+ assert result[0]["content"] == STYLE + "\n\n" + updated
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.py
index 2d74b78b164..d85255fe7f0 100644
--- a/unsloth_cli/commands/start.py
+++ b/unsloth_cli/commands/start.py
@@ -2754,6 +2754,8 @@ def _claude_local_env(base: str, key: str, entry: dict) -> dict:
"ANTHROPIC_AUTH_TOKEN": key,
"ANTHROPIC_MODEL": model_id,
"CLAUDE_CODE_ATTRIBUTION_HEADER": "0",
+ # Per-tool countdown reminders change the system prefix on local models.
+ "CLAUDE_CODE_TOTAL_TOKENS_REMINDER": "off",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
"CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1",
"CLAUDE_CODE_NO_FLICKER": "1",
diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.py
index 676015e8b78..f9ca24f55c1 100644
--- a/unsloth_cli/tests/test_start.py
+++ b/unsloth_cli/tests/test_start.py
@@ -162,6 +162,7 @@ def test_claude_settings_overlay_pins_local_routing_and_auth():
assert overlay["env"][name] == ""
# The attribution-header suppression is preserved alongside it.
assert overlay["env"]["CLAUDE_CODE_ATTRIBUTION_HEADER"] == "0"
+ assert overlay["env"]["CLAUDE_CODE_TOTAL_TOKENS_REMINDER"] == "off"
# Subagents fall through to the served model instead of a user's opus/sonnet pin.
assert overlay["env"]["CLAUDE_CODE_SUBAGENT_MODEL"] == "inherit"
@@ -1565,6 +1566,7 @@ def test_connect_claude_no_launch(fake_studio):
# Attribution header is suppressed for the session via env + --settings, never
# by writing the user's ~/.claude/settings.json.
_assert_env_set(result.output, "CLAUDE_CODE_ATTRIBUTION_HEADER", "0")
+ _assert_env_set(result.output, "CLAUDE_CODE_TOTAL_TOKENS_REMINDER", "off")
# Claude assumes 200k for an unrecognized model id and clamps the auto-compact
# window into [100k, that], so the real window has to be pinned as well.
_assert_env_set(result.output, "CLAUDE_CODE_MAX_CONTEXT_TOKENS", str(MODEL["context_length"]))
@@ -1876,6 +1878,7 @@ def test_connect_claude_launch_scrubs_conflicting_auth_env(fake_studio, monkeypa
assert captured["env"]["ANTHROPIC_BASE_URL"] == BASE
assert captured["env"]["ANTHROPIC_MODEL"] == MODEL["id"]
assert captured["env"]["CLAUDE_CODE_ATTRIBUTION_HEADER"] == "0"
+ assert captured["env"]["CLAUDE_CODE_TOTAL_TOKENS_REMINDER"] == "off"
@pytest.mark.skipif(