Skip to content

Vercel errorVercel function times out with a 504 (FUNCTION_INVOCATION_TIMEOUT)

An API route works locally, then times out on Vercel, often one that calls an AI model or a slow database. Raising the limit can help, but find what’s slow first. Here’s how, or hand it over at a fixed price.

Short answer

Vercel returns a 504 when a function doesn’t finish within its maximum duration, and that time includes sending the response. Find the slow call first: an AI or outside API with no timeout, a slow database query, or a database far from the function’s region. Then stream the response or move long work to a background job. Raise maxDuration last, within your plan’s limit.

Find the slow part, then fix it

  1. 01

    The limit includes sending the response

    “For request handlers, this includes time spent processing the request and sending the response, including streamed responses.” Vercel, Functions limits

    The maximum duration covers processing the request and sending the response, streamed responses included. A reply that streams for a long time still counts toward it.

  2. 02

    An AI or outside API call with no timeout

    A call to an AI model or another service can take far longer on a busy day than in testing. Give each call its own timeout, shorter than the function’s limit, and show the user a clear error when it runs out.

  3. 03

    A slow query, or a database far away

    Functions run in one region by default, iad1, unless you change it. If your database is in another region, every query makes the round trip. Run the function near the database, and check slow queries for missing indexes.

  4. 04

    Work that doesn’t belong in a request

    “For workloads that require unlimited execution time, use Vercel Workflows” Vercel, Functions limits

    Long jobs such as reports, imports or several AI calls in a row should run in a background job or queue, with the request answering straight away. For work that needs unlimited time, Vercel points to Vercel Workflows.

  5. 05

    Then raise maxDuration, within your plan

    “Next.js pages router and Node.js /api routes use an exported config object, while the Next.js app router uses a named maxDuration export.” Vercel, Maximum duration

    For Node.js functions with fluid compute, the default is 300 seconds on every plan. Hobby can’t go higher; Pro and Enterprise can set up to 800 seconds, and up to 1800 in beta for some runtimes. Set it per function in the route file.

Checked against the vendor’s docs on : Vercel docs: Functions limits; Vercel docs: Configuring maximum duration.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: Build failed on Netlify or Vercel · 404 on refresh on Netlify or Vercel · Backend and API development

Questions

More on this error

Why does it work locally but time out on Vercel?

Your own computer usually has no such limit, and your database may be close by. On Vercel the function has a maximum duration and runs in one region by default, so slow or distant calls add up.

Does waiting on an AI model count toward the timeout?

Yes. The maximum duration is the whole time the invocation runs, waiting included. Waiting on I/O doesn’t count toward active CPU billing, but it still counts toward the timeout.

Can you find the slow call for me?

The Vercel project stays in your account. We trace the slow calls, add timeouts, streaming or a background job, and send it as a pull request you review and deploy.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; we reply by email, usually within one business day.