[JS] anthropic plugin: per-request baseUrl and customHeaders, as googleai has
What: Let a request's config set the Anthropic client's base URL and extra headers for that one call, under the names the googleai plugin already uses: baseUrl and customHeaders. Optionally also maxRetries.
Why: We route model calls through an LLM proxy (LiteLLM) that bills each call to the right customer from per-call headers, and we send a call straight to the provider when the proxy is down. With googleai, that is three config fields per call: baseUrl, apiKey and customHeaders, which the plugin re-reads on every request. The anthropic plugin takes apiKey per request (and apiKey: false at plugin level), but has no per-request URL or headers. The workarounds are worse:
ANTHROPIC_BASE_URLand default headers are process-wide, so they can't vary per call.- The plugin can't take a caller-built client: the test-client option is internal and isn't exported.
- A wrapped global
fetchdoesn't see the SDK's own retries, and it affects every other library in the process.
Today we carry a pnpm patch on 0.3.0 that does this.
Proposal (0.7.0, claudeRunner in models.ts, which already builds a client per request):
const { baseUrl, customHeaders, maxRetries } = normalizedRequest.config ?? {};
const client = testClient ?? new Anthropic({
apiKey: calculateApiKey(pluginApiKey, normalizedRequest.config?.apiKey),
...(baseUrl !== undefined && { baseURL: baseUrl }),
...(customHeaders !== undefined && { defaultHeaders: customHeaders }),
...(maxRetries !== undefined && { maxRetries }),
});
Also:
- declare the three fields in
AnthropicConfigSchema. googleai declaresbaseUrland readscustomHeaderswithout declaring it, socustomHeadersonly gets through because the schema is.passthrough(); declaring it here avoids that; - drop them in the stable and beta request-body builders, where
apiKeyis already dropped, so they never reach the request body.
Tests: a request with baseUrl and customHeaders reaches that URL with those headers, and none of the three fields appears in the request body. A request without them behaves as today.
Happy to send the PR if this shape is welcome.
Triage: P3 · S · stop: needs design decision
Area: js
Root cause: claudeRunner in js/plugins/anthropic/src/models.ts constructs a new Anthropic client per request using only apiKey, and neither AnthropicConfigSchema nor PluginOptions in js/plugins/anthropic/src/types.ts exposes baseUrl, customHeaders, or maxRetries.
Options:
- Per-request and plugin-level
baseUrlandcustomHeaders(Recommended): AddbaseUrlandcustomHeaders(z.record(z.string(), z.string())) to bothPluginOptionsandAnthropicConfigSchema(matchinggoogleai), resolve request overrides over plugin defaults inclaudeRunnerandlist(), and strip them inRunnerandBetaRunnerrequest-body builders. - Per-request
baseUrl,customHeaders, andmaxRetriesonly: Add the three fields only toAnthropicConfigSchema, pass them tonew Anthropic(...)inclaudeRunner, and strip them from request bodies. - Anthropic SDK naming (
baseURL,defaultHeaders,maxRetries): Expose the underlying Anthropic SDKClientOptionsnames instead ofgoogleainames.
Details
js/plugins/anthropic/src/models.ts(claudeRunner, lines 162-169): Instantiatesnew Anthropic({ apiKey: calculateApiKey(...) })on each request whentestClientis not injected.js/plugins/anthropic/src/types.ts(PluginOptions, lines 35-45;AnthropicConfigSchema, lines 129-191): Only definesapiKeyandapiVersionfor connection configuration. BecauseAnthropicConfigSchemauses.passthrough(), any extra config keys not destructured inRunner.toAnthropicRequestBody/toAnthropicStreamingRequestBody(js/plugins/anthropic/src/runner/stable.ts, lines 240-250 and 293-303) andBetaRunner.toAnthropicRequestBody/toAnthropicStreamingRequestBody(js/plugins/anthropic/src/runner/beta.ts, lines 322-332 and 381-391) are spread via...restConfiginto the Anthropic messages request body.js/plugins/google-genai/src/googleai/types.ts(lines 63-91) andgemini.ts(lines 195-211) definebaseUrlandcustomHeadersonGoogleAIPluginOptionsandbaseUrlonGeminiConfigSchema, applying per-request overrides incalculateRequestOptions(js/plugins/google-genai/src/googleai/utils.ts, lines 161-188).
Run d4d96e91-5581-4447-8f3d-d0b26d9d20eb · Cloud Run logs