Skip to content

Add ANTHROPIC_CUSTOM_HEADERS for gateway auth - #6

Open
pre wants to merge 1 commit into
boldsoftware:mainfrom
pre:feat/custom-headers
Open

Add ANTHROPIC_CUSTOM_HEADERS for gateway auth#6
pre wants to merge 1 commit into
boldsoftware:mainfrom
pre:feat/custom-headers

Conversation

@pre

@pre pre commented Aug 4, 2026

Copy link
Copy Markdown

Pointing ANTHROPIC_BASE_URL at a corporate LLM gateway fronted by Azure API Management fails with a 401: the gateway authenticates on Ocp-Apim-Subscription-Key, a header meat has no way to send.

This adds $ANTHROPIC_CUSTOM_HEADERS, parsed in the same Name: value per-line format Claude Code already uses, so the same env var configures both tools.

Behavior

  • Headers are applied after the defaults, so a gateway can override anthropic-version when it needs to.
  • A malformed line, a non-token field-name, or a control character in a value is a hard error. Silently dropping an auth header just resurfaces as an opaque 401 from the gateway.
  • The value is a credential, so it is never echoed back in an error message.
  • Their presence counts as configuration. A gateway-authenticated user may have no ANTHROPIC_API_KEY at all, so an empty key no longer falls through to exe.dev discovery, and no longer trips the Generate guard. An empty x-api-key is not sent.

Behavior is unchanged when the variable is unset.

Testing

12 new cases: table-driven parsing and rejection tests, an error-does-not-leak-the-value test, and httptest assertions that the header reaches the wire, that it overrides a default, that header-only auth works, and that no credential at all still errors.

go build ./..., go vet ./..., and go test -race ./... pass; gofmt clean. Also verified end to end against a real APIM-fronted gateway, where it produced an abridged diff instead of the 401.

Gateways fronted by Azure API Management authenticate on a header meat
never sent, so pointing ANTHROPIC_BASE_URL at one returned a 401 with no
way to supply the missing credential.

Parse $ANTHROPIC_CUSTOM_HEADERS in the same "Name: value" per line format
Claude Code uses, and apply the headers after the defaults so a gateway
can override one. A malformed line, an invalid field-name, or a control
character in a value is a hard error, since a silently dropped auth
header resurfaces as an opaque 401. The value is a credential, so it is
never echoed in an error.

Their presence also counts as configuration: a gateway-authenticated user
may have no ANTHROPIC_API_KEY at all, so an empty key no longer falls
through to exe.dev discovery or fails the Generate guard.
@pre
pre force-pushed the feat/custom-headers branch from 07058b3 to 1e9ff22 Compare August 4, 2026 09:48
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