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
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.
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.
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.
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.
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.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.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.See scope and start
Related: Build failed on Netlify or Vercel · 404 on refresh on Netlify or Vercel · Backend and API development