Fix guides · v0, Cursor, Claude Code, Bolt, Replit ·
Fix Vercel 504 FUNCTION_INVOCATION_TIMEOUT
504: GATEWAY_TIMEOUT Code: FUNCTION_INVOCATION_TIMEOUT · Vercel Runtime Timeout Error: Task timed out after 300 seconds
FUNCTION_INVOCATION_TIMEOUT means your Vercel Function (an API route, server action or server-rendered page) didn't finish before its maximum duration, so Vercel stopped it and returned a 504. Usually the code is waiting on something slow or never sends a response: a hanging database connection, a slow AI or third-party API call, or a code path with no return. Find what it is waiting for in the logs before raising the limit.
Common causes – and how to tell which one you have
1. A code path never returns a response
How to tell: The function always hits the exact limit (for example 'Task timed out after 300 seconds') even for simple requests, and the logs show no error. Often an early if branch, a catch block or a callback-style handler doesn't send a response.
Fix: Make sure every branch returns a Response (or calls res.status(...).json(...) in the pages router), including error branches inside catch.
2. A slow external call: AI model, scraper or third-party API
How to tell: The timeout happens on the route that calls OpenAI, Anthropic, a scraper or a slow API, and it gets worse with longer inputs. Locally it works because there is no limit.
Fix: Stream the AI response instead of waiting for the whole answer, set a timeout on the outgoing fetch (AbortSignal.timeout), and move long jobs into a background job or queue that the page polls.
3. Database connection hangs or the database is far away
How to tell: Requests that touch the database time out, especially the first one after a pause. The database region is far from the function region, or the code opens a new connection on every request without a pooler.
Fix: Use your database's pooled connection string (for example Supabase's pooler or Prisma Accelerate), reuse one client instance, and set the function region close to the database in the project settings.
4. The plan's maximum duration is too low for real work
How to tell: The work genuinely takes long (big exports, video or long AI chains) and finishes just after the limit. Older projects without Fluid compute can still have much shorter limits (10–60 seconds on Hobby).
Fix: Enable Fluid compute for the project, and set export const maxDuration = 300 (or what your plan allows) in the route. For jobs that take longer, use a background job or Vercel Workflows instead of one request.
5. An infinite loop or an unhandled promise
How to tell: CPU-heavy code, a while loop or a recursive call; or a promise that's never awaited and never resolves. The logs may show an unhandled rejection before the timeout.
Fix: Add logging around the loop or the awaited calls to see where it stops, fix the loop condition, and await every promise the response depends on.
How to fix it
- Open the deployment in the Vercel dashboard and go to Logs. Filter by the failing route and read the last log lines before the timeout.
- Note the exact message: 'Task timed out after N seconds' tells you the current limit for that function.
- Add console.time / console.log around each slow call (database, AI, external API) and redeploy to see which one hangs.
- Check that every branch of the handler returns a response, including catch blocks.
- Fix the slow part: streaming for AI responses, a fetch timeout with AbortSignal.timeout(), a pooled database connection, and a function region near your database.
- Only if the work really needs more time: enable Fluid compute and set export const maxDuration in that route, within your plan's limit, and redeploy.
Using Lovable, Bolt or v0?
In v0 or Bolt, tell the AI: "This route times out on Vercel (504 FUNCTION_INVOCATION_TIMEOUT). Add timing logs around every database and API call, make sure every code path returns a response, and stream the AI response instead of waiting for it." Then check the Logs tab of the deployment on Vercel to see which call was slow.
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to trace the request through the handler and list every awaited call with a rough expected duration. Have it add timing logs, check that each branch returns a Response, and confirm the database client is created once at module level. Use the Vercel CLI (vercel logs <deployment-url>) or the dashboard to read the logs after redeploying.
The “fix” to avoid
Agents often just set maxDuration to the maximum or add retries. If the code never returns a response or a connection hangs, it still times out, only later, and you pay for the extra seconds. Find what it's waiting for first.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My Vercel deployment returns 504 FUNCTION_INVOCATION_TIMEOUT on one route. Find the handler (API route, route handler, server action or page) and check: 1) does every branch, including catch blocks, return a response? 2) which awaited calls can be slow (database, AI model, external API) — add timing logs and an AbortSignal.timeout() on outgoing fetches; 3) is the database client reused and using a pooled connection string? 4) can long AI responses be streamed? Fix the real cause first. Only then set export const maxDuration if the work genuinely needs longer, within my plan's limit. Do not just wrap it in a retry loop. Tell me what was slow.
Questions people ask
What is the Vercel function time limit?
With Fluid compute (on by default for new projects since April 2025), functions get 300 seconds by default. Hobby can't go higher; Pro and Enterprise can set maxDuration up to 800 seconds. Older projects without Fluid compute can have much shorter limits.
Why does it work locally but time out on Vercel?
Locally there is no duration limit and your database may be on the same machine. On Vercel the function has a hard limit and may run in a region far from your database.
Is 504 FUNCTION_INVOCATION_TIMEOUT the same as a build timeout?
No. This happens when a deployed function runs, not during the build. Build problems show up in the build logs before deployment.
Official documentation
- Vercel: FUNCTION_INVOCATION_TIMEOUT
- Vercel: Configuring maximum duration
- Vercel: What can I do about functions timing out?