Next.js · Vercel runtime log

AI SDK stream stops or times out on Vercel (maxDuration)

verified on · Next.js 16.4.5 · React 19.3

Check yours: npm ls next react · On a different version? Paste your package.json into the free Doctor and it checks your versions against these fixes.

what it looks like
Vercel Runtime Timeout Error: Task timed out after 30 seconds
504 Gateway Timeout  FUNCTION_INVOCATION_TIMEOUT

browser: Uncaught (in promise) Error: Connection closed

What it means

Vercel stops a function when it reaches its maximum duration, and for a chat route that time includes every second spent streaming the answer. Nothing crashes in your code. The stream just ends, the UI keeps half an answer, useChat can throw Connection closed, and the log says Task timed out after N seconds. N is the limit that fired. With Fluid compute, which is on by default for projects created since April 23, 2025, the default is 300 seconds on every plan. So if N is 30, look for export const maxDuration = 30 in your route: the AI SDK chat examples include that line, and it lowers the limit. If N is 10 or 15, the project most likely has Fluid compute turned off, where the old defaults of 10 seconds on Hobby and 15 on Pro still apply.

Common causes

  • export const maxDuration = 30 copied from the AI SDK examples, then a reasoning model or a tool calling loop runs longer than 30 seconds
  • An older project with Fluid compute turned off (10 seconds default on Hobby, 15 on Pro)
  • No maxDuration in the route and a low Default Max Duration in Project Settings, Functions, or a functions maxDuration in vercel.json
  • export const runtime = "edge" on the chat route. Edge has to start the response within 25 seconds, so a slow first token fails before anything streams
  • The model or a tool call hangs with no deadline, so the function waits until the limit

How to fix it

  1. Set export const maxDuration = 300 in the route file that streams, like app/api/chat/route.ts. 300 is the most Hobby allows. Pro and Enterprise can go up to 800, or up to 1800 in beta when set per function on a supported Node.js version
  2. Check the seconds in the log line. That number is the limit you hit, and it tells you which setting to change
  3. Calling the model from a Server Action? Put maxDuration in the page.tsx that uses the action. Next.js applies it to the Server Actions on that page
  4. If the log says 10 or 15, turn on Fluid compute in Project Settings, Functions, then redeploy
  5. Remove export const runtime = "edge" from the chat route unless you really need Edge. Node.js functions get the full duration
  6. Redeploy and confirm: next build writes the value to .next/server/functions-config-manifest.json, and vercel inspect <deployment url> --json shows it as timeout
  7. If answers really need longer than your plan allows, cap them with maxOutputTokens or move the work into a background job instead of one request
src/app/api/chat/route.ts
// Vercel counts streaming time too. Hobby max is 300, Pro and Enterprise 800 (1800 in beta).
export const maxDuration = 300;

export async function POST(req: Request) {
  // your streamText(...) call and stream response stay exactly as they are
}

// Server Action instead of a route? Put the same export in the page.tsx that uses it.

works with Next.js 16.4.5 · React 19.3last verified

Prompt for Cursor or Claude Code

My AI SDK chat works locally, but on Vercel long answers stop halfway and the log says "Vercel Runtime Timeout Error: Task timed out after N seconds". Find every route handler, and every page that uses a Server Action, that calls streamText or generateText. List each export const maxDuration and export const runtime you find, plus any functions maxDuration in vercel.json. Then set export const maxDuration = 300 in the chat route file (or in the page.tsx that uses the Server Action) and remove runtime = "edge" from it unless something there needs Edge. Don't change the streaming code. Tell me what each file had before.

Not quite your error? Paste yours into the free VibeBoot Doctor or browse the Next.js error fixer.

Check your whole repo from the terminal: npx github:vibeboot/vibeboot-doctor (free, offline).

Upgrading from Next.js 15? The Next.js 16 upgrade checklist covers every breaking change, the error it causes and the fix.

next: skip the next one

Skip the setup. The VibeBoot SaaS Starter gives you auth, Stripe and a database already wired on Next.js 16, with AGENTS.md and .cursor/rules that keep your agent on the Next.js 16 conventions. $49, one-time.

14-day money-back guarantee · lifetime updates

Building an AI chat app? The AI Chatbot Starter ships streaming chat, saved conversations, Free/Pro usage limits enforced on the server and Stripe, with AGENTS.md and .cursor/rules. Get the AI Chatbot Starter, $59

$ fixed it? get the next one first

New fixes for what AI agents break, by email.

When we add a fix or a Cursor rule for a new Next.js, Auth.js, Prisma or Stripe break, it goes out in the VibeBoot newsletter. Free, occasional, unsubscribe in one click.

Subscribe free