Skip to content

fix: preserve anthropic auth header name#220

Merged
mxyhi merged 1 commit intomainfrom
anthropic-count-tokens-auth-header
Apr 14, 2026
Merged

fix: preserve anthropic auth header name#220
mxyhi merged 1 commit intomainfrom
anthropic-count-tokens-auth-header

Conversation

@mxyhi
Copy link
Copy Markdown
Owner

@mxyhi mxyhi commented Apr 14, 2026

Summary

This fixes Anthropic-native /v1/messages/count_tokens requests when the inbound client authenticates with Authorization instead of x-api-key.

Problem

A request came in on /v1/messages/count_tokens through the Anthropic wire format and reached an Anthropic-compatible relay that requires Authorization: Bearer .... Token Proxy resolved the upstream as provider=anthropic, but the proxy layer always rewrote Anthropic upstream auth to x-api-key, which caused the relay to reject the forwarded request with Required request header 'Authorization' ... is not present.

Root Cause

resolve_request_auth only retained Anthropic credential values, not the original inbound header name. Later, resolve_upstream_auth unconditionally emitted x-api-key for every Anthropic provider, even when the inbound Anthropic-native request had arrived with Authorization and the target upstream expected that same header name.

Fix

The proxy now records the original auth header shape for Anthropic-native inbound paths (Authorization, x-api-key, or x-anthropic-api-key). When forwarding to an Anthropic provider, Token Proxy preserves that header name and only replaces the value with the configured upstream key when one exists. Non-Anthropic-native inbound paths keep the old fallback behavior so OpenAI/Gemini conversion paths are not changed accidentally.

Validation

I ran:

  • cargo test -p token_proxy_core
  • cargo test -p token_proxy_core proxy::http::tests -- --nocapture
  • cargo test -p token_proxy_core anthropic_count_tokens_preserves_authorization_header_name_for_upstream -- --nocapture
  • cargo test -p token_proxy_core anthropic_upstream_auth_reuses_x_api_key_header_name_with_upstream_key -- --nocapture
  • cargo fmt --all --check

@mxyhi mxyhi marked this pull request as ready for review April 14, 2026 12:10
@mxyhi mxyhi merged commit 7d39da8 into main Apr 14, 2026
1 check passed
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.

1 participant