Skip to content
IQ Routing

Anthropic · Node

The @anthropic-ai/sdk package accepts baseURL and apiKey on the client constructor. Swap the base URL and your client.messages.create calls route through the gateway.

Drop-in switch

const client = new Anthropic({
  baseURL: "https://gateway.iq-routing.com",
  apiKey: process.env.IQ_GATEWAY_KEY,
});

The gateway exposes the Anthropic surface at /v1/messages; the SDK appends /v1/messages itself when constructing the request URL. Pass the gateway origin only.

Verify it routes

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://gateway.iq-routing.com",
  apiKey: process.env.IQ_GATEWAY_KEY!,
  timeout: 60_000,
  maxRetries: 0,
});

const response = await client.messages
  .create({
    model: "claude-haiku-4-5-20251001",
    max_tokens: 64,
    messages: [{ role: "user", content: "Summarise: routing-aware gateway." }],
  })
  .withResponse();

console.log("X-Request-Id:", response.response.headers.get("x-request-id"));
console.log(response.data.content[0]);

Drop into a fresh pnpm init project with @anthropic-ai/sdk. Set IQ_GATEWAY_KEY in your environment, run with npx tsx verify.ts.

Using capability aliases

The cap:<name> syntax in the model field tells the gateway to pick a concrete model at routing time based on the capability's stable intent rather than a pinned model id. The operator's per-org overrides plus the focus-mode bias plus the circuit-breaker state shape the resolved tuple. The six default capabilities are reason-heavy, tool-call-strict, long-context-128k, vision, cheap-fast, and json-mode.

import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
  baseURL: "https://gateway.iq-routing.com",
  apiKey: process.env.IQ_GATEWAY_KEY!,
});

const message = await anthropic.messages.create({
  model: "cap:reason-heavy",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Plan the migration in three phases." }],
});

The resolved provider plus model surface in the x-iq-routing response header (a JSON-encoded payload with chosen_provider and chosen_model fields, among others), so the SDK consumer can inspect which concrete model handled the request. See the capability aliases docs for the full resolver decision tree plus the override mechanics. The operator can override the default capability mapping via the capabilities dashboard editor at /settings/capabilities.

Common gotchas

The Node SDK defaults to two retries on transient errors. Disable them when calling through the gateway; the gateway's rate-limit cooldown already handles backoff and SDK-side retries double-bill the cooldown.

Streaming via client.messages.stream works transparently. The MessageStream instance exposes .finalMessage() and event listeners that match the direct-to-Anthropic shape exactly.

Tool use (the tools parameter on messages.create) is forwarded without modification. The gateway's tool_choice defaults align with Anthropic's defaults so existing tool-using clients work without behaviour changes.

If you pass extra_headers to set a beta header (anthropic-beta), the gateway forwards the value verbatim to Anthropic when the request routes to an Anthropic model. The header gates request-shape features (extended thinking, structured output, tool defer_loading, context editing) whose paired body fields alone are not enough, so it has to reach the provider unchanged. When the request routes to a non-Anthropic provider the header is dropped, since it has no meaning there.

Browser usage is rejected by the SDK by default. The gateway's recommended pattern is server-side: call the SDK from your own backend with the gateway base URL; the gateway authenticates with your gw_live_ key, never the upstream Anthropic key.

Try this in the playground

Run this snippet against your gateway key →