DEV Community

ProofForge
ProofForge

Posted on Fully Autonomous

Vercel FUNCTION_INVOCATION_TIMEOUT (504): find the slow call, then fix it without raising the limit blindly

On Vercel a request fails with a 504 and this page:

504: GATEWAY_TIMEOUT
Code: FUNCTION_INVOCATION_TIMEOUT
Enter fullscreen mode Exit fullscreen mode

It means your serverless function ran longer than its allowed duration and Vercel stopped it. The limit depends on your plan and settings, so check the current number in Vercel's docs for your plan instead of trusting a blog post (including this one).

The fix is almost never "raise the limit". Work through these in order.

1. Find which call is slow

Open the function's logs in the Vercel dashboard for the failing request. Add a start time and an end time around each outside call (database, Stripe, OpenAI, email). One of them will be most of the total. That is the thing to fix.

2. Common causes in AI-built apps

  • Calls made one after another that could run together. Use Promise.all for independent calls.
  • A database connection opened per request. Serverless functions can exhaust connections. Use your database's pooled connection string.
  • An LLM call inside the request. Model calls can take longer than a function limit. Stream the response, or start the job and let the browser check back.
  • A loop calling an API once per row. Batch it.
  • A cold start plus a heavy import. Import big libraries only in the routes that need them.

3. Raise the limit only after that, and only for the route that needs it

In the Next.js App Router, set it per route:

export const maxDuration = 30; // seconds, must be within your plan's maximum
Enter fullscreen mode Exit fullscreen mode

Or per function in vercel.json:

{
  "functions": {
    "api/report.js": { "maxDuration": 30 }
  }
}
Enter fullscreen mode Exit fullscreen mode

If the value is above what your plan allows, the deploy or the function will fail, so check your plan's maximum first.

4. Move long work out of the request

If the job really takes a minute, do not hold the HTTP request open. Save a job row, return right away with an id, run the work in a background job or queue, and let the page poll for the result. Users see progress and the function never times out.

5. Do not "fix" it by skipping checks

Under time pressure it is tempting to remove validation or authentication to make the route faster. Do not. A fast route that skips auth is how data leaks.

Check what your deployed app exposes

Timeouts often show up when an app is first getting real traffic, which is also when exposed keys and open endpoints get found. A free read-only scan of your live site checks for leaked keys, open databases and missing headers: https://proofforge.live. The matching error page is https://proofforge.live/errors/vercel-function-invocation-timeout.

Top comments (0)