November 22, 2026 · 8 min read · Thomas Osayi
Vercel function timeout (FUNCTION_INVOCATION_TIMEOUT): causes and fixes
What the Vercel FUNCTION_INVOCATION_TIMEOUT 504 error means, the current maxDuration limits per plan, and how to fix it with config, streaming or background jobs.
The app works on your machine. You deploy to Vercel, click the button that generates a report or calls an AI model, wait, and get a page that says 504: GATEWAY_TIMEOUT with the code FUNCTION_INVOCATION_TIMEOUT underneath.
This one is common in AI-built apps because they lean on slow outside calls: language models, PDF generation, imports, scraping, bulk emails. Here is what the error actually means, the current limits, and the fixes in the order I try them.
What does FUNCTION_INVOCATION_TIMEOUT mean?
It means one of your Vercel Functions ran longer than its maximum allowed duration, so Vercel terminated it and returned an HTTP 504.
Every API route, server action and server-rendered page on Vercel runs as a function with a time limit. When the limit passes, Vercel stops the function, whatever it was doing, and the user gets the error page. Any work not finished is lost unless you designed for that.
Vercel's error documentation lists five causes, and in my experience they show up in roughly this order in AI-built apps:
- The function takes too long, usually waiting on an external API or a slow database query.
- The function never returns a response. Some code path finishes without returning anything, so the request hangs until the limit.
- Upstream errors. An external API or database is failing or hanging and the code waits forever.
- Unhandled exceptions that leave the request without a response.
- An infinite loop.
Note the second one. A route that times out only in certain cases is often not slow at all. It just forgot to return in one branch, such as an early exit for a missing parameter.
What are the current Vercel function time limits?
At the time of writing, with fluid compute enabled, which is the default, functions get a default of 300 seconds on every plan, Hobby can go no higher than 300 seconds, and Pro and Enterprise can raise it to 800 seconds.
Vercel's duration documentation, current at the time of writing, sets these limits:
- Hobby: 300 seconds default, 300 seconds maximum.
- Pro: 300 seconds default, 800 seconds maximum, and up to 1,800 seconds (30 minutes) in an extended duration beta.
- Enterprise: same as Pro.
The 30 minute beta applies to supported Node.js, Bun and Python runtimes, and during the beta anything above 800 seconds must be set per function, not as a project default. For work with no time limit at all, Vercel points to its Workflows product.
Two caveats. Older projects that still have fluid compute turned off run with much lower limits, so check Settings, then Functions, in your project before assuming the numbers above apply. And limits change, so confirm against the documentation in the sources below if you are reading this months later.
How do I set maxDuration in Next.js?
In the App Router, export a constant named maxDuration from the route file, in seconds; for a project-wide default, use the dashboard or vercel.json.
The three ways:
- Per route in the App Router: add export const maxDuration = 60 at the top of app/api/report/route.ts or a page file.
- Per route in the Pages Router: export a config object with maxDuration set from the API route file.
- Project-wide: set Default Max Duration under Settings, Functions, or add a functions entry in vercel.json with a glob like app/api/ followed by a wildcard and a maxDuration value.
If your project uses the src directory, the vercel.json paths need the src prefix or they will not match.
Raising the limit is the right fix when the work is legitimately long but bounded, for example a report that reliably takes 90 seconds. It is the wrong fix when the function hangs. A function that never returns will hang for 800 seconds just as happily as for 300, and you pay for the compute while it waits.
How do I find out why my function is slow?
Read the runtime logs for the failing request, then time each step inside the function so you know which call eats the time.
In the Vercel dashboard, open the project, go to Logs, and filter by the failing route. Look for errors logged just before the timeout, and for the duration of each request. Then add simple timing logs around each step: the database query, each external API call, any loop over many items. Deploy and trigger it again.
The usual findings:
- An AI model call generating a long response without streaming, taking 40 to 120 seconds.
- A loop that calls an API once per row, such as sending 500 emails one at a time.
- A database connection that waits because the pool is exhausted. Serverless functions open many connections, so use your database's pooled connection string; Supabase provides one for exactly this.
- A fetch to an external API with no timeout, which waits for a server that never answers. Add an AbortSignal timeout so it fails fast with a clear error.
- A slow query missing an index, which my post on speeding up a slow Lovable app covers in detail.
How does streaming help with timeouts?
Streaming sends the response in pieces as it is produced, so users see output within a second or two and long-running connections do not sit idle, but it does not lift the duration limit.
For AI features this is the most important fix. Instead of waiting for a model to finish a 2,000 word answer and then returning it, stream tokens to the browser as they arrive. Libraries like the Vercel AI SDK make this a few lines: the route returns a streaming response and the client renders text as it comes in. The user experience changes from a frozen spinner to text appearing immediately.
Streaming also helps with intermediaries. Vercel's documentation notes that HTTP/1.1 clients and network layers may close idle connections, and recommends streaming progress or heartbeat data while work runs.
What streaming does not do is let a function run forever. The stream still has to finish within maxDuration. If a single model call can take longer than your limit, you need a background job instead.
When should I move work into a background job?
Move work into the background whenever it does not need to finish before the user sees a response, or when it can take longer than your plan's limit.
Vercel's background workers guide lists the main options:
- waitUntil from the Vercel functions package, or after() in Next.js 15.1 and later, runs a short task after the response is sent. It stays within the function's max duration and has no retries, so a failed task is lost. Good for logging or a single follow-up call.
- Cron Jobs run a function on a schedule defined in vercel.json, in production only. At the time of writing Hobby supports one run per day per cron job, while Pro and Enterprise allow intervals as short as one minute. Make cron jobs safe to run twice, since delivery can be missed or duplicated.
- Queues give you retries and at least once delivery, so consumers must be idempotent.
- Workflows split work into steps that can pause, retry and resume, with each step limited by the function max duration but the whole run unlimited.
The usual pattern: the route saves a job row with status pending, returns immediately, and the background process does the work and updates the row. The interface polls or subscribes and shows progress.
When I think about features like SQWAD's revenue attribution, which ties Meta ads back to the creator and video behind them, that is the kind of bulk data work I would never try to squeeze into a single user request.

How do I stop timeouts from coming back?
Give every slow operation an explicit budget, watch durations in your logs, and test with realistic data sizes before each release.
Timeouts tend to return quietly as data grows. A route that handled 50 records in 20 seconds at launch handles 500 records a few months later and starts failing. A few habits keep that from surprising you:
- Set your own timeouts on outside calls, shorter than maxDuration, so a hanging API fails with a clear error you can log and retry, instead of taking the whole function down with it.
- Cap batch sizes in code. If a route processes items in a loop, limit it to a number you know finishes comfortably, and hand the rest to a background job.
- Watch function durations in the Vercel dashboard. A route creeping from 10 seconds toward the limit is a warning you can act on early.
- Keep a staging copy with production-sized data, so a slow path shows up before users see it.
- Return a useful error message to the interface, so users see "this is taking longer than expected" rather than a raw 504 page.
What does a typical fix look like? A worked example
Here is an illustration with a made-up app: a property management tool built in Bolt, deployed on a Hobby Vercel plan, with a "generate monthly owner reports" button that timed out for larger clients.
The logs showed the route looping over every property, calling an AI model to write a summary for each, then building a PDF, all inside one request. Small clients with five properties finished in a minute. A client with 40 properties needed about eight minutes and hit the limit every time.
The fix had three parts. The button now creates a report job and returns at once. A background workflow processes one property per step, so no single step comes close to the limit. The AI summary calls stream when a user previews one report interactively, and the final PDF is built once all steps finish. The interface shows a progress bar and emails the owner when the report is ready. Nothing needed a plan upgrade.
A single cause, like a missing return or an unpooled connection, fits my Quick Fix from $60. Restructuring a long task into a background job usually fits Fix Plus Features from $200.
The bottom line
FUNCTION_INVOCATION_TIMEOUT is Vercel telling you a function ran past its limit or never answered. Check that every code path returns, find the slow step with logs and timing, then pick the right fix: a higher maxDuration for bounded work, streaming for AI responses, and background jobs for anything long or bulk. If you are seeing other deploy errors too, "Vercel build failed? The most common Next.js deploy errors and fixes" covers the build side.
Questions, answered in one line
What does FUNCTION_INVOCATION_TIMEOUT mean on Vercel?
It means a Vercel Function took longer than its allowed maximum duration, so Vercel stopped it and returned a 504 Gateway Timeout.
How do I increase the timeout of a Next.js route on Vercel?
In the App Router, export a maxDuration constant in seconds from the route file, staying within the maximum your plan allows.
Does upgrading to Vercel Pro fix timeouts?
It raises the ceiling from 300 to 800 seconds at the time of writing, but a function that hangs or never returns will still time out.
Does streaming stop a function from timing out?
Streaming gets the first bytes to the user quickly and keeps connections from looking idle, but the function is still bound by its maximum duration.
Can someone fix Vercel timeouts in my AI-built app?
Yes, Thomas Osayi fixes a single timeout bug from $60 with his Quick Fix package, or moves long work into background jobs as part of Fix Plus Features from $200.
Sources
Related articles
- Supabase auth redirect URL not working? Fix localhost, preview and production redirects
- Environment variables undefined on Vercel? Every cause and fix
- How to fix a CORS error on a Next.js API route
- Firestore "Missing or insufficient permissions": what triggers it and how to fix it
Fix, finish and launch your AI-built app
Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.
See the service