Skip to content

fix: apply custom_headers to Anthropic requests and honor API context_length#2848

Open
romiluz13 wants to merge 1 commit intoantinomyhq:mainfrom
romiluz13:fix/anthropic-custom-headers-and-context-length
Open

fix: apply custom_headers to Anthropic requests and honor API context_length#2848
romiluz13 wants to merge 1 commit intoantinomyhq:mainfrom
romiluz13:fix/anthropic-custom-headers-and-context-length

Conversation

@romiluz13
Copy link
Copy Markdown

@romiluz13 romiluz13 commented Apr 5, 2026

Summary

  • Bug 1: custom_headers not applied to Anthropic requests — Provider-level custom_headers defined in .forge.toml are parsed during config loading but never injected into outgoing HTTP requests for Anthropic-type providers. This breaks any proxy that requires alternative header names (e.g., Azure APIM expects api-key instead of x-api-key). Fixed by appending custom_headers in get_headers() after standard headers are built.

  • Bug 2: API-reported context_length ignored — The Model struct in the Anthropic response DTO only deserializes id and display_name, discarding the context_length field returned by /v1/models. Instead, get_context_length() hardcodes all Claude models to 200K. This prevents custom providers from advertising larger context windows (e.g., 1M tokens with the context-1m-2025-08-07 beta header). Fixed by adding context_length: Option<u64> to Model and preferring the API value over the hardcoded fallback.

Motivation

When using Forge with Azure APIM-based proxies (which require api-key header instead of x-api-key), the [providers.custom_headers] config option has no effect for Anthropic providers. Users have to stand up a local reverse proxy just to translate headers — defeating the purpose of the config.

Similarly, operators serving a custom /v1/models endpoint that reports 1M context windows (enabled via the Anthropic beta header) cannot propagate that information to Forge because the context_length field is never deserialized.

Changes

File Change
crates/forge_repo/src/provider/anthropic.rs Append provider.custom_headers in get_headers() + test
crates/forge_app/src/dto/anthropic/response.rs Add context_length field to Model, prefer API value over fallback + tests

Test plan

  • New test: test_get_headers_includes_custom_headers — verifies custom headers are appended alongside standard headers
  • New test: test_model_conversion_api_context_length_takes_precedence — verifies API-reported 1M overrides hardcoded 200K
  • New test: test_model_conversion_unknown_model_with_api_context_length — verifies unknown models work with API context_length
  • Existing tests updated with context_length: None to match new struct field
  • CI: cargo test passes (no Rust toolchain available locally — relying on CI)

…_length

Two bugs fixed:

1. Provider-level `custom_headers` from config are parsed but never
   injected into Anthropic HTTP requests. This breaks proxies that
   require alternative header names (e.g. Azure APIM's `api-key`
   instead of `x-api-key`). Fix: append custom_headers in
   `get_headers()` after standard headers.

2. The `Model` struct ignores the `context_length` field returned by
   the API's `/v1/models` endpoint. Instead, `get_context_length()`
   hardcodes all Claude models to 200K. This prevents operators from
   advertising larger context windows (e.g. 1M with the
   `context-1m-2025-08-07` beta). Fix: deserialize `context_length`
   from the API response and prefer it over the hardcoded fallback.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 5, 2026

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the type: fix Iterations on existing features or infrastructure. label Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: fix Iterations on existing features or infrastructure.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants