Prerequisite: You need an AI Gateway endpoint before continuing. Create one using the dashboard quickstart or follow the manual setup guide.
Installation
Basic usage
Point the SDK at your AI Gateway endpoint. Which API key do I use?- AI Gateway API Keys (recommended): Use your AI Gateway API Key (format:
ng-xxxxx-g1-xxxxx). ngrok handles provider keys for you. - BYOK (passthrough mode): Use your provider API key (for example,
sk-...from OpenAI). See Bring Your Own Keys.
Try using this prompt on your agent to verify your gateway connection: 'State your exact model name and provider.'
Streaming
The AI Gateway supports streaming responses:Try using this prompt on your agent to test streaming — tokens should appear one by one: 'Write a haiku about APIs'
Using different providers
Route to different providers using model prefixes:Automatic model selection
Let the gateway choose the best model:Embeddings
Generate embeddings through the gateway:Function calling
Tool/function calling works exactly as documented by OpenAI:Try using this prompt on your agent to test tool calling — your get_weather function should be invoked: 'What is the current weather in Paris?'
Async usage
Use async clients for better performance:Error handling
The gateway handles many errors automatically through failover. For errors that reach your app:Supported endpoints
The AI Gateway supports these OpenAI API endpoints:| Endpoint | Description |
|---|---|
/v1/chat/completions | Chat completions |
/v1/completions | Legacy completions |
/v1/embeddings | Text embeddings |
/v1/responses | Responses |
Next steps
- Model Selection Strategies - Configure routing logic
- Configuring Providers - Set up providers and keys
- Multi-Provider Failover - Failover examples