Next.js OpenAI Streaming Response Aborted on Vercel — Edge Function Timeout Fix
The Exact Error
Error: AbortError: The operation was aborted.
at async POST (app/api/chat/route.ts:24)
Vercel Edge Function exceeded maximum execution duration of 25s.
Function: api/chat
Duration: 25001ms (limit: 25000ms)
Status: 504What's Happening
OpenAI streaming chat responses work locally but silently abort after exactly 25 seconds in production on Vercel.
Root Cause
Vercel Edge Functions have a hard 25-second execution limit on the Hobby plan (25s) and Pro plan (30s). Long AI responses that take more than this to fully stream will be aborted. The Edge runtime does not support streaming beyond this limit without specific configuration.
Quick Diagnosis Checklist
Run through these steps to confirm this is your issue:
Confirm you're using Edge Runtime: `export const runtime = 'edge'`
Measure your average AI response time — if > 20s, you'll hit this on complex queries
Check Vercel function logs for 504 errors — they'll show the exact timeout
Verify you're not accidentally using Node.js runtime for streaming routes
The Permanent Fix
Switch to `runtime = 'nodejs'` with the correct maxDuration config, or implement response streaming with chunked transfer encoding and a client-side reconnect mechanism for long responses.
Drop-In Package That Permanently Fixes This
Instead of building the fix from scratch, use this production-ready package.
// audit-logger.middleware.ts
import { withAuditLog } from './audit-logger.middleware'
export const POST = withAuditLog(
async (req: Request) => {
// Your existing AI API route — unchanged
const response = await openai.chat.completions.create({ ... })
return Response.json(response)
},
{
storage: 'supabase', // or 'r2', 's3'
tableName: 'ai_audit_logs',
hashPrompts: true, // SHA-256 hash, never stores raw prompts
includeUserContext: true,
complianceMode: 'eu-ai-act' // or 'colorado', 'both'
}
)