Getting Started with AI Gateway
Use AI Gateway from any environment to call models, connect coding agents, and inspect routing and spend. AI Gateway API keys belong to a Vercel team and work in local development, CI, external servers, and Vercel deployments.
| Goal | Start here |
|---|---|
| Ask a coding agent to make a request | Copy the agent setup prompt |
| Route a coding agent through AI Gateway | Run the coding-agent setup |
| Make your first request | Use cURL, TypeScript, or Python |
| Use AI Gateway with existing code | Use an SDK or API, or migrate to AI Gateway |
Use this path when you want a coding agent to write and run an AI Gateway request in your current environment.
Paste this prompt into a coding agent with terminal access:
Set up AI Gateway and make one text-generation request from this environment. Use the AI Gateway skill for this task. If it is unavailable, run npx skills add vercel/vercel-plugin --skill ai-gateway, then find and read its SKILL.md before continuing. Reuse the environment's language, package manager, and AI client when possible; otherwise use cURL. Reuse AI_GATEWAY_API_KEY without exposing it. If the key is missing, use npx vercel@latest to create a team-scoped key without installing the Vercel CLI globally. Ask for the team slug only if the current CLI scope is ambiguous, and capture the new key without printing it. Use openai/gpt-6-astra. Run the request and report only the generated text and any action I need to take.
npx plugins add vercel/vercel-pluginUse this path when you want to route a coding agent's own model requests through AI Gateway. The Vercel CLI detects supported agents, including Claude Code, and lets you select which ones to connect:
npx vercel@latest ai-gateway setupThe command provisions or reuses an API key, previews its configuration changes, and connects the agents you select. For Claude Code, it uses the dedicated AI Gateway endpoint and adds Gateway models to the /model picker. See Coding Agents for supported agents and manual setup.
You need a Vercel account. To use free AI Gateway Credits, add a valid payment method to your team. The cURL path needs no runtime. The TypeScript path needs Node.js 22.18 or later, and the Python path needs Python 3.12 or later with uv.
Create and export an API key:
- Open the Create API Key dialog, enter a name, and select Create API Key. The link opens the dialog directly.
- Copy the key immediately and export it in the terminal where you will run the example. You cannot retrieve its value again.
export AI_GATEWAY_API_KEY="your_ai_gateway_api_key"These examples use openai/gpt-6-astra and consume AI Gateway Credits. You can replace the model slug with any model your team can access. If your team has not purchased credits, choose a free-tier model.
Send a Chat Completions request directly to AI Gateway:
curl https://ai-gateway.vercel.sh/v1/chat/completions \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-6-astra",
"messages": [
{
"role": "user",
"content": "Invent a new holiday and describe its traditions."
}
]
}'Install the AI SDK:
pnpm add ai@latestCreate index.mts:
import { generateText } from 'ai';
const { text } = await generateText({
model: 'openai/gpt-6-astra',
prompt: 'Invent a new holiday and describe its traditions.',
});
console.log(text);Run the script:
node index.mtsCreate quickstart.py:
import asyncio
import ai
async def main() -> None:
model = ai.get_model('openai/gpt-6-astra')
messages = [
ai.user_message('Invent a new holiday and describe its traditions.')
]
async with ai.stream(model, messages) as stream:
async for event in stream:
if isinstance(event, ai.events.TextDelta):
print(event.chunk, end='', flush=True)
print()
if __name__ == '__main__':
asyncio.run(main())Run the script with the AI SDK for Python:
uv run --with ai quickstart.pyAfter the request completes, open AI Gateway Logs and select the newest request. You can inspect its status, model, provider, token usage, cost, duration, and routing attempts.
Logs may take up to 90 seconds to appear. Refresh the page if you don't see the request.
Use a supported SDK or API to connect an existing client. To move calls from a provider endpoint without changing their behavior, migrate to AI Gateway.
- Troubleshoot request errors by status code, type, and message
- Generate text, images, video, realtime audio, or speech
- Answer typed questions about application state in the Decision quickstart
- Browse models by capability, provider, price, and free-tier availability, or query
GET /v1/models - Configure provider routing and model fallbacks
- Build an agent application
Was this helpful?