The Error

Your Vercel deployment returns:

504: GATEWAY_TIMEOUT
Code: FUNCTION_INVOCATION_TIMEOUT

A Vercel Function (an API route, Route Handler, Server Action or server-rendered page) ran longer than its allowed maximum duration, so Vercel stopped it.

The fix depends on why it's slow: sometimes you just need a higher limit; often the work shouldn't happen inside a request at all.


🎯 Quick Fix

  1. Check your limit: with Fluid compute, the default is 300 seconds on all plans; Hobby can't go higher, Pro and Enterprise can configure up to 800 seconds.
  2. Raise it for a specific route (Pro/Enterprise):
// app/api/report/route.ts
export const maxDuration = 600; // seconds
  1. If the work takes minutes, move it to a background job and return immediately.

Current Limits (Fluid Compute)

Plan Default Maximum
Hobby 300 s 300 s
Pro 300 s 800 s
Enterprise 300 s 800 s

Vercel has also offered longer extended durations for some runtimes in beta. Limits change, so confirm on Vercel's current docs.


Why Functions Time Out

flowchart TD T{"What's slow?"} -->|"Slow external API /
AI model call"| A["Stream the response,
set timeouts on calls"] T -->|"Big database query"| B["Add indexes, paginate,
check connection pooling"] T -->|"Long job (reports,
video, bulk emails)"| C["Queue it: return 202,
process in background"] T -->|"Waiting on a dead
connection"| D["Set client timeouts,
fix pooling"]

Fix 1: Stream Long AI Responses

LLM calls can take a long time to finish but start producing tokens quickly. Stream them so the user sees progress and the response starts immediately:

// app/api/chat/route.ts
import Anthropic from "@anthropic-ai/sdk";

export const maxDuration = 300;
const client = new Anthropic();

export async function POST(req: Request) {
  const { prompt } = await req.json();
  const stream = client.messages.stream({
    model: "claude-sonnet-5-5",
    max_tokens: 2048,
    messages: [{ role: "user", content: prompt }],
  });
  return new Response(stream.toReadableStream()); // newline-delimited JSON events
}

Fix 2: Do Work After the Response

For follow-up work that the user doesn't need to wait for (logging, sending an email, updating analytics), use after() in Next.js or waitUntil() from @vercel/functions:

import { after } from "next/server";

export async function POST(req: Request) {
  const data = await req.json();
  const order = await saveOrder(data);
  after(async () => {
    await sendConfirmationEmail(order); // runs after the response is sent
  });
  return Response.json({ id: order.id });
}

The function still has to finish within its maximum duration, but the user gets their response immediately.


Fix 3: Move Long Jobs to a Queue

sequenceDiagram participant U as User participant F as Vercel Function participant Q as Queue / workflow participant W as Worker U->>F: POST /api/reports F->>Q: Enqueue job #81 F-->>U: 202 Accepted (job #81) Q->>W: Run job #81 (minutes) W->>W: Generate report, store result U->>F: GET /api/reports/81 (poll or webhook) F-->>U: ✅ Ready, download link

For jobs that take minutes or need retries, use a queue or a durable workflow tool (Vercel Workflow, Inngest, Trigger.dev, QStash, or your own queue), and let the user poll or get notified. (See Message Queue Explained.)


Fix 4: Speed Up the Function Itself

  • Database: add indexes, avoid N+1 queries, use a pooled connection (Fix Postgres Too Many Connections)
  • Region: put your function in the same region as your database
  • Parallelise independent calls with Promise.all
  • Set timeouts on outgoing fetch calls so one slow API can't hang the function
  • Cache results that don't change every request

Checklist

  • Confirm your plan's maximum duration and whether Fluid compute is enabled
  • Set maxDuration per route where needed (within plan limits)
  • Stream long AI responses
  • Use after() / waitUntil() for post-response work
  • Move multi-minute jobs to queues or workflows
  • Check logs in the Vercel dashboard for which step is slow

Frequently Asked Questions

What is the Vercel function timeout limit?

With Fluid compute, Vercel's documented default maximum duration is 300 seconds on all plans. Hobby is capped at 300 seconds, while Pro and Enterprise can configure up to 800 seconds per function. Check Vercel's docs for the latest limits.

How do I increase maxDuration in Next.js on Vercel?

Export a maxDuration constant from the route or page file, for example export const maxDuration = 600, within your plan's maximum. You can also set function durations in vercel.json for other frameworks.

How do I run long background jobs on Vercel?

Return a response quickly and hand the work to a queue or durable workflow service that runs it in steps with retries, then notify the user or let them poll for the result. For short follow-up tasks, Next.js after() or waitUntil() can run work after the response is sent.


Conclusion

FUNCTION_INVOCATION_TIMEOUT means your function exceeded its maximum duration. Raise maxDuration within your plan's limits if the work genuinely needs it, stream slow AI responses, run follow-up work after responding, and move multi-minute jobs to queues or workflows.


Sources

Last verified: 2 November 2026.


Related reads: How to Deploy Next.js on Vercel · Message Queue Explained · Fix Postgres Too Many Connections · Serverless vs Containers

I write practical guides for developers. Follow on LinkedIn or X, or invite me to speak.