Vercel Deployment
Deploy your ChatGPT SDK integration to Vercel with proper environment configuration
Vercel Deployment Guide
Deploy your ChatGPT SDK application to Vercel with optimized configuration for AI streaming and MCP endpoints.
Pre-Deployment Checklist
- OpenAI API key generated
- All environment variables documented
- Streaming endpoints tested locally
- Build passes without errors:
bun run build
Environment Variables
Configure these in your Vercel project settings:
| Variable | Description | Required |
|---|---|---|
OPENAI_API_KEY | Your OpenAI API key | Yes |
OPENAI_MODEL | Default model (e.g., gpt-4o) | No |
MAX_TOKENS | Maximum tokens per response | No |
ENABLE_STREAMING | Enable response streaming | No |
Production API Key Best Practices
# Use a restricted API key with limited permissions
# Only allow access to required models
# Set IP restrictions if possible
# Rotate keys regularlyVercel Configuration
vercel.json
Create or update vercel.json for optimal AI streaming:
{
"functions": {
"app/api/chat/route.ts": {
"maxDuration": 60
}
},
"headers": [
{
"source": "/api/chat",
"headers": [
{
"key": "Cache-Control",
"value": "no-store, max-age=0"
}
]
}
]
}Edge Runtime (Optional)
For lower latency, use Edge Runtime:
// app/api/chat/route.ts
export const runtime = 'edge';
export const maxDuration = 60;
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
export async function POST(req: Request) {
// Edge-compatible implementation
}Deployment Steps
Option 1: Vercel CLI
# Install Vercel CLI
bun add -g vercel
# Login
vercel login
# Deploy
vercel --prodOption 2: Git Integration
- Push your code to GitHub/GitLab/Bitbucket
- Import project in Vercel dashboard
- Configure environment variables
- Deploy
Post-Deployment Verification
Test the Chat Endpoint
curl -X POST https://your-app.vercel.app/api/chat \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "Hello"}]
}'Monitor Logs
Check Vercel Functions logs for:
- API response times
- Error rates
- Token usage patterns
vercel logs --jsonPerformance Optimization
Caching Strategies
// Cache tool results when appropriate
const cache = new Map();
const result = streamText({
model: openai('gpt-4o'),
messages,
tools: {
getUserData: {
description: 'Get user data',
parameters: z.object({ userId: z.string() }),
execute: async ({ userId }) => {
if (cache.has(userId)) {
return cache.get(userId);
}
const data = await fetchUserData(userId);
cache.set(userId, data);
return data;
},
},
},
});Rate Limiting
import { Ratelimit } from '@upstash/ratelimit';
import { Redis } from '@upstash/redis';
const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(10, '1 m'),
});
export async function POST(req: Request) {
const ip = req.headers.get('x-forwarded-for');
const { success } = await ratelimit.limit(ip ?? 'anonymous');
if (!success) {
return new Response('Rate limit exceeded', { status: 429 });
}
// Continue with chat logic
}Troubleshooting Deployment Issues
| Issue | Solution |
|---|---|
| 504 Timeout | Increase maxDuration in vercel.json |
| Streaming not working | Ensure runtime is set to edge or nodejs |
| CORS errors | Add CORS headers to API routes |
| High latency | Consider Edge Runtime or regional deployment |
Next Steps
- Troubleshooting Guide
- Monitor your deployment analytics
- Set up alerting for errors