How to Fix 'connection timeout' in LangChain (TypeScript)

By Cyprian AaronsUpdated 2026-04-21
connection-timeoutlangchaintypescript

What the error means

connection timeout in LangChain usually means your app tried to call an LLM, embeddings API, vector store, or tool endpoint and never got a response before the network layer gave up. In TypeScript projects, this shows up most often during invoke(), stream(), or vector store initialization when the request is blocked, misconfigured, or too slow.

The key thing: this is usually not a LangChain bug. It’s almost always a network, config, or runtime issue around the request path.

The Most Common Cause

The #1 cause is creating a new client per request and letting it use default timeouts, especially in serverless or API route handlers. With LangChain JS/TS, this often happens when you instantiate ChatOpenAI, OpenAIEmbeddings, or a retriever inside the handler and then hit cold starts, DNS delays, or slow upstream responses.

Here’s the broken pattern versus the fixed one:

BrokenFixed
New client created on every requestReuse a singleton client
No explicit timeout/retry settingsSet sane timeout/retry values
Handler waits on slow init + networkKeep initialization outside hot path
// ❌ Broken: creates a new model client on every request
import { ChatOpenAI } from "@langchain/openai";

export async function POST(req: Request) {
  const body = await req.json();

  const model = new ChatOpenAI({
    apiKey: process.env.OPENAI_API_KEY,
    model: "gpt-4o-mini",
    // default timeout can be too short for your environment
  });

  const result = await model.invoke(body.prompt);
  return Response.json({ result });
}
// ✅ Fixed: reuse the client and set explicit network settings
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  model: "gpt-4o-mini",
  timeout: 30_000,
  maxRetries: 2,
});

export async function POST(req: Request) {
  const body = await req.json();
  const result = await model.invoke(body.prompt);
  return Response.json({ result });
}

If you’re using LCEL, the same rule applies. Build chains once, not inside every request path.

import { ChatOpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";

const model = new ChatOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  model: "gpt-4o-mini",
  timeout: 30_000,
});

const prompt = PromptTemplate.fromTemplate("Answer briefly: {question}");
const chain = prompt.pipe(model).pipe(StringOutputParser());

export async function ask(question: string) {
  return chain.invoke({ question });
}

Other Possible Causes

1) Wrong base URL or proxy config

If you’re routing through Azure OpenAI, OpenRouter, a corporate proxy, or an internal gateway, a bad baseURL often looks like a timeout instead of a clean auth error.

// Check this carefully
const model = new ChatOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: process.env.OPENAI_BASE_URL, // typo here causes silent connection issues
});

Make sure the URL is reachable from your runtime and includes the correct protocol:

baseURL: "https://api.openai.com/v1"

2) DNS / outbound network blocked in your environment

This happens in Docker, Kubernetes, Vercel serverless, corporate VPCs, or locked-down CI runners. The code looks fine, but the runtime cannot reach the endpoint.

# Test connectivity from the same environment
curl -I https://api.openai.com/v1/models

If that hangs or fails, LangChain will fail too. Fix security groups, egress rules, NAT gateway routing, or proxy settings.

3) Streaming response never completes

A stream() call can appear to “timeout” if your consumer never reads the stream correctly or if you wait for final completion in an environment that kills long-lived connections.

const stream = await model.stream("Write a long answer");

// ❌ Broken if you never consume chunks properly
for await (const chunk of stream) {
  // must read chunks here
}

If your platform has short execution limits, use non-streaming invoke() for debugging first.

4) Embeddings/vector store initialization is too slow

Timeouts are common when loading large documents and embedding them synchronously during startup.

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

const embeddings = new OpenAIEmbeddings({
  apiKey: process.env.OPENAI_API_KEY,
  timeout: 60_000,
});

If you’re embedding thousands of chunks during request time, move that work to background jobs. Don’t do ingestion in the API request path.

How to Debug It

  1. Check which LangChain class is failing

    • Look at whether the error comes from ChatOpenAI, OpenAIEmbeddings, AzureChatOpenAI, a vector store like PineconeStore, or a custom tool.
    • The fix depends on whether you’re calling an LLM endpoint or a storage backend.
  2. Log the exact error object

    • Don’t just log err.message.
    • Print stack traces and nested causes.
try {
  await chain.invoke({ question });
} catch (err) {
  console.error("LangChain error:", err);
}

Look for messages like:

  • Error: Connection timed out
  • fetch failed
  • ETIMEDOUT
  • Request timed out after 30000ms
  • APIConnectionError / TimeoutError depending on the provider wrapper
  1. Test outside LangChain

    • Call the same endpoint with plain fetch or curl.
    • If plain HTTP fails too, it’s not LangChain.
  2. Reduce variables

    • Remove streaming.
    • Remove tools.
    • Remove retrievers.
    • Call only one model method with a tiny prompt.

If this works:

await model.invoke("ping");

but this fails:

await chain.invoke({ question });

then your issue is likely in prompt formatting, tool calls, retrieval latency, or downstream service calls inside the chain.

Prevention

  • Create shared clients once at module scope and reuse them across requests.
  • Set explicit timeouts and retries on every external dependency:
new ChatOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  timeout: 30_000,
  maxRetries: 2,
});
  • Keep ingestion and embedding jobs out of synchronous request handlers.
  • Add network checks in CI/CD for any environment that runs LangChain code.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides