Anthropic · Python
The Anthropic Python SDK accepts a base_url argument on the client. Point
it at the gateway and the standard client.messages.create calls route
through. This recipe pins claude-haiku-5-5 directly, which
keeps every call in the example with Anthropic; name claude-opus-5-5
or claude-fable-5-1 instead for an exact model. See the Using
capability aliases section below, or pass model="auto", to let the
gateway pick the model per request instead.
Drop-in switch
client = anthropic.Anthropic(
base_url="https://gateway.iq-routing.com",
api_key="gw_live_xxxxxxxx",
)
Pass the bare origin; the SDK appends /v1/messages itself (see the
Anthropic Python SDK).
Verify it routes
import os
import anthropic
client = anthropic.Anthropic(
base_url="https://gateway.iq-routing.com",
api_key=os.environ["IQ_API_KEY"],
timeout=60.0,
max_retries=0,
)
response = client.messages.with_raw_response.create(
model="claude-haiku-5-5",
max_tokens=64,
messages=[{"role": "user", "content": "Summarise: routing-aware gateway."}],
)
print("X-Request-Id:", response.headers.get("x-request-id"))
parsed = response.parse()
print("Stop reason:", parsed.stop_reason)
print(parsed.content[0].text)
Drop the script into a fresh venv, pip install anthropic, set
IQ_API_KEY in your environment, run. The X-Request-Id header is the
gateway's correlation handle.
Use model="auto" for routing, or pass a model ID to keep the request
within that model's family. Find the request in Requests by its
X-Request-Id to see the model IQ recorded.
Using capability aliases
Pass cap:<name> in the model field to request a capability; check
the x-iq-model response header for the dispatched or recorded model and
manage your workspace's overrides at /settings/capabilities.
import os
import anthropic
client = anthropic.Anthropic(
base_url="https://gateway.iq-routing.com",
api_key=os.environ["IQ_API_KEY"],
)
message = client.messages.create(
model="cap:reason-heavy",
max_tokens=1024,
messages=[{"role": "user", "content": "Plan the migration in three phases."}],
)
Use with_raw_response to read the response headers, as in the Verify it
routes example. When routing telemetry is enabled, replies can also
include x-iq-routing with JSON fields such as chosen_model,
chosen_provider and cache_hit. See the capability aliases
docs for the full list of default capabilities and
how to override them. A workspace owner or admin can override the default
capability mapping in the editor at /settings/capabilities.
Common gotchas
The Anthropic SDK retries some errors by default (see the Retries
section of the Anthropic Python SDK README).
Set max_retries=0 to avoid burning attempts inside the cooldown window
and handle backoff in your own queue.
stream=True works against the gateway. The SDK's
MessageStreamManager consumes the SSE just as it does against
api.anthropic.com. Read X-Request-Id from the response headers with
with_raw_response (the OpenAI Python recipe
has an example), or strip the msg_ prefix from the streamed message id,
which carries the same request id.
Tool use (tools parameter) routes through the gateway transparently. The
gateway forwards the tool-use blocks in both directions without rewriting
the schema.
The gateway forwards anthropic-beta identifiers to Anthropic and Claude
Bedrock, may normalize the comma-separated values or add required beta
identifiers, and omits them for other providers.
Every error body on this surface
carries a request_id field for correlation against the requests view.
The gateway allows up to 600 seconds per provider attempt and 1800 seconds across the fallback loop; streaming limits cover upstream stream readiness rather than the full token stream, so set an explicit client timeout if you need a shorter limit.
When the fallback chain runs out because providers are overloaded or
returning server errors, the gateway returns 503
(rendered as an overloaded_error in the Anthropic error body). That
response does not carry a Retry-After header, so back off on your own
schedule. IQ's own rate-limit and daily-budget 429s carry a
Retry-After hint; monthly-budget and spend-ceiling 429s do not. With max_retries=0,
your queue should read Retry-After when it is present and apply its own
backoff otherwise, including on the 503.