Skip to content

fix(bedrock): Filter anthropic-beta header for Bedrock passthrough#20012

Open
brian-lai wants to merge 1 commit intoBerriAI:mainfrom
brian-lai:para/filter-anthropic-beta-header-bedrock
Open

fix(bedrock): Filter anthropic-beta header for Bedrock passthrough#20012
brian-lai wants to merge 1 commit intoBerriAI:mainfrom
brian-lai:para/filter-anthropic-beta-header-bedrock

Conversation

@brian-lai
Copy link

@brian-lai brian-lai commented Jan 29, 2026

Summary

  • Filters out anthropic-beta header from requests before forwarding to AWS Bedrock
  • Prevents "invalid beta flag" errors when using Claude Code or other clients that send Anthropic beta headers through LiteLLM proxy to Bedrock

Problem

When using Claude Code with LiteLLM as a proxy to AWS Bedrock, requests fail with:

{"message":"invalid beta flag"}

This happens because Claude Code sends anthropic-beta: oauth-2025-04-20 (or other beta values) headers, which AWS Bedrock doesn't recognize or support.

Solution

Filter the anthropic-beta header (case-insensitively) in BedrockPassthroughConfig.sign_request() before the request is signed and forwarded to Bedrock.

Key points:

  • Filtering happens before AWS SigV4 signing (critical for signature validity)
  • Case-insensitive matching handles Anthropic-Beta, ANTHROPIC-BETA, etc.
  • Other headers like anthropic-version are preserved

Test plan

  • Unit tests added covering:
    • Basic header filtering
    • Case-insensitive filtering
    • Multiple beta value types
    • Preservation of other headers
    • Edge cases (empty headers, only-beta-header)
  • Manual testing with Claude Code + LiteLLM proxy + Bedrock

Run tests:

pytest tests/test_litellm/llms/bedrock/passthrough/test_bedrock_header_filtering.py -v

Related

🤖 Generated with Claude Code

@vercel
Copy link

vercel bot commented Jan 29, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 3, 2026 2:13am

Request Review

@CLAassistant
Copy link

CLAassistant commented Jan 29, 2026

CLA assistant check
All committers have signed the CLA.

@hunterchris
Copy link

💣 We are having the same issues when using the claude-code action through litellm; we are running 1.81.3-nightly.

AWS Bedrock only supports a specific whitelist of beta flags and rejects
requests with "invalid beta flag" error for unsupported ones (e.g.,
mcp-servers-2025-12-04, oauth-2025-04-20 from Claude Code).

This implements whitelist-based filtering in BedrockPassthroughConfig
to filter the anthropic-beta header before signing and forwarding
requests, consistent with the Invoke API approach in PR BerriAI#19877.

Supported beta flags (per AWS docs):
- computer-use-2024-10-22, computer-use-2025-01-24
- token-efficient-tools-2025-02-19
- interleaved-thinking-2025-05-14
- output-128k-2025-02-19
- dev-full-thinking-2025-05-14
- context-1m-2025-08-07
- context-management-2025-06-27
- effort-2025-11-24
- tool-search-tool-2025-10-19
- tool-examples-2025-10-29

Fixes: BerriAI#16726

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@brian-lai brian-lai force-pushed the para/filter-anthropic-beta-header-bedrock branch from 73f66ea to e91c0fd Compare February 3, 2026 02:11
@brian-lai brian-lai marked this pull request as ready for review February 3, 2026 02:20
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 3, 2026

Greptile Overview

Greptile Summary

Filters unsupported anthropic-beta header values before forwarding requests to AWS Bedrock, preventing "invalid beta flag" errors when clients like Claude Code send headers that Bedrock doesn't support.

Key Changes

  • Added BEDROCK_SUPPORTED_BETAS whitelist containing 11 beta flags officially supported by AWS Bedrock
  • Implemented _filter_anthropic_beta_header() method that parses comma-separated beta values and filters to only supported ones
  • Filtering occurs before AWS SigV4 signing in sign_request() to maintain signature validity
  • Case-insensitive header matching handles various capitalizations
  • If no supported beta values remain after filtering, the header is omitted entirely

Impact

  • Fixes Claude Code integration with Bedrock when MCP servers are configured (sends mcp-servers-2025-12-04)
  • Fixes OAuth beta header issues (oauth-2025-04-20)
  • Maintains compatibility with supported beta flags like computer-use-2024-10-22, token-efficient-tools-2025-02-19, etc.
  • Consistent with the whitelist-based approach used in Invoke API (PR fix: filter unsupported beta headers for AWS Bedrock Invoke API #19877)

Test Coverage

  • Comprehensive test suite with 11 test cases covering edge cases, case-insensitivity, mixed values, and header preservation
  • All tests properly mock _sign_request to verify filtered headers without requiring AWS credentials

Confidence Score: 5/5

  • Safe to merge - well-tested fix that prevents request failures without breaking existing functionality
  • Clean implementation following existing patterns, comprehensive test coverage (11 tests), proper placement before AWS signing, consistent with Invoke API approach (PR fix: filter unsupported beta headers for AWS Bedrock Invoke API #19877), and solves a real user-facing issue
  • No files require special attention

Important Files Changed

Filename Overview
litellm/llms/bedrock/passthrough/transformation.py Added whitelist-based filtering for anthropic-beta headers to prevent "invalid beta flag" errors from Bedrock
tests/test_litellm/llms/bedrock/passthrough/test_bedrock_header_filtering.py Comprehensive test suite covering all filtering scenarios including edge cases and case-insensitive matching

Sequence Diagram

sequenceDiagram
    participant CC as Claude Code
    participant LP as LiteLLM Proxy
    participant BPC as BedrockPassthroughConfig
    participant AWS as AWS Bedrock
    
    CC->>LP: POST /bedrock/messages<br/>Headers: anthropic-beta: oauth-2025-04-20,mcp-servers-2025-12-04
    LP->>BPC: sign_request(headers, ...)
    BPC->>BPC: _filter_anthropic_beta_header(headers)
    Note over BPC: Parse comma-separated values<br/>Filter against BEDROCK_SUPPORTED_BETAS<br/>oauth-2025-04-20 ❌ (unsupported)<br/>mcp-servers-2025-12-04 ❌ (unsupported)<br/>Header omitted (no supported betas remain)
    BPC->>BPC: _sign_request(filtered_headers, ...)
    Note over BPC: Sign request with AWS SigV4<br/>(without anthropic-beta header)
    BPC-->>LP: Signed headers + body
    LP->>AWS: POST /model/.../invoke<br/>(No anthropic-beta header)
    AWS-->>LP: 200 OK (Success)
    LP-->>CC: 200 OK (Success)
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@hunterchris
Copy link

The lint failure is a pre-existing issue in integrations/opentelemetry.py:1013 (mypy error about LogRecord attribute) that exists on the main branch - it's not related to this PR's changes.

Could this be merged despite the CI failure? The actual fix here is correct and tested.

@brian-lai
Copy link
Author

brian-lai commented Feb 17, 2026

@hunterchris if you're still waiting for this PR to get merged in, try https://github.com/brian-lai/anthropic-beta-stripping-proxy

you can easily set this up wherever you're running your nightly execution of claude code and have claude-code hit the local proxy instead. that will strip ALL beta headers from the request before it gets to litellm

you don't necessarily have to use the repo, but the pattern is simple and easily adaptable for any setup

@Quentin-M
Copy link
Contributor

Quentin-M commented Feb 18, 2026

While we started by striping headers manually in Istio, we now have a much longer term solution for our company.. My PR includes a commit that centralizes and fixes definitely the header stripping issue by setting up a straightforward system that blocks beta headers by default, and then allows beta headers based on the specifics models called & its support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants