Integrate Anthropic’s Claude models with Portkey’s AI Gateway
Portkey provides a robust and secure gateway to integrate various Large Language Models (LLMs) into applications, including Anthropic’s Claude APIs.With Portkey, take advantage of features like fast AI gateway access, observability, prompt management, and more, while securely managing API keys through Model Catalog.
All Models
Full support for all Claude models including Sonnet and Haiku 4-5
All Endpoints
/messages, count-tokens and more fully supported
Multi-Provider Support
Use Claude from Anthropic, Bedrock, and Vertex with native SDK support
response = portkey.chat.completions.create( model="@anthropic/claude-sonnet-4-5-20250929", messages=[{"role": "user", "content": "Tell me a story"}], max_tokens=500, stream=True)for chunk in response: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="")
const response = await portkey.chat.completions.create({ model: "@anthropic/claude-sonnet-4-5-20250929", messages: [{ role: "user", content: "Tell me a story" }], max_tokens: 500, stream: true})for await (const chunk of response) { if (chunk.choices[0].delta.content) { process.stdout.write(chunk.choices[0].delta.content) }}
By default, the gateway treats this as a successful (status 200) response and streams the error directly to the client, which means retry, fallback, and circuit breaker strategies do not activate (they rely on HTTP status codes).When Catch Overloaded Error on Stream is enabled on an Anthropic integration, the gateway intercepts these errors before they reach the client and converts them into HTTP 529 responses, allowing your retry and fallback strategies to trigger automatically.
This feature is only available for the Anthropic provider. Other providers (e.g., Bedrock) handle overload errors at the HTTP level, where existing retry/fallback already applies. It also only applies to streaming requests — non-streaming Anthropic requests already return HTTP 529 directly.
Reads the first chunk of the Anthropic streaming response before committing it to the client
Skips any keepalive ping events
If the first meaningful event is an overloaded_error, returns an HTTP 529 response instead of the stream
If the first event is normal content, continues streaming as usual with no data loss
If no retry strategy is present and an overloaded_error is found, the request fails as a normal request with error 529.The 529 response integrates with the gateway’s existing error handling and supports all existing config combinations:
Retry: Triggers automatically when retry is configured
Fallback: Moves to the next target in a fallback strategy
Circuit breaker: Counts as a failure for circuit breaker thresholds
Performance: There is zero overhead when the setting is disabled. When enabled, only the first event is inspected before the stream is committed.
With a fallback config using two Anthropic integrations (both with Catch Overloaded Error on Stream enabled), if the primary returns an overloaded error during streaming, the gateway automatically retries with the backup:
Portkey supports Anthropic’s vision models including claude-sonnet-4-5-20250929, claude-3-5-sonnet, claude-3-haiku, claude-3-opus, and claude-3.7-sonnet. Use the same format as OpenAI:
Anthropic only accepts base64-encoded images and does not support image URLs. Use the same base64 format to send images to both Anthropic and OpenAI models.
import base64import httpxfrom portkey_ai import Portkeyportkey = Portkey(api_key="PORTKEY_API_KEY")# Fetch and encode the imageimage_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")response = portkey.chat.completions.create( model="@anthropic/claude-sonnet-4-5-20250929", messages=[{ "role": "user", "content": [ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}, {"type": "text", "text": "What's in this image?"} ] }], max_tokens=300)print(response.choices[0].message.content)
import Portkey from 'portkey-ai'import axios from 'axios'const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY"})// Fetch and encode the imageconst imageUrl = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"const imageResponse = await axios.get(imageUrl, { responseType: 'arraybuffer' })const imageBase64 = Buffer.from(imageResponse.data).toString('base64')const response = await portkey.chat.completions.create({ model: "@anthropic/claude-sonnet-4-5-20250929", messages: [{ role: "user", content: [ { type: "image_url", image_url: { url: `data:image/jpeg;base64,${imageBase64}` } }, { type: "text", text: "What's in this image?" } ] }], max_tokens: 300})console.log(response.choices[0].message.content)
To prompt with PDFs, update the url field to: data:application/pdf;base64,BASE64_PDF_DATA
Models like claude-3-7-sonnet-latest support extended thinking. Get the model’s reasoning as it processes the request.
The assistant’s thinking response is returned in the response_chunk.choices[0].delta.content_blocks array, not the response.choices[0].message.content string.
Set strict_open_ai_compliance=False to use this feature:
from portkey_ai import Portkeyportkey = Portkey( api_key="PORTKEY_API_KEY", strict_open_ai_compliance=False)response = portkey.chat.completions.create( model="@anthropic/claude-3-7-sonnet-latest", max_tokens=3000, thinking={"type": "enabled", "budget_tokens": 2030}, stream=False, messages=[{ "role": "user", "content": [{ "type": "text", "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" }] }])print(response)
import Portkey from 'portkey-ai'const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", strictOpenAiCompliance: false})const response = await portkey.chat.completions.create({ model: "@anthropic/claude-3-7-sonnet-latest", max_tokens: 3000, thinking: { type: "enabled", budget_tokens: 2030 }, stream: false, messages: [{ role: "user", content: [{ type: "text", text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" }] }]})console.log(response)
curl --location 'https://api.portkey.ai/v1/messages' \--header 'x-portkey-provider: anthropic' \--header 'Content-Type: application/json' \--header 'x-portkey-api-key: YOUR_PORTKEY_API_KEY' \--data-raw '{ "model": "@your-provider-slug/claude-sonnet-4-5-20250929", "max_tokens": 1024, "stream": true, "messages": [ { "role": "user", "content": "What is the weather like in Chennai?" } ]}'
You can use all Portkey features (like caching, observability, configs) with this route. Just add the x-portkey-config, x-portkey-provider, x-portkey-... headers.
Portkey supports Anthropic’s Files API (beta), enabling you to upload, list, retrieve, and delete files through the gateway. Uploaded files can be referenced in chat completions using file_id instead of re-uploading content each request.
Files API
Upload, list, retrieve, and delete files — then use them in chat completions
Manage all prompt templates to Anthropic in the Prompt Library. All current Anthropic models are supported, and you can easily test different prompts.Use the portkey.prompts.completions.create interface to use the prompt in an application.