Use the Anthropic SDK with AI Gateway
Keep your Anthropic SDK code as it is and use it through AI Gateway by changing only the base URL and API key.
AI Gateway supports requests and responses in the same format as the Anthropic API. Code written with the official Anthropic SDK therefore works through AI Gateway without other changes. You change only two values:
- Base URL:
https://<gateway-host>/anthropicinstead ofhttps://api.anthropic.com. - API key: a Gateway Key instead of an Anthropic API key.
Request and response formats, streaming, and tool use stay the same, so the rest of your code does not change. AI Gateway applies the project’s model permissions, routing policy, and usage limits to every request.
To use the OpenAI SDK instead, see Use the OpenAI SDK.
Before you start
Section titled “Before you start”- Install the Anthropic SDK:
pip install anthropicfor Python, ornpm install @anthropic-ai/sdkfor JavaScript / TypeScript. - Issue a Gateway Key from the Gateway Keys menu of the project you want to use. See Issue a Gateway Key for the steps.
- Check that the model you want appears in the Models menu of the same project. In the row of the model, select the gear icon in the Installation column and check the Gateway base URL and model ID.
Set the Gateway Key as an environment variable where your code runs. The examples on this page read it from AI_GATEWAY_API_KEY.
export AI_GATEWAY_API_KEY='<your Gateway Key>'A Gateway Key is a secret. Do not put the key value directly in source code, source control, or shared screenshots.
Supported APIs
Section titled “Supported APIs”| API | SDK method | Path |
|---|---|---|
| Messages | messages.create | /anthropic/v1/messages |
| Models | models.list | /anthropic/v1/models |
Replace <gateway-host> in the examples with the host of your AI Gateway. The base URL ends with /anthropic, without /v1; the SDK adds /v1/... itself.
Python:
import osimport anthropic
client = anthropic.Anthropic( base_url="https://<gateway-host>/anthropic", api_key=os.environ["AI_GATEWAY_API_KEY"],)
message = client.messages.create( model="<provider-name>/<model-id>", max_tokens=1024, messages=[{"role": "user", "content": "Hello!"}],)print(message.content[0].text)JavaScript / TypeScript:
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ baseURL: "https://<gateway-host>/anthropic", apiKey: process.env.AI_GATEWAY_API_KEY,});
const message = await client.messages.create({ model: "<provider-name>/<model-id>", max_tokens: 1024, messages: [{ role: "user", content: "Hello!" }],});console.log(message.content);Streaming
Section titled “Streaming”with client.messages.stream( model="<provider-name>/<model-id>", max_tokens=1024, messages=[{"role": "user", "content": "Count from 1 to 5."}],) as stream: for text in stream.text_stream: print(text, end="")List models
Section titled “List models”The models.list method returns the models the project can call. Each id is in the form provider-name/model-id. Use it as the model value.
Python:
for model in client.models.list(): print(model.id)JavaScript / TypeScript:
for await (const model of client.models.list()) { console.log(model.id);}display_name is the same as id. AI Gateway does not know the release date of a model, so created_at is always 1970-01-01T00:00:00Z. The response is always a single page (has_more is false). If you pass limit, you get at most that many models and no next page.
Use models from other providers
Section titled “Use models from other providers”The model value selects the provider and the model in the form provider-name/model-id. AI Gateway converts the request between formats, so the Anthropic SDK can call a model of any provider allowed in the project, including OpenAI models:
# Call a model registered under an OpenAI providerclient.messages.create( model="<openai-provider-name>/<gpt-model-id>", max_tokens=1024, messages=[{"role": "user", "content": "Hello!"}],)Use the exact model ID shown in the Models menu. When the project has a routing policy, the policy can choose the provider model, and the administrator may tell you a different model value to use.
Limitations
Section titled “Limitations”- Only the APIs in the Supported APIs table are supported. Other APIs, such as Token Counting, Files, and Message Batches, return an error.
- A field or feature works only when the target provider supports it. For example, a request that uses a feature only Anthropic offers may be rejected when it is routed to a model of another provider.
- Models of a provider that connects ChatGPT accounts (type
openai_codex) cannot be used with the Anthropic SDK. Use the OpenAI SDK Responses API for them.
If a request fails
Section titled “If a request fails”- 401 error: Check that the API key is a Gateway Key and that the environment variable is set where the code runs. If the key is correct and you still get 401, check that the API you call is in the Supported APIs table. A request to an unsupported API also returns 401, even with a valid Gateway Key.
- 404 error or model not found: Check that the base URL ends with
/anthropicand does not include/v1. Also check thatmodelis an exact ID in the project’s Models menu. - Model not allowed: A Gateway Key belongs to the project it was issued in and can call only the models allowed in that project. Check that you use the key of the project where the model is allowed.
- 429 error: The project’s usage limit or the provider’s rate limit was reached. Try again later, or ask the project administrator to check the limits.
- If it still fails, ask the AI Gateway administrator to check the request. Share the request ID from the error response, not the key value.
For the full SDK reference, see the Anthropic SDK documentation.