Docs

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:

VariableDescriptionRequired
OPENAI_API_KEYYour OpenAI API keyYes
OPENAI_MODELDefault model (e.g., gpt-4o)No
MAX_TOKENSMaximum tokens per responseNo
ENABLE_STREAMINGEnable response streamingNo

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 regularly

Vercel 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 --prod

Option 2: Git Integration

  1. Push your code to GitHub/GitLab/Bitbucket
  2. Import project in Vercel dashboard
  3. Configure environment variables
  4. 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 --json

Performance 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

IssueSolution
504 TimeoutIncrease maxDuration in vercel.json
Streaming not workingEnsure runtime is set to edge or nodejs
CORS errorsAdd CORS headers to API routes
High latencyConsider Edge Runtime or regional deployment

Next Steps

On this page