Fix · API calls hang in production: add timeouts, retries and real checks

Vercel function timeout 504 calling OpenAI, works locally

The route that calls OpenAI works locally, but in production it hangs until Vercel returns a 504 with FUNCTION_INVOCATION_TIMEOUT. It usually comes from a function deadline shorter than the model call, a call with no deadline of its own, or a function running far from the API.

Likely causes, most common first

Cause 1 · in 1 of 4 matching cases

The function's deadline is shorter than the model call

Vercel ends an invocation that runs past its maximum duration, and that limit depends on the runtime and plan. A function on the Edge runtime must also begin sending a response within 25 seconds. Newer Next.js releases run routes on Node.js instead of the Edge runtime.

How to tell: Failing requests end at the same elapsed time, close to a configured maxDuration or to 25 seconds on an edge route.

Cause 2 · in 1 of 4 matching cases

The function runs in a region far from the API, or in a different region per request

Distance to an external service adds latency. A function on the Edge runtime runs in the region closest to the incoming request by default, so different callers can take different paths to the API. A slow path from one region can fail while another succeeds.

How to tell: Logging VERCEL_REGION on each failure shows the failures cluster in some regions.

Cause 3

Nothing on your side stops the OpenAI call before Vercel does

The official OpenAI Node client waits 10 minutes by default and retries timed-out requests twice. A slow call can therefore outlast the function, and your handler gets no chance to log the client's own error. Vercel returns its timeout instead.

How to tell: The logs show the request start and no OpenAI error or status line before the 504.

Cause 4

The handler throws or returns without a response

A function that returns no response times out. Vercel also lists an unhandled exception as a common cause of this error, so a throw inside the handler can surface as a timeout.

How to tell: The application logs show an exception or an early return on the failing path, with no response written.

Check and fix it, step by step

  1. Confirm Vercel is the one ending the request

    Look for FUNCTION_INVOCATION_TIMEOUT on the failing requests and note how long each ran. Then read the application logs at the host URL under the /_logs path for exceptions.

    Docs: vercel.com →

  2. Find which runtime and duration apply to the route

    Search the route for an Edge runtime setting and for maxDuration. For Next.js App Router the duration is a named maxDuration export. Pages router and /api routes use an exported config object.

    grep -rnE "runtime = 'edge'|runtime: 'edge'|maxDuration|timeout:" app api pages

    Docs: vercel.com →

  3. Log the region on every failure

    Print the region next to the outcome of each OpenAI call. If failures cluster in some regions, treat it as a region problem before changing any limits.

    console.log("region", process.env.VERCEL_REGION);

    Docs: vercel.com →

  4. Put a deadline on the OpenAI call and cap retries

    Set the client timeout below the function's maximum duration and lower maxRetries, so one slow call plus its retries fits inside the function limit. On timeout the client throws APIConnectionTimeoutError. Catch it and return a response, because a function that returns none will time out.

    const client = new OpenAI({ timeout: 20 * 1000, maxRetries: 0 }); // defaults: 10 minutes, 2 retries

    Docs: github.com →

  5. Set a maximum duration that fits the call

    Export maxDuration from the route, within the limit for your plan. On the Edge runtime, send the first bytes of the response within 25 seconds and keep streaming, or move the route to Node.js, which Vercel recommends.

    export const maxDuration = 60; // seconds, App Router route file

    Docs: vercel.com →

  6. Run the function close to the API it calls

    Set regions in vercel.json, or per function under functions when different functions call different services. Pinning one region also makes a region-dependent failure easier to reproduce.

    {
      "regions": ["iad1"]
    }

    Docs: vercel.com →

  7. Check the function's memory size before raising limits elsewhere

    Open the deployment's Resources tab, select the function and hover the information icon in Observability to see its memory size. If the function does heavy work itself, raise the size in the project's Function CPU setting and redeploy.

    Docs: vercel.com →

Quick check: grep -rnE "runtime = 'edge'|maxDuration|timeout:" app api pages; then log process.env.VERCEL_REGION on each failure

How often this shows up in our data

4 of the 937 verified cases from the last 12 months in our data match this symptom (0.4%). The most common cause was “The function's deadline is shorter than the model call” (1 of 4); 2 didn't show which cause. How we collect and verify cases.

With Gemmein

On Gemmein, the browser calls the model through a named AI tool: the call has 170 s in all, and on a stream 10 s to the first response headers, and with no answer before the first byte it answers provider_unreachable (502) and the credit is refunded. A generate or transcribe tool answers with 202 { run }, a pipeline on provider external is one tool and one run, and a run open past its deadline (60 minutes, or the tool's deadlineMinutes) becomes expired with its hold returned.

Questions

Why do I get a Vercel 504 instead of an error from OpenAI?

Vercel returns FUNCTION_INVOCATION_TIMEOUT when an invocation runs longer than the allowed execution time. If the OpenAI client has not timed out by then, its own error does not reach your handler.


Does the OpenAI client retry a timed-out request?

Yes, the Node client retries timed-out requests twice by default, with a short exponential backoff. Set maxRetries and timeout so the total fits inside the function limit.


Can I set function memory in vercel.json?

No. Vercel warns at build time if you try. On Pro and Enterprise you change the default in the project settings under Functions, Advanced Settings, Function CPU, then create a new deployment.


What if the AI response really takes longer than the function limit?

Vercel points to Fluid compute for longer durations and to Vercel Workflows for work that needs more time than a function allows. On the Edge runtime you can also start the response within 25 seconds and keep streaming.


Sources

Every cause and step above was checked against these pages on 3 Nov 2026.

The broader pattern

This is one symptom of a wider failure pattern: API calls hang in production: add timeouts, retries and real checks. The guide covers every cause we see for it, on any stack.

Get a heads-up when OpenAI or Vercel breaks something

OpenAIVercel

Leave your email and we'll let you know when something big changes for OpenAI and Vercel. Unsubscribe any time by replying. Gemmein Limited. Research terms · Privacy

← All fixes