Skip to content

Conversation

@ibetitsmike
Copy link

Overview

This PR adds support for the ANTHROPIC_CUSTOM_HEADERS environment variable, allowing users to inject custom HTTP headers into all Anthropic API requests.

Changes

  • Configuration: Added CustomHeaders field to AnthropicConfig
  • Parsing: Implemented parseCustomHeaders() to parse the environment variable
    • Format: Name: Value with multiple headers separated by newlines
    • Handles edge cases: URLs with colons, whitespace, empty lines, malformed headers
  • Injection: Modified newAnthropicClient() to inject headers using option.WithHeader()
  • Tests: Added 15 comprehensive test cases
    • TestParseCustomHeaders: 12 test cases for parsing logic
    • TestCustomHeadersIntegration: 3 test cases for API integration
  • Compatibility: Fixed test helpers for new config structure

Usage Example

# Single header
export ANTHROPIC_CUSTOM_HEADERS="X-Custom-Header: my-value"

# Multiple headers
export ANTHROPIC_CUSTOM_HEADERS="X-Header-1: value1
X-Header-2: value2"

# Headers with complex values (URLs, etc.)
export ANTHROPIC_CUSTOM_HEADERS="X-Callback-URL: https://example.com:8080/webhook"

Testing

✅ All tests pass (1.486s)
✅ Race detection passes (2.463s)
✅ No linting issues introduced
✅ Code formatted with gofumpt

Files Modified

  • config.go - Added CustomHeaders field
  • provider_anthropic.go - Header parsing and initialization
  • intercept_anthropic_messages_base.go - Header injection
  • anthropic_internal_test.go - Comprehensive tests
  • bridge_integration_test.go - Test helper fixes

Total: 285 lines added across 5 files

This change allows users to inject custom HTTP headers into all Anthropic API requests via the ANTHROPIC_CUSTOM_HEADERS environment variable.

The format is 'Name: Value' with multiple headers separated by newlines.

Changes:
- Added CustomHeaders field to AnthropicConfig
- Implemented parseCustomHeaders() to parse the environment variable
- Modified newAnthropicClient() to inject headers using option.WithHeader()
- Added comprehensive tests (15 test cases) for parsing and integration
- Fixed test helpers for new config structure

The implementation handles edge cases like URLs with colons, whitespace, empty lines, and malformed headers gracefully.

All tests pass including race detection.
Copy link
Collaborator

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

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

Can you augment this to allow custom headers to be given generically, and then we inject them regardless of the provider?

@matifali
Copy link
Member

Can you augment this to allow custom headers to be given generically, and then we inject them regardless of the provider?

Can there be a case where the provided headers don't apply to both OpenAI and Anthropic? Just make sure we handle both of them independently and gracefully (if applicable)

@dannykopping
Copy link
Collaborator

Can you augment this to allow custom headers to be given generically, and then we inject them regardless of the provider?

Can there be a case where the provided headers don't apply to both OpenAI and Anthropic? Just make sure we handle both of them independently and gracefully (if applicable)

We have provider-specific config, and they both embed the ProviderConfig type: https://github.com/coder/aibridge/blob/main/config.go

So yes, one could configure it on a per-provider basis.

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.

3 participants