This application supports Vercel AI Gateway for AI model access.
The required packages are already installed:
ai- Vercel AI SDK@ai-sdk/openai- OpenAI provider for AI SDK
Add to your .env.local (for local development) and Vercel environment variables:
# AI Gateway API Key (required for AI Gateway endpoint)
AI_GATEWAY_API_KEY=your_gateway_api_key_here
# Optional: Custom AI Gateway base URL
# Default: https://gateway.ai.cloudflare.com/v1
AI_GATEWAY_BASE_URL=https://your-gateway-url.com/v1The application has the following AI endpoint:
- Uses Vercel AI Gateway
- Requires:
AI_GATEWAY_API_KEY - Uses Vercel AI SDK for streaming
- Supports OpenAI-compatible models
The frontend uses the AI Gateway endpoint. The fetch call in app/page.tsx should use:
const res = await fetch('/api/query-gateway', {AI Gateway endpoint supports these models (mapped from internal IDs):
ooverta→google/gemini-2.0-flash-exp:freegemini-flash→google/gemini-2.0-flash-exp:freegpt-4o→gpt-4oclaude-3-5-sonnet→claude-3-5-sonnetclaude-3-opus→claude-3-opusclaude-3-haiku→claude-3-haiku
- Unified Interface: Single API for multiple providers
- Rate Limiting: Built-in rate limiting and caching
- Cost Optimization: Automatic failover and cost tracking
- Better Error Handling: Improved error recovery
- Streaming Support: Native streaming with Vercel AI SDK
AI_GATEWAY_API_KEY=your_cloudflare_gateway_key
AI_GATEWAY_BASE_URL=https://gateway.ai.cloudflare.com/v1AI_GATEWAY_API_KEY=your_custom_gateway_key
AI_GATEWAY_BASE_URL=https://your-custom-gateway.com/v1Test the AI Gateway endpoint:
curl -X POST http://localhost:3000/api/query-gateway \
-H "Content-Type: application/json" \
-d '{
"query": "Hello, how are you?",
"model": "ooverta"
}'To set up AI Gateway:
- Set
AI_GATEWAY_API_KEYin environment variables - Ensure frontend uses
/api/query-gatewayendpoint - Test with your models
- Monitor performance and costs
Error: AI Gateway API key missing
- Make sure
AI_GATEWAY_API_KEYis set in environment variables - Restart your development server after adding to
.env.local
Error: Invalid model specified
- Check that the model ID is in the
MODEL_MAPallowlist - Add more models to
MODEL_MAPif needed
Error: Gateway connection failed
- Verify
AI_GATEWAY_BASE_URLis correct - Check network connectivity
- Ensure gateway API key is valid