mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-10 03:28:53 +00:00
fix(anthropic): strip Claude Code identity from system prompt for non-Claude providers
Claude Code prefixes its system prompt with "You are Claude Code, Anthropic's official CLI for Claude.". When routed to a provider whose Anthropic-compatible endpoint serves a different model (DeepSeek, MiniMax, Tencent, OpenAI-like passthrough), that sentence is a false self-description and should be dropped before sending upstream. This adds a should_strip_claude_code_identity() gate alongside the existing should_strip_billing_metadata() gate, plus a strip_claude_code_identity() helper. The two gates stay distinct: the billing-header strip also fires for Bedrock/Vertex/Azure, which still serve real Claude models, whereas the identity strip only fires for non-Claude models.
This commit is contained in:
parent
e484a7c89c
commit
965cbb3f87
9 changed files with 244 additions and 0 deletions
|
|
@ -1685,6 +1685,19 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
|
|||
"""
|
||||
return False
|
||||
|
||||
def should_strip_claude_code_identity(self) -> bool:
|
||||
"""
|
||||
Whether to drop Claude Code's self-identification from the system prompt.
|
||||
|
||||
Distinct from ``should_strip_billing_metadata``: that drops the billing header on
|
||||
every provider whose request shape rejects it, including Bedrock/Vertex/Azure, which
|
||||
still serve *Claude* models. This only trips on providers whose
|
||||
Anthropic-compatible endpoint serves a *different* model, where "You are Claude
|
||||
Code" is a false self-description. The first-party config and the Claude-serving
|
||||
providers keep it, so this stays False here.
|
||||
"""
|
||||
return False
|
||||
|
||||
def translate_system_message(self, messages: list[AllMessageValues]) -> list[AnthropicSystemMessageContent]:
|
||||
"""
|
||||
Translate system message to anthropic format.
|
||||
|
|
|
|||
|
|
@ -77,6 +77,9 @@ _CLAUDE_CODE_OBJECT_LIST_ADAPTER: Final = TypeAdapter(list[object])
|
|||
_CLAUDE_CODE_USER_AGENT_PREFIXES: Final = ("claude-cli/", "claude-code/")
|
||||
|
||||
|
||||
_CLAUDE_CODE_IDENTITY: Final = "You are Claude Code, Anthropic's official CLI for Claude."
|
||||
|
||||
|
||||
def supports_anthropic_cache_control(model: str, custom_llm_provider: str | None) -> bool:
|
||||
from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider
|
||||
from litellm.utils import supports_prompt_caching
|
||||
|
|
@ -98,6 +101,25 @@ def is_claude_code_user_agent(user_agent: str) -> bool:
|
|||
return user_agent.startswith(_CLAUDE_CODE_USER_AGENT_PREFIXES)
|
||||
|
||||
|
||||
def strip_claude_code_identity(text: str) -> str | None:
|
||||
"""Remove Claude Code's self-identification sentence from a single system text block.
|
||||
|
||||
Claude Code prefixes its system prompt with ``You are Claude Code, Anthropic's
|
||||
official CLI for Claude.``. When that prompt is forwarded to a non-Claude model
|
||||
(e.g. a DeepSeek-compatible or OpenAI-like Anthropic-compatible endpoint), the
|
||||
sentence is a false self-description and should be dropped before sending
|
||||
upstream. Returns ``None`` when the text was nothing but the identity sentence
|
||||
(so callers can drop the whole block), otherwise the text with the sentence and
|
||||
its framing whitespace removed.
|
||||
"""
|
||||
stripped: Final = text.strip()
|
||||
if stripped == _CLAUDE_CODE_IDENTITY:
|
||||
return None
|
||||
if stripped.startswith(_CLAUDE_CODE_IDENTITY):
|
||||
return text.replace(_CLAUDE_CODE_IDENTITY, "", 1).lstrip("\n")
|
||||
return text
|
||||
|
||||
|
||||
def _validated_claude_code_mapping(value: object) -> dict[object, object] | None:
|
||||
try:
|
||||
return _CLAUDE_CODE_OBJECT_MAPPING_ADAPTER.validate_python(value)
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ from ...common_utils import (
|
|||
AnthropicModelInfo,
|
||||
optionally_handle_anthropic_oauth,
|
||||
strip_advisor_blocks_from_messages,
|
||||
strip_claude_code_identity,
|
||||
strip_encrypted_reasoning_blocks_from_anthropic_messages,
|
||||
)
|
||||
from .mid_conversation_system import (
|
||||
|
|
@ -122,6 +123,56 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig):
|
|||
"""
|
||||
return False
|
||||
|
||||
def should_strip_claude_code_identity(self) -> bool:
|
||||
"""
|
||||
Whether to drop Claude Code's self-identification from the system prompt.
|
||||
|
||||
Distinct from ``should_strip_billing_metadata``: that drops the billing header on
|
||||
every provider whose request shape rejects it, including Bedrock/Vertex/Azure, which
|
||||
still serve *Claude* models. This only trips on providers whose
|
||||
Anthropic-compatible endpoint serves a *different* model (DeepSeek, MiniMax,
|
||||
Tencent, OpenAI-like passthrough), where "You are Claude Code" is a false
|
||||
self-description. The first-party config keeps it.
|
||||
"""
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _strip_claude_code_identity_from_system(system_param):
|
||||
"""
|
||||
Strip Claude Code's self-identification sentence from system parameter.
|
||||
|
||||
Args:
|
||||
system_param: Can be a string or a list of system message content blocks
|
||||
|
||||
Returns:
|
||||
System parameter with the identity sentence removed, or None if all content was removed
|
||||
"""
|
||||
if isinstance(system_param, str):
|
||||
return strip_claude_code_identity(system_param)
|
||||
elif isinstance(system_param, list):
|
||||
filtered_list: Final = []
|
||||
for content_block in system_param:
|
||||
if isinstance(content_block, dict):
|
||||
text = content_block.get("text", "")
|
||||
content_type = content_block.get("type", "")
|
||||
if content_type != "text":
|
||||
filtered_list.append(content_block)
|
||||
continue
|
||||
stripped_text: Final[str | None] = strip_claude_code_identity(text)
|
||||
if stripped_text is None:
|
||||
continue
|
||||
# Only copy the block when the text changed.
|
||||
if stripped_text == text:
|
||||
filtered_list.append(content_block)
|
||||
else:
|
||||
filtered_list.append({**content_block, "text": stripped_text})
|
||||
else:
|
||||
# Keep non-dict items as-is
|
||||
filtered_list.append(content_block)
|
||||
return filtered_list if len(filtered_list) > 0 else None
|
||||
else:
|
||||
return system_param
|
||||
|
||||
@staticmethod
|
||||
def _filter_billing_headers_from_system(system_param):
|
||||
"""
|
||||
|
|
@ -529,6 +580,14 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig):
|
|||
else:
|
||||
anthropic_messages_optional_request_params.pop("system", None)
|
||||
|
||||
stripped_identity_system: Final = anthropic_messages_optional_request_params.get("system")
|
||||
if self.should_strip_claude_code_identity() and stripped_identity_system is not None:
|
||||
identity_filtered: Final = self._strip_claude_code_identity_from_system(stripped_identity_system)
|
||||
if identity_filtered is not None and len(identity_filtered) > 0:
|
||||
anthropic_messages_optional_request_params["system"] = identity_filtered
|
||||
else:
|
||||
anthropic_messages_optional_request_params.pop("system", None)
|
||||
|
||||
# Transform context_management from OpenAI format to Anthropic format if needed
|
||||
context_management_param: Final = anthropic_messages_optional_request_params.get("context_management")
|
||||
if context_management_param is not None:
|
||||
|
|
|
|||
|
|
@ -29,6 +29,9 @@ class DeepSeekAnthropicMessagesConfig(AnthropicMessagesConfig):
|
|||
def should_strip_billing_metadata(self) -> bool:
|
||||
return True
|
||||
|
||||
def should_strip_claude_code_identity(self) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_api_key(api_key: str | None = None) -> str | None:
|
||||
return api_key or get_secret_str("DEEPSEEK_API_KEY") or litellm.api_key
|
||||
|
|
|
|||
|
|
@ -31,6 +31,9 @@ class MinimaxMessagesConfig(AnthropicMessagesConfig):
|
|||
def should_strip_billing_metadata(self) -> bool:
|
||||
return True
|
||||
|
||||
def should_strip_claude_code_identity(self) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_api_key(api_key: str | None = None) -> str | None:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -32,6 +32,9 @@ class OpenAILikeAnthropicMessagesConfig(AnthropicMessagesConfig):
|
|||
super().__init__()
|
||||
self._cache_control_ttl: Final = cache_control_ttl
|
||||
|
||||
def should_strip_claude_code_identity(self) -> bool:
|
||||
return True
|
||||
|
||||
def validate_anthropic_messages_environment(
|
||||
self,
|
||||
headers: dict[str, str],
|
||||
|
|
|
|||
|
|
@ -30,6 +30,9 @@ class TencentAnthropicMessagesConfig(AnthropicMessagesConfig):
|
|||
def should_strip_billing_metadata(self) -> bool:
|
||||
return True
|
||||
|
||||
def should_strip_claude_code_identity(self) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_api_key(api_key: str | None = None) -> str | None:
|
||||
return api_key or get_secret_str("TENCENT_API_KEY") or litellm.api_key
|
||||
|
|
|
|||
|
|
@ -5894,6 +5894,120 @@ def test_should_strip_billing_metadata_by_provider(
|
|||
assert config_cls().should_strip_billing_metadata() is expected_strip
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"module_path, class_name, expected_strip",
|
||||
[
|
||||
# First-party and Claude-serving providers keep the identity sentence: the
|
||||
# model genuinely *is* Claude, even via Bedrock/Vertex/Azure.
|
||||
("litellm.llms.anthropic.chat.transformation", "AnthropicConfig", False),
|
||||
(
|
||||
"litellm.llms.anthropic.experimental_pass_through.messages.transformation",
|
||||
"AnthropicMessagesConfig",
|
||||
False,
|
||||
),
|
||||
(
|
||||
"litellm.llms.bedrock.claude_platform.transformation",
|
||||
"BedrockClaudePlatformConfig",
|
||||
False,
|
||||
),
|
||||
(
|
||||
"litellm.llms.vertex_ai.vertex_ai_partner_models.anthropic.transformation",
|
||||
"VertexAIAnthropicConfig",
|
||||
False,
|
||||
),
|
||||
("litellm.llms.azure_ai.anthropic.transformation", "AzureAnthropicConfig", False),
|
||||
# Non-Claude Anthropic-compatible endpoints strip the false self-description.
|
||||
("litellm.llms.minimax.messages.transformation", "MinimaxMessagesConfig", True),
|
||||
(
|
||||
"litellm.llms.deepseek.messages.transformation",
|
||||
"DeepSeekAnthropicMessagesConfig",
|
||||
True,
|
||||
),
|
||||
(
|
||||
"litellm.llms.tencent.messages.transformation",
|
||||
"TencentAnthropicMessagesConfig",
|
||||
True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_should_strip_claude_code_identity_by_provider(
|
||||
module_path, class_name, expected_strip
|
||||
):
|
||||
import importlib
|
||||
|
||||
config_cls = getattr(importlib.import_module(module_path), class_name)
|
||||
assert config_cls().should_strip_claude_code_identity() is expected_strip
|
||||
|
||||
|
||||
def _system_with_identity_block(identity_sentence: str) -> list:
|
||||
return [
|
||||
{
|
||||
"role": "system",
|
||||
"content": [
|
||||
# Claude Code sends the identity sentence as its own text block.
|
||||
{"type": "text", "text": identity_sentence},
|
||||
{"type": "text", "text": "real system prompt"},
|
||||
],
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_messages_request_strips_claude_code_identity_for_minimax():
|
||||
from litellm.llms.minimax.messages.transformation import MinimaxMessagesConfig
|
||||
from litellm.types.router import GenericLiteLLMParams
|
||||
|
||||
config = MinimaxMessagesConfig()
|
||||
assert config.should_strip_claude_code_identity() is True
|
||||
|
||||
optional_params = {
|
||||
"max_tokens": 16,
|
||||
"system": [
|
||||
{"type": "text", "text": "You are Claude Code, Anthropic's official CLI for Claude."},
|
||||
{"type": "text", "text": "real system prompt"},
|
||||
],
|
||||
}
|
||||
result = config.transform_anthropic_messages_request(
|
||||
model="MiniMax-M2",
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
anthropic_messages_optional_request_params=optional_params,
|
||||
litellm_params=GenericLiteLLMParams(),
|
||||
headers={},
|
||||
)
|
||||
|
||||
texts = [block["text"] for block in result.get("system", [])]
|
||||
assert "You are Claude Code, Anthropic's official CLI for Claude." not in texts
|
||||
assert "real system prompt" in texts
|
||||
|
||||
|
||||
def test_messages_request_keeps_claude_code_identity_for_first_party():
|
||||
from litellm.llms.anthropic.experimental_pass_through.messages.transformation import (
|
||||
AnthropicMessagesConfig,
|
||||
)
|
||||
from litellm.types.router import GenericLiteLLMParams
|
||||
|
||||
config = AnthropicMessagesConfig()
|
||||
assert config.should_strip_claude_code_identity() is False
|
||||
|
||||
optional_params = {
|
||||
"max_tokens": 16,
|
||||
"system": [
|
||||
{"type": "text", "text": "You are Claude Code, Anthropic's official CLI for Claude."},
|
||||
{"type": "text", "text": "real system prompt"},
|
||||
],
|
||||
}
|
||||
result = config.transform_anthropic_messages_request(
|
||||
model="claude-3-5-sonnet-latest",
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
anthropic_messages_optional_request_params=optional_params,
|
||||
litellm_params=GenericLiteLLMParams(),
|
||||
headers={},
|
||||
)
|
||||
|
||||
texts = [block["text"] for block in result.get("system", [])]
|
||||
assert "You are Claude Code, Anthropic's official CLI for Claude." in texts
|
||||
assert "real system prompt" in texts
|
||||
|
||||
|
||||
def test_namespace_tool_flat_nested_tools_are_extracted():
|
||||
"""Codex sends nested tools in flat format {type, name, description, parameters} with no 'function' wrapper.
|
||||
These must be normalized and mapped without raising KeyError: 'function'."""
|
||||
|
|
|
|||
|
|
@ -53,6 +53,30 @@ def test_is_claude_code_one_shot_subagent_request(messages, system, expected):
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"text,expected",
|
||||
[
|
||||
# Identity sentence alone -> drop the whole block.
|
||||
("You are Claude Code, Anthropic's official CLI for Claude.", None),
|
||||
# Identity sentence followed by the rest of the system prompt -> keep the rest.
|
||||
(
|
||||
"You are Claude Code, Anthropic's official CLI for Claude.\nYou are an interactive agent.",
|
||||
"You are an interactive agent.",
|
||||
),
|
||||
# Leading/trailing whitespace around the bare sentence is treated as drop-only.
|
||||
(" You are Claude Code, Anthropic's official CLI for Claude. ", None),
|
||||
# Non-Claude-Code prompts are untouched.
|
||||
("You are a helpful assistant.", "You are a helpful assistant."),
|
||||
# A sentence that merely mentions Claude Code is untouched.
|
||||
("You are Claude Code's helper.", "You are Claude Code's helper."),
|
||||
],
|
||||
)
|
||||
def test_strip_claude_code_identity(text, expected):
|
||||
from litellm.llms.anthropic.common_utils import strip_claude_code_identity
|
||||
|
||||
assert strip_claude_code_identity(text) == expected
|
||||
|
||||
|
||||
class TestOptionallyHandleAnthropicOAuth:
|
||||
"""Tests for optionally_handle_anthropic_oauth function."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue