genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[JS] anthropic plugin: per-request baseUrl and customHeaders, as googleai has

Open harshith-babs opened this issue 2 weeks ago • 1 comments

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_URL and 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 fetch doesn'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 declares baseUrl and reads customHeaders without declaring it, so customHeaders only gets through because the schema is .passthrough(); declaring it here avoids that;
  • drop them in the stable and beta request-body builders, where apiKey is 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.

harshith-babs avatar Nov 16 '26 15:11 harshith-babs

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:

  1. Per-request and plugin-level baseUrl and customHeaders (Recommended): Add baseUrl and customHeaders (z.record(z.string(), z.string())) to both PluginOptions and AnthropicConfigSchema (matching googleai), resolve request overrides over plugin defaults in claudeRunner and list(), and strip them in Runner and BetaRunner request-body builders.
  2. Per-request baseUrl, customHeaders, and maxRetries only: Add the three fields only to AnthropicConfigSchema, pass them to new Anthropic(...) in claudeRunner, and strip them from request bodies.
  3. Anthropic SDK naming (baseURL, defaultHeaders, maxRetries): Expose the underlying Anthropic SDK ClientOptions names instead of googleai names.
Details
  • js/plugins/anthropic/src/models.ts (claudeRunner, lines 162-169): Instantiates new Anthropic({ apiKey: calculateApiKey(...) }) on each request when testClient is not injected.
  • js/plugins/anthropic/src/types.ts (PluginOptions, lines 35-45; AnthropicConfigSchema, lines 129-191): Only defines apiKey and apiVersion for connection configuration. Because AnthropicConfigSchema uses .passthrough(), any extra config keys not destructured in Runner.toAnthropicRequestBody / toAnthropicStreamingRequestBody (js/plugins/anthropic/src/runner/stable.ts, lines 240-250 and 293-303) and BetaRunner.toAnthropicRequestBody / toAnthropicStreamingRequestBody (js/plugins/anthropic/src/runner/beta.ts, lines 322-332 and 381-391) are spread via ...restConfig into the Anthropic messages request body.
  • js/plugins/google-genai/src/googleai/types.ts (lines 63-91) and gemini.ts (lines 195-211) define baseUrl and customHeaders on GoogleAIPluginOptions and baseUrl on GeminiConfigSchema, applying per-request overrides in calculateRequestOptions (js/plugins/google-genai/src/googleai/utils.ts, lines 161-188).

Run d4d96e91-5581-4447-8f3d-d0b26d9d20eb · Cloud Run logs

genkit-bot avatar Nov 19 '26 16:11 genkit-bot