Skip to Content

AI Gateway

For more on using AI Gateway with Claude Code, see the Claude Code instructions.

SDKs & APIs

AI Gateway provides Anthropic Messages API endpoints, so you can use the Anthropic SDK and tools like Claude Code through a unified gateway with only a URL change.

Anthropic Messages API

Copy page

https://ai-gateway.vercel.sh

The Anthropic Messages API is available at the following base URL:

Anthropic Messages API with AI Gateway

The Anthropic Messages API implements the same specification as the Anthropic Messages API.

The Anthropic Messages API supports the same authentication methods as the main AI Gateway:

API key: Use your AI Gateway API key with the x-api-key header or Authorization: Bearer header

OIDC token: Use your Vercel OIDC token with the Authorization: Bearer header

You only need to use one of these forms of authentication. If an API key is specified it will take precedence over any OIDC token, even if the API key is invalid.

Supported endpoints

The AI Gateway supports the following Anthropic Messages API endpoints:

For advanced features, see:

Configuring Claude Code

Claude Code is Anthropic's agentic coding tool. You can configure it to use Vercel AI Gateway, enabling you to:

  • Route requests through multiple AI providers
  • Monitor traffic and spend in your AI Gateway Overview
  • View detailed traces in Vercel Observability under AI
  • Use any model available through the gateway
  1. Configure environment variablesConfigure Claude Code to use the AI Gateway by setting these environment variables: Variable Value ANTHROPIC_BASE_URL https://ai-gateway.vercel.sh Your AI Gateway API key ANTHROPIC_AUTH_TOKEN "" (empty string) ANTHROPIC_API_KEY

    Option 1: Shell alias (simplest)Add this alias to your ~/.zshrc (or ~/.bashrc):

    aliasclaude-vercel='ANTHROPIC_BASE_URL="https://ai-gateway.vercel.sh" ANTHROPIC_AUTH_TOKEN="your-api-key-here" ANTHROPIC_API_KEY="" claude'

    Then reload your shell:

    source~/.zshrc

    Option 2: Wrapper scriptFor more flexibility (e.g., adding additional logic), create a wrapper script at ~/bin/claude-vercel:

    #!/usr/bin/env bash # Routes Claude Code through Vercel AI Gateway ANTHROPIC_BASE_URL="https://ai-gateway.vercel.sh" \ ANTHROPIC_AUTH_TOKEN="your-api-key-here" \ ANTHROPIC_API_KEY="" \ claude "$@"Make it executable and ensure ~/bin is in your PATH:

    mkdir -p~/bin

    chmod +x~/bin/claude-vercel

    echo'export PATH="$HOME/bin:$PATH"'>>~/.zshrc

    source~/.zshrc

  2. Run Claude CodeRun claude-vercel to start Claude Code with AI Gateway: claude-vercelYour requests will now be routed through Vercel AI Gateway.

Integration with Anthropic SDK

You can use the AI Gateway's Anthropic Messages API with the official Anthropic SDK. Point your client to the AI Gateway's base URL and use your AI Gateway API key or OIDC token for authentication.

Anthropic Messages API documentation.


TypeScript

Python


apiKey:process.env.AI_GATEWAY_API_KEY,

cURL

baseURL:'https://ai-gateway.vercel.sh',

constanthropic=newAnthropic({

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

});

max_tokens:1024,

});

messages: [{ role:'user', content:'Hello, world!' }],

model:'anthropic/claude-opus-5',

constmessage=awaitanthropic.messages.create({

Message batches

Use the Anthropic SDK's client.messages.batches methods to submit requests for asynchronous processing, poll for completion, and read results.

Submit a batch and read results

Install @anthropic-ai/sdk for TypeScript or JavaScript, or anthropic for Python. Set AI_GATEWAY_API_KEY to your AI Gateway API key. Use https://ai-gateway.vercel.sh as the SDK base URL, without /v1.

Set AI_GATEWAY_BATCH_IDEMPOTENCY_KEY to a value you persist for this batch, such as sentiment-eval-2026-09-17. Reuse that value and the identical payload when retrying submission. AI Gateway returns the existing batch for a matching retry. A new key creates a new batch; reusing a key with a different payload returns an error.

The example prints the batch ID, checks status up to 20 times at 30-second intervals, and reads results when processing ends:

});

import { setTimeout } from'node:timers/promises';

custom_id:'review-1',

if (!key) thrownewError('Set AI_GATEWAY_BATCH_IDEMPOTENCY_KEY');

requests: [{
max_tokens:64,
}],

if (!batch.results_url) {

batch =awaitclient.messages.batches.retrieve(batch.id);

},
},
params: {
}

awaitsetTimeout(30_000);

constresults=awaitclient.messages.batches.results(batch.id);

);
}
} else {
}

Save the printed batch ID to resume polling with client.messages.batches.retrieve later. The batch keeps processing after the example stops waiting.

When processing ends, a non-null results_url indicates that results are available. The SDK uses this URL when you call results. Individual requests can still fail. Match each result to its request by custom_id, regardless of result order. Successful results include result.message; other results report errored, canceled, or expired.

List batches

Use await client.messages.batches.list({ limit: 20 }) in TypeScript or JavaScript, or client.messages.batches.list(limit=20) in Python. The default page size is 20, with a maximum of 100. Pass either after_id or before_id to paginate, never both.

Batch requirements

  • Submit up to 1,000 requests per batch, all using the same model.
  • Give each request a unique custom_id of 1–64 characters using letters, numbers, underscores, or hyphens ([A-Za-z0-9_-]).
  • Batch requests don't support streaming, provider-executed tools, or Model Context Protocol (MCP) servers.
  • Batch cancellation and deletion aren't supported through AI Gateway.
  • You can use Bring Your Own Key (BYOK) credentials saved to your team. Request-scoped raw provider keys aren't supported.
  • Zero Data Retention (ZDR) isn't available for batches.

You can reach our customer support team by emailing info@yourcompany.example.com, calling +1 555-555-5556, or using the live chat on our website. Our dedicated team is available 24/7 to assist with any inquiries or issues.

We’re committed to providing prompt and effective solutions to ensure your satisfaction.

We offer a 30-day return policy for all products. Items must be in their original condition, unused, and include the receipt or proof of purchase. Refunds are processed within 5-7 business days of receiving the returned item.

  • stream (boolean): Whether to stream the response. Defaults to false
  • temperature (number): Controls randomness in the output. Range: 0-1
  • top_p (number): Nucleus sampling parameter. Range: 0-1
  • top_k (integer): Top-k sampling parameter
  • stop_sequences (array): Stop sequences for the generation
  • tools (array): Array of tool definitions for function calling
  • tool_choice (object): Controls which tools are called
  • thinking (object): Extended thinking configuration
  • system (string or array): System prompt

Prompt caching

The gateway passes through the cache_control parameter to Anthropic's prompt caching feature. This is explicit caching: you specify cache breakpoints, and Anthropic handles storing and reusing cached content automatically.

Example request


// most Claude models. A short string is silently not cached.

import fs from'node:fs';

text:'You are a helpful assistant that analyzes documents.',

type:'text',
apiKey,
max_tokens:1024,
});

// input_tokens: 50,

content:'Summarize the key points from this document.',

type:'text',
messages: [
system: [
role:'user',

text: longDocumentContent,

// cache_creation_input_tokens: 10000, // Tokens written to cache

// {
},
// }
},

Where to place cache breakpoints

Add cache_control: { type: 'ephemeral' } to mark content that should be cached. You can place cache breakpoints on system messages, user message content, tool definitions, tool results, and assistant message content. Anthropic also supports automatic caching, where a single top-level cache_control field automatically applies to the last cacheable block.

For the full list of cacheable locations and automatic caching details, see the Anthropic prompt caching docs.

Cache behavior

  • First request: Content up to the breakpoint is cached ( cache_creation_input_tokens)
  • Subsequent requests: Matching prefixes are read from cache ( cache_read_input_tokens)
  • TTL: Cached content expires after 5 minutes, refreshed on each cache hit

Configuring the Claude Agent SDK

The Claude Agent SDK ( @anthropic-ai/claude-agent-sdk) lets you build agents with the same tools and agentic loop that power Claude Code. Because the SDK spawns Claude Code as a subprocess, it inherits the same ANTHROPIC_* environment variables described above, so your agent code needs no gateway-specific configuration:

import { query } from'@anthropic-ai/claude-agent-sdk';

forawait (constmessageofquery({

prompt:'Find and fix the bug in auth.ts',

options: { allowedTools: ['Read','Edit','Bash'] },

})) {

console.log(message);

}


Refer to the Claude Agent SDK documentation for more details.

Passing AI Gateway options

The Agent SDK respects any environment variable the Claude Code CLI reads, including these two for working with AI Gateway:

Variable

CLAUDE_CODE_EXTRA_BODY

CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS

For example, to restrict requests to Amazon Bedrock only, set these alongside the ANTHROPIC_* variables in your environment:

Purpose

Strips Anthropic-specific anthropic-beta headers and beta tool-schema fields from requests. Set to 1 when routing through providers like Bedrock or Vertex AI that reject those fields.

CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1

Error handling

How is your data secured?

The API returns standard HTTP status codes and error responses:

Merges a JSON object into the top level of every request body. Use it to pass providerOptions like order, only, and sort.

CLAUDE_CODE_EXTRA_BODY='{"providerOptions":{"gateway":{"only":["bedrock"]}}}'

Common error codes

  • 400 Bad Request: Invalid request parameters
  • 401 Unauthorized: Invalid or missing authentication
  • 403 Forbidden: Insufficient permissions
  • 404 Not Found: Model or endpoint not found
  • 429 Too Many Requests: Rate limit exceeded
  • 500 Internal Server Error: Server error

Error response format

{

"type":"error",

"error": {

"type":"invalid_request_error",

"message":"Invalid request: missing required parameter 'max_tokens'"

}

}

Last updated September 18, 2026

Cross-link map: Anthropic Messages API with AI Gateway (/docs/ai-gateway/sdks-and-apis/anthropic-messages-api)From the Vercel docs graph (built 2026-09-21T05:26:59.511Z), spanning vercel.com docs + KB, nextjs.org, ai-sdk.dev, and other Vercel documentation sites. Full graph as JSON: https://vercel.com/docs/graph.jsonSemantically closest pagesClaude Code and Claude Agent SDK with AI Gateway — Connect Claude Code to AI Gateway with one CLI command, or configure it manually.Anthropic Messages Configuration with AI Gateway — Advanced Anthropic API features including web search, provider timeouts, and automatic caching through AI Gateway.Anthropic Messages Streaming with AI Gateway — Stream Anthropic Messages API responses token by token as they are generated through AI Gateway.AI Gateway SDKs and APIs — Connect to AI Gateway with the AI SDK, Python, REST, or compatible OpenAI, Anthropic Messages, OpenResponses, and CoherePython with AI Gateway: OpenAI and Anthropic SDKs — Use AI Gateway with Python through OpenAI or Anthropic SDKs with full streaming, tool calling, and async support.This page links to (10)AI Gateway Authentication and BYOK — Authenticate AI Gateway requests with API keys or OIDC tokens, and configure bring your own key \(BYOK\) credentials forClaude Code and Claude Agent SDK with AI Gateway — Connect Claude Code to AI Gateway with one CLI command, or configure it manually.AI Gateway Provider Routing and Fallbacks — Configure provider routing, ordering, and fallback behavior in Vercel AI Gateway.Anthropic Messages Configuration with AI Gateway — Advanced Anthropic API features including web search, provider timeouts, and automatic caching through AI Gateway.Anthropic Messages Images and PDFs with AI Gateway — Send images and PDF documents as part of your Anthropic API message requests through AI Gateway.Anthropic Messages Requests with AI Gateway — Create messages using the Anthropic Messages API format with support for streaming through AI Gateway.Anthropic Messages Extended Thinking with AI Gateway — Configure how much Claude thinks before answering, using the Anthropic Messages API thinking parameter through AI GatewaAnthropic Messages Streaming with AI Gateway — Stream Anthropic Messages API responses token by token as they are generated through AI Gateway.Anthropic Messages Structured Outputs with AI Gateway — Get JSON responses conforming to a JSON Schema from Anthropic models through AI Gateway.Anthropic Messages Tool Calling with AI Gateway — Use function calling with the Anthropic Messages API to allow models to call tools and functions through AI Gateway.Pages that link here (13)By site: vercel-changelog (3) · vercel-docs (10)From vercel-changelogFast mode for Opus 4.7 available on AI GatewayOpus 4.6 Fast Mode available on AI GatewayService tiers now available on AI GatewayFrom vercel-docsVercel AI Gateway: Models, Routing, and Observability — Call AI models from any infrastructure through a managed gateway. Centralize credentials, request logs, spend budgets, rClaude Code and Claude Agent SDK with AI Gateway — Connect Claude Code to AI Gateway with one CLI command, or configure it manually.Conductor with AI Gateway — Connect Conductor to AI Gateway through its Claude Code configuration. Route parallel coding agents through the AnthropiHarbor with AI Gateway — Evaluate coding-agent harnesses with Harbor and AI Gateway. Choose a harness, configure its connection, and pass separatAI Gateway Text Generation Quickstart — Generate and stream text responses using AI Gateway.AI Gateway Reasoning — Discover model reasoning capabilities and configure effort across AI SDK, Chat Completions, Messages, and Responses withAI Gateway SDKs and APIs — Connect to AI Gateway with the AI SDK, Python, REST, or compatible OpenAI, Anthropic Messages, OpenResponses, and CohereAI SDK with AI Gateway — Build AI-powered TypeScript applications using the AI SDK with AI Gateway for unified access to 200+ models.Python with AI Gateway: OpenAI and Anthropic SDKs — Use AI Gateway with Python through OpenAI or Anthropic SDKs with full streaming, tool calling, and async support.AI Gateway REST API Reference — Reference for AI Gateway REST endpoints: models, usage, generations, and reporting.