← Back to All Posts
November 2026 · 5 min read · Vercel · LangChain · Serverless

Fixing Common Timeout Errors in Vercel for AI & LangChain Apps

Deploying an AI chatbot or RAG endpoint on Vercel feels effortless—until your LLM call takes 12 seconds and production suddenly returns 504: FUNCTION_INVOCATION_TIMEOUT.

Standard serverless functions are designed for fast database queries, not multi-step LangChain agents or long GPT completions. Below are four practical ways to eliminate timeout errors without rewriting your entire app.

1. Switch From Buffered Responses to Streaming

By default, waiting for an entire LLM completion before sending HTTP bytes forces the serverless function to block until the very last token finishes. Streaming sends the first token in under 1–2 seconds and keeps the HTTP connection alive while tokens arrive.

In Next.js App Router, return a ReadableStream directly from your Route Handler:

import { ChatOpenAI } from "@langchain/openai";

export const runtime = "edge";

export async function POST(req) {
  const { prompt } = await req.json();

  const model = new ChatOpenAI({
    modelName: "gpt-4o-mini",
    streaming: true,
  });

  const stream = await model.stream(prompt);

  const readable = new ReadableStream({
    async start(controller) {
      const encoder = new TextEncoder();
      for await (const chunk of stream) {
        controller.enqueue(encoder.encode(chunk.content));
      }
      controller.close();
    },
  });

  return new Response(readable, {
    headers: { "Content-Type": "text/plain; charset=utf-8" },
  });
}

2. Increase maxDuration Explicitly

Vercel defaults serverless endpoints to conservative timeout thresholds unless you declare a higher limit per route. Export maxDuration at the top of your API route file:

// app/api/chat/route.js
export const maxDuration = 60; // seconds (up to plan limit)

export async function POST(req) {
  // Your LangChain or vector retrieval logic here
}
Plan Check: On Vercel Hobby plans, streaming + Edge runtime or configuring maxDuration up to the Hobby cap (60s with Fluid Compute enabled) prevents most single-turn LLM timeouts. For Pro teams, you can extend this up to 300 seconds.

3. Use the Edge Runtime for Lightweight Proxy Calls

If your route only calls OpenAI, Gemini, or Pinecone over HTTP without heavy native Node.js binaries, adding export const runtime = "edge"; removes cold starts and lets streaming responses continue past initial connection limits—as long as the first byte is sent within 25 seconds.

4. Offload Heavy RAG Indexing to AWS or Background Queues

PDF chunking, embedding generation, and vector upserts should never run synchronously inside a Vercel API request. If a user uploads a 50-page PDF, split the architecture cleanly:

  1. Upload the document directly from the browser to Amazon S3 using a pre-signed URL.
  2. Trigger an asynchronous worker on AWS EC2, AWS Lambda, or a queue service (like Upstash QStash) to chunk and embed the file into ChromaDB or Pinecone.
  3. Poll a lightweight status endpoint from the frontend until indexing completes.

Quick Troubleshooting Checklist