Configuring Maximum Duration for Vercel Functions

Learn how to set the maximum duration of a Vercel Function.
Table of Contents

The maximum duration configuration determines the longest time that a function can run. This guide will walk you through configuring the maximum duration for your Vercel Functions.

You are charged based on the amount of time your function has run, also known as its duration. It specifically refers to the actual time elapsed during the entire invocation, regardless of whether that time was actively used for processing or spent waiting for a streamed response. To learn more see Managing function duration.

For this reason, Vercel has set a default maximum duration for functions, which can be useful for preventing runaway functions from consuming resources indefinitely.

If a function runs for longer than its set maximum duration, Vercel will terminate it. Therefore, when setting this duration, it's crucial to strike a balance:

  1. Allow sufficient time for your function to complete its normal operations, including any necessary waiting periods (for example, streamed responses).
  2. Set a reasonable limit to prevent abnormally long executions.

The method of configuring the maximum duration depends on your framework and runtime:

For these runtimes / frameworks, you can configure the number of seconds directly in your function:

app/api/my-function/route.ts
export const maxDuration = 5; // This function can run for a maximum of 5 seconds
 
export function GET(request: Request) {
  return new Response('Vercel', {
    status: 200,
  });
}

For these runtimes and frameworks, configure the maxDuration property of the functions object in your vercel.json file:

vercel.json
{
  "functions": {
    "api/test.js": {
      "maxDuration": 30 // This function can run for a maximum of 30 seconds
    },
    "api/*.js": {
      "maxDuration": 15 // This function can run for a maximum of 15 seconds
    },
    "src/api/*.js": {
      "maxDuration": 25 // You must prefix functions in the src directory with /src/
    }
  }
}

If your Next.js project is configured to use src directory, you will need to prefix your function routes with /src/ for them to be detected.

The order in which you specify file patterns is important. For more information, see Glob pattern .

While Vercel specifies defaults for the maximum duration of a function, you can also override it in the following ways:

  1. From your dashboard, select your project and go to the Settings tab.
  2. From the left side, select the Functions tab and scroll to the Function Max Duration section.
  3. Update the Default Max Duration field value and select Save.
vercel.json
{
  "functions": {
    "app/api/**/*": {
      "maxDuration": 5 // All functions can run for a maximum of 5 seconds
    }
  }
}

This glob pattern will match everything in the specified path, so you may wish to be more specific by adding a file type, such as app/api/**/*.ts instead.

Vercel Functions have the following defaults and maximum limits for the duration of a function:

DefaultMaximum
Hobby10s60s
Pro15s300s
Enterprise15s900s

If you have enabled fluid compute, the following defaults and maximum limits apply:

DefaultMaximum
Hobby60s60s
Pro90s800s
Enterprise90s800s
Last updated on August 23, 2024