Skip to navigation

Use the Anthropic SDK with Fabric AI Gateway

You can send Anthropic SDK traffic through Fabric AI Gateway by changing the client’s base URL to a gateway route and adding your gateway key as a default header. Your code doesn’t change otherwise. This topic shows you how to configure the Python and TypeScript SDKs and confirm that requests arrive.

Prerequisites

Before you begin, make sure you have the following:

  • The Anthropic SDK for Python (anthropic) or TypeScript (@anthropic-ai/sdk).
  • A route that uses the Anthropic Messages API. For the steps, see Connect an LLM provider.
  • A gateway key for the route. Send it in the fg-api-key header, which every route accepts, unless its Key Auth instance is an older one that sets a headers list. In that case, send the key in one of the listed headers, and ask your admin which. For more information, see Authenticate and send a request.
  • The base URL of the route. In the console, select LLMs > Routes, find the route, and then select Copy LLM base URL.

Configure the client

Set the client’s base URL to the route’s base URL, without /v1. The SDK adds /v1/messages itself. Send the gateway key in the fg-api-key header, and keep it in an environment variable:

export FABRIC_GATEWAY_KEY="GATEWAY_KEY"
import os
from anthropic import Anthropic
client = Anthropic(
base_url="BASE_URL",
api_key="unused",
default_headers={"fg-api-key": os.environ["FABRIC_GATEWAY_KEY"]},
)
message = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=256,
messages=[{"role": "user", "content": "Hello world!"}],
)
print(message.content[0].text)

Replace BASE_URL with the URL that you copied, and GATEWAY_KEY with your gateway key. Use a model that the connection allows.

The SDK requires an API key, but you don’t need a real Anthropic key. When it forwards the request, the gateway adds the Anthropic key saved on the connection in place of the SDK’s x-api-key header, so any placeholder value works. The SDK sends the anthropic-version header for you.

Treat the gateway key as a secret. Don’t commit it to source control.

Confirm that requests pass through the gateway

To confirm that the SDK uses the gateway, do the following:

  1. Run your code. A successful request prints the model’s reply.
  2. In the Fabric console, select Traces. To see traces from every namespace, select Admin, and then select Traces.
  3. Find the request on the route.

If the request returns 401 Unauthorized, check that the fg-api-key header carries a key for this route. For more information, see Troubleshoot authentication errors.

Next steps