Vercel 504 FUNCTION_ INVOCATION_ TIMEOUT: why it happens and how to fix it
A Vercel function that runs past its time limit returns a 504 with FUNCTION_INVOCATION_TIMEOUT. How to find the slow step, raise the limit safely and move long work out of the request.
· 6 min read · By the Spot Downtime team
A page or API route on Vercel hangs for a while, then returns a plain error page: 504: GATEWAY_TIMEOUT with the code FUNCTION_INVOCATION_TIMEOUT. It means your serverless function was still running when it hit its maximum duration, and Vercel stopped it and answered for it.
The limit is not a bug, it is a budget. The fix is almost never "raise the limit and hope". It is to find out what used the time, and then either make it faster, cap it, or take it out of the request.
What the error means
Every function on Vercel has a maximum duration: the longest a single invocation may run before it is cut off. The default and the ceiling depend on your plan and on the runtime settings, and Vercel changes them from time to time, so check the current numbers in their documentation and in your project's function settings instead of trusting a figure from a blog post, including this one.
The response is a 504, the status code for "a gateway did not get an answer in time". Here the gateway is Vercel's own, and the function behind it is yours. Our general guide to 502, 503 and 504 errors covers the same family on other stacks.
Related, but different
FUNCTION_INVOCATION_FAILED is a 500: your code threw or crashed. EDGE_FUNCTION_INVOCATION_TIMEOUT is the same idea for Edge functions, which must start responding quickly and have different limits. This post is about the serverless timeout.1. Find out which step is slow
Open the project in Vercel, go to the deployment's logs and filter for the failing route around the time of the error. A timed-out invocation has no error from your code, because it never finished, so look for the last log line it did print.
If your code prints nothing, add timing around each step that talks to something else:
const t0 = Date.now();
const user = await db.user.findUnique({ where: { id } });
console.log("db ms", Date.now() - t0);
const t1 = Date.now();
const res = await fetch("https://api.example.com/report");
console.log("report api ms", Date.now() - t1);One slow line almost always accounts for the whole timeout. The usual suspects are below.
2. A call with no timeout of its own
A fetch to a third-party API, a webhook, or an LLM provider has no timeout by default in Node. If the other side stalls, your function waits until Vercel kills it. Put a shorter limit on every outbound call so you fail on your terms and can return a useful error:
const res = await fetch("https://api.example.com/report", {
signal: AbortSignal.timeout(8000), // give up after 8 seconds
});Pick a value comfortably below your function's maximum duration. Then handle the abort: return a 504 or a cached answer yourself, with a message a user can act on, rather than the generic Vercel page.
3. A database that is slow, far away or out of connections
- Region mismatch. If functions run in one region and the database sits in another, every query pays a round trip across an ocean, and a page with ten sequential queries pays it ten times. Move the function region next to the database, or the other way round.
- Connection exhaustion. Each concurrent invocation can open its own connection. Under load the database runs out of slots and new invocations wait. Use a pooler or your provider's serverless driver, and reuse the client across calls by creating it at module scope.
- Missing index. A query that was fine with a thousand rows is not fine with a million. Run
EXPLAINon the slow one. - Sequential awaits that could be parallel. Use
Promise.allfor independent queries.
4. Cold starts and heavy imports
The first request to an idle function has to load your code before running it. A large bundle, a big SDK imported at the top of the file, or work done at import time all add to that. Usually this costs a second or two, not a timeout, but it pushes an already slow request over the line. Import heavy libraries only in the routes that use them, and keep expensive setup out of module scope when it is not needed on every request.
5. Work that does not belong in a request
Generating a large PDF, resizing a batch of images, calling a slow model, importing a CSV, sending 500 emails: if the user does not need the result in the same response, do not make the request wait for it. Instead:
- Accept the request and return quickly, with a
202 Acceptedand an id. - Hand the work to a queue or background job runner, or use your platform's way of finishing work after the response is sent.
- Let the client poll an endpoint for the result, or notify it by email or webhook when it is done.
In Next.js route handlers, after() from next/server (and waitUntil from @vercel/functions) lets short follow-up work continue after the response is sent. They still run inside the function's maximum duration, so they suit small jobs such as logging and cache warming, not long ones.
6. Raising the limit, when it is the right answer
Some work is legitimately slow and has to happen in the request, like streaming a long model response. Then set a higher maximum duration for that route only. In a Next.js App Router file:
// app/api/report/route.ts
export const maxDuration = 60; // seconds, up to your plan's ceilingOr for all matching functions in vercel.json:
{
"functions": {
"app/api/report/route.ts": { "maxDuration": 60 }
}
}A value above what your plan allows is rejected at deploy time, and raising the limit only moves the cliff. A function that needs 55 seconds today and has no timeouts on its calls will need 65 next month. Keep the caps from step 2 in place.
7. Catch it before your users do
A function that is slow on a few routes still returns 200 on your homepage, so a basic site check stays green. Monitor the routes that matter directly:
- Add an API monitor for each important endpoint, with an assertion on the status code and the fields you expect.
- Watch the response-time chart for creep: a route that goes from 2 seconds to 8 is about to time out. Our guide to tracking API response times shows how to set a slowness alert.
- Run a check after each release; see checking your site after every deploy.
- If the platform itself is having a bad day, monitoring Vercel and other clouds explains how to tell your bug from theirs.
Quick checklist
- Read the logs for the route and find the last line before the cut-off.
- Add timing around each outbound call and query.
- Put an
AbortSignal.timeouton every external fetch. - Put the function in the same region as the database, and pool connections.
- Move anything the user does not wait for into a background job.
- Raise
maxDurationfor that one route only if the work is truly slow. - Monitor the route and its response time so the next slowdown pages you first.
Get new guides by email
Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.
Keep reading
- HTTP errors · Troubleshootingnginx 502 Bad Gateway: fixing "connect() failed" to PHP-FPM and other upstreamsnginx logs connect() failed (111), (2) or (13) when it can't reach your app. What each errno means, and how to fix PHP-FPM sockets, ports, permissions and SELinux.November 13, 2026 · 7 min read
- HTTP errors · Troubleshooting407 Proxy Authentication Required: what it means and how to fix itA 407 comes from a proxy, not the website. Why it appears in browsers, curl and CI jobs, how it differs from 401, and how to send proxy credentials correctly.November 12, 2026 · 6 min read