Skip to content

[LLM translation] Refactor Anthropic Configurations and Add Support for anthropic_beta Headers #13590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions docs/my-website/docs/providers/bedrock.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,150 @@ Same as [Anthropic API response](../providers/anthropic#usage---thinking--reason
Same as [Anthropic API response](../providers/anthropic#usage---thinking--reasoning_content).


## Usage - Anthropic Beta Features

LiteLLM supports Anthropic's beta features on AWS Bedrock through the `anthropic-beta` header. This enables access to experimental features like:

- **1M Context Window** - Up to 1 million tokens of context (Claude Sonnet 4)
- **Computer Use Tools** - AI that can interact with computer interfaces
- **Token-Efficient Tools** - More efficient tool usage patterns
- **Extended Output** - Up to 128K output tokens
- **Enhanced Thinking** - Advanced reasoning capabilities

### Supported Beta Features

| Beta Feature | Header Value | Compatible Models | Description |
|--------------|-------------|------------------|-------------|
| 1M Context Window | `context-1m-2025-08-07` | Claude Sonnet 4 | Enable 1 million token context window |
| Computer Use (Latest) | `computer-use-2025-01-24` | Claude 3.7 Sonnet | Latest computer use tools |
| Computer Use (Legacy) | `computer-use-2024-10-22` | Claude 3.5 Sonnet v2 | Computer use tools for Claude 3.5 |
| Token-Efficient Tools | `token-efficient-tools-2025-02-19` | Claude 3.7 Sonnet | More efficient tool usage |
| Interleaved Thinking | `interleaved-thinking-2025-05-14` | Claude 4 models | Enhanced thinking capabilities |
| Extended Output | `output-128k-2025-02-19` | Claude 3.7 Sonnet | Up to 128K output tokens |
| Developer Thinking | `dev-full-thinking-2025-05-14` | Claude 4 models | Raw thinking mode for developers |

<Tabs>
<TabItem value="sdk" label="SDK">

**Single Beta Feature**

```python
from litellm import completion
import os

# set env
os.environ["AWS_ACCESS_KEY_ID"] = ""
os.environ["AWS_SECRET_ACCESS_KEY"] = ""
os.environ["AWS_REGION_NAME"] = ""

# Use 1M context window with Claude Sonnet 4
response = completion(
model="bedrock/anthropic.claude-sonnet-4-20250115-v1:0",
messages=[{"role": "user", "content": "Hello! Testing 1M context window."}],
max_tokens=100,
extra_headers={
"anthropic-beta": "context-1m-2025-08-07" # 👈 Enable 1M context
}
)
```

**Multiple Beta Features**

```python
from litellm import completion

# Combine multiple beta features (comma-separated)
response = completion(
model="bedrock/converse/anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[{"role": "user", "content": "Testing multiple beta features"}],
max_tokens=100,
extra_headers={
"anthropic-beta": "computer-use-2024-10-22,context-1m-2025-08-07"
}
)
```

**Computer Use Tools with Beta Features**

```python
from litellm import completion

# Computer use tools automatically add computer-use-2024-10-22
# You can add additional beta features
response = completion(
model="bedrock/converse/anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[{"role": "user", "content": "Take a screenshot"}],
tools=[{
"type": "computer_20241022",
"name": "computer",
"display_width_px": 1920,
"display_height_px": 1080
}],
extra_headers={
"anthropic-beta": "context-1m-2025-08-07" # Additional beta feature
}
)
```

</TabItem>
<TabItem value="proxy" label="PROXY">

**Set on YAML Config**

```yaml
model_list:
- model_name: claude-sonnet-4-1m
litellm_params:
model: bedrock/anthropic.claude-sonnet-4-20250115-v1:0
extra_headers:
anthropic-beta: "context-1m-2025-08-07" # 👈 Enable 1M context

- model_name: claude-computer-use
litellm_params:
model: bedrock/converse/anthropic.claude-3-5-sonnet-20241022-v2:0
extra_headers:
anthropic-beta: "computer-use-2024-10-22,context-1m-2025-08-07"

general_settings:
forward_client_headers_to_llm_api: true # 👈 Required for client-side header forwarding
```

**Set on Request**

```python
import openai

client = openai.OpenAI(
api_key="anything",
base_url="http://0.0.0.0:4000"
)

response = client.chat.completions.create(
model="claude-sonnet-4-1m",
messages=[{
"role": "user",
"content": "Testing 1M context window"
}],
extra_headers={
"anthropic-beta": "context-1m-2025-08-07"
}
)
```

:::info
**For client-side header forwarding**: When using the proxy and sending `anthropic-beta` headers from the client (like the OpenAI SDK), you need to enable `forward_client_headers_to_llm_api: true` in your proxy's `general_settings`. This tells the proxy to extract headers from HTTP requests and forward them to the underlying LLM provider.
:::

</TabItem>
</Tabs>

:::info

Beta features may require special access or permissions in your AWS account. Some features are only available in specific AWS regions. Check the [AWS Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html) for availability and access requirements.

:::


## Usage - Structured Output / JSON mode

<Tabs>
Expand Down
4 changes: 2 additions & 2 deletions litellm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ def add_known_models():
AnthropicMessagesConfig,
)
from .llms.bedrock.messages.invoke_transformations.anthropic_claude3_transformation import (
AmazonAnthropicClaude3MessagesConfig,
AmazonAnthropicClaudeMessagesConfig,
)
from .llms.together_ai.chat import TogetherAIConfig
from .llms.together_ai.completion.transformation import TogetherAITextCompletionConfig
Expand Down Expand Up @@ -1101,7 +1101,7 @@ def add_known_models():
AmazonAnthropicConfig,
)
from .llms.bedrock.chat.invoke_transformations.anthropic_claude3_transformation import (
AmazonAnthropicClaude3Config,
AmazonAnthropicClaudeConfig,
)
from .llms.bedrock.chat.invoke_transformations.amazon_cohere_transformation import (
AmazonCohereConfig,
Expand Down
5 changes: 5 additions & 0 deletions litellm/llms/bedrock/chat/converse_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async def async_streaming(
messages=messages,
optional_params=optional_params,
litellm_params=litellm_params,
headers=headers,
)
data = json.dumps(request_data)

Expand Down Expand Up @@ -185,8 +186,10 @@ async def async_completion(
messages=messages,
optional_params=optional_params,
litellm_params=litellm_params,
headers=headers,
)
data = json.dumps(request_data)

prepped = self.get_request_headers(
credentials=credentials,
aws_region_name=litellm_params.get("aws_region_name") or "us-west-2",
Expand Down Expand Up @@ -390,8 +393,10 @@ def completion( # noqa: PLR0915
messages=messages,
optional_params=optional_params,
litellm_params=litellm_params,
headers=extra_headers,
)
data = json.dumps(_data)

prepped = self.get_request_headers(
credentials=credentials,
aws_region_name=aws_region_name,
Expand Down
28 changes: 26 additions & 2 deletions litellm/llms/bedrock/chat/converse_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
)
from litellm.utils import add_dummy_tool, has_tool_call_blocks, supports_reasoning

from ..common_utils import BedrockError, BedrockModelInfo, get_bedrock_tool_name
from ..common_utils import BedrockError, BedrockModelInfo, get_bedrock_tool_name, get_anthropic_beta_from_headers

# Computer use tool prefixes supported by Bedrock
BEDROCK_COMPUTER_USE_TOOLS = [
Expand Down Expand Up @@ -593,12 +593,14 @@ def _handle_top_k_value(self, model: str, inference_params: dict) -> dict:

return {}


def _transform_request_helper(
self,
model: str,
system_content_blocks: List[SystemContentBlock],
optional_params: dict,
messages: Optional[List[AllMessageValues]] = None,
headers: Optional[dict] = None,
) -> CommonRequestObject:
## VALIDATE REQUEST
"""
Expand Down Expand Up @@ -651,6 +653,12 @@ def _transform_request_helper(
# Initialize bedrock_tools
bedrock_tools: List[ToolBlock] = []

# Collect anthropic_beta values from user headers
anthropic_beta_list = []
if headers:
user_betas = get_anthropic_beta_from_headers(headers)
anthropic_beta_list.extend(user_betas)

# Only separate tools if computer use tools are actually present
if original_tools and self.is_computer_use_tool_used(original_tools, model):
# Separate computer use tools from regular function tools
Expand All @@ -663,14 +671,25 @@ def _transform_request_helper(

# Add computer use tools and anthropic_beta if needed (only when computer use tools are present)
if computer_use_tools:
additional_request_params["anthropic_beta"] = ["computer-use-2024-10-22"]
anthropic_beta_list.append("computer-use-2024-10-22")
# Transform computer use tools to proper Bedrock format
transformed_computer_tools = self._transform_computer_use_tools(computer_use_tools)
additional_request_params["tools"] = transformed_computer_tools
else:
# No computer use tools, process all tools as regular tools
bedrock_tools = _bedrock_tools_pt(original_tools)

# Set anthropic_beta in additional_request_params if we have any beta features
if anthropic_beta_list:
# Remove duplicates while preserving order
unique_betas = []
seen = set()
for beta in anthropic_beta_list:
if beta not in seen:
unique_betas.append(beta)
seen.add(beta)
additional_request_params["anthropic_beta"] = unique_betas

bedrock_tool_config: Optional[ToolConfigBlock] = None
if len(bedrock_tools) > 0:
tool_choice_values: ToolChoiceValuesBlock = inference_params.pop(
Expand Down Expand Up @@ -708,6 +727,7 @@ async def _async_transform_request(
messages: List[AllMessageValues],
optional_params: dict,
litellm_params: dict,
headers: Optional[dict] = None,
) -> RequestObject:
messages, system_content_blocks = self._transform_system_message(messages)
## TRANSFORMATION ##
Expand All @@ -717,6 +737,7 @@ async def _async_transform_request(
system_content_blocks=system_content_blocks,
optional_params=optional_params,
messages=messages,
headers=headers,
)

bedrock_messages = (
Expand Down Expand Up @@ -747,6 +768,7 @@ def transform_request(
messages=messages,
optional_params=optional_params,
litellm_params=litellm_params,
headers=headers,
),
)

Expand All @@ -756,6 +778,7 @@ def _transform_request(
messages: List[AllMessageValues],
optional_params: dict,
litellm_params: dict,
headers: Optional[dict] = None,
) -> RequestObject:
messages, system_content_blocks = self._transform_system_message(messages)

Expand All @@ -764,6 +787,7 @@ def _transform_request(
system_content_blocks=system_content_blocks,
optional_params=optional_params,
messages=messages,
headers=headers,
)

## TRANSFORMATION ##
Expand Down
2 changes: 1 addition & 1 deletion litellm/llms/bedrock/chat/invoke_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def completion( # noqa: PLR0915
model=model, messages=messages, custom_llm_provider="anthropic_xml"
) # type: ignore
## LOAD CONFIG
config = litellm.AmazonAnthropicClaude3Config.get_config()
config = litellm.AmazonAnthropicClaudeConfig.get_config()
for k, v in config.items():
if (
k not in inference_params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from litellm.llms.bedrock.chat.invoke_transformations.base_invoke_transformation import (
AmazonInvokeConfig,
)
from litellm.llms.bedrock.common_utils import get_anthropic_beta_from_headers
from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import ModelResponse

Expand All @@ -17,13 +18,22 @@
LiteLLMLoggingObj = Any


class AmazonAnthropicClaude3Config(AmazonInvokeConfig, AnthropicConfig):
class AmazonAnthropicClaudeConfig(AmazonInvokeConfig, AnthropicConfig):
"""
Reference:
https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=claude
https://docs.anthropic.com/claude/docs/models-overview#model-comparison
https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html

Supported Params for the Amazon / Anthropic Claude 3 models:
Supported Params for the Amazon / Anthropic Claude models (Claude 3, Claude 4, etc.):
Supports anthropic_beta parameter for beta features like:
- computer-use-2025-01-24 (Claude 3.7 Sonnet)
- computer-use-2024-10-22 (Claude 3.5 Sonnet v2)
- token-efficient-tools-2025-02-19 (Claude 3.7 Sonnet)
- interleaved-thinking-2025-05-14 (Claude 4 models)
- output-128k-2025-02-19 (Claude 3.7 Sonnet)
- dev-full-thinking-2025-05-14 (Claude 4 models)
- context-1m-2025-08-07 (Claude Sonnet 4)
"""

anthropic_version: str = "bedrock-2023-05-31"
Expand All @@ -50,6 +60,7 @@ def map_openai_params(
drop_params,
)


def transform_request(
self,
model: str,
Expand All @@ -72,6 +83,11 @@ def transform_request(
if "anthropic_version" not in _anthropic_request:
_anthropic_request["anthropic_version"] = self.anthropic_version

# Handle anthropic_beta from user headers
anthropic_beta_list = get_anthropic_beta_from_headers(headers)
if anthropic_beta_list:
_anthropic_request["anthropic_beta"] = anthropic_beta_list

return _anthropic_request

def transform_response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ def transform_request(
] = True # cohere requires stream = True in inference params
request_data = {"prompt": prompt, **inference_params}
elif provider == "anthropic":
return litellm.AmazonAnthropicClaude3Config().transform_request(
transformed_request = litellm.AmazonAnthropicClaudeConfig().transform_request(
model=model,
messages=messages,
optional_params=optional_params,
litellm_params=litellm_params,
headers=headers,
)

return transformed_request
elif provider == "nova":
return litellm.AmazonInvokeNovaConfig().transform_request(
model=model,
Expand Down Expand Up @@ -293,7 +295,7 @@ def transform_response( # noqa: PLR0915
completion_response["generations"][0]["finish_reason"]
)
elif provider == "anthropic":
return litellm.AmazonAnthropicClaude3Config().transform_response(
return litellm.AmazonAnthropicClaudeConfig().transform_response(
model=model,
raw_response=raw_response,
model_response=model_response,
Expand Down
Loading
Loading