How to Fix 'cold start latency during development' in CrewAI (TypeScript)
What this error means
cold start latency during development in CrewAI TypeScript usually means your agent is spending too long initializing before the first task runs. In practice, this shows up when you create heavy objects at module load time, rebuild the LLM client on every request, or let your dev server re-import the same files repeatedly.
You’ll typically see it during local development with hot reload, especially in Next.js, Vite, or ts-node watch mode.
The Most Common Cause
The #1 cause is doing expensive initialization at import time instead of inside a singleton or lazy factory. In CrewAI TypeScript, that usually means instantiating Crew, Agent, Task, and OpenAIChat directly in the file scope, so every refresh rebuilds everything.
Broken vs fixed pattern
| Broken | Fixed |
|---|---|
| Recreates clients on every import | Reuses a cached instance |
| Triggers cold start on hot reload | Initializes once per process |
| Harder to debug in dev | Predictable startup behavior |
// ❌ broken.ts
import { Crew, Agent, Task } from "@crewai/typescript";
import { OpenAIChat } from "@langchain/openai";
const llm = new OpenAIChat({
model: "gpt-4o-mini",
apiKey: process.env.OPENAI_API_KEY!,
});
const agent = new Agent({
role: "Support analyst",
goal: "Classify customer tickets",
backstory: "You work in banking operations",
llm,
});
const task = new Task({
description: "Classify this ticket",
agent,
});
export const crew = new Crew({
agents: [agent],
tasks: [task],
});
// Every import pays the initialization cost again.
// ✅ fixed.ts
import { Crew, Agent, Task } from "@crewai/typescript";
import { OpenAIChat } from "@langchain/openai";
let crewInstance: Crew | null = null;
function buildCrew() {
const llm = new OpenAIChat({
model: "gpt-4o-mini",
apiKey: process.env.OPENAI_API_KEY!,
});
const agent = new Agent({
role: "Support analyst",
goal: "Classify customer tickets",
backstory: "You work in banking operations",
llm,
});
const task = new Task({
description: "Classify this ticket",
agent,
});
return new Crew({
agents: [agent],
tasks: [task],
});
}
export function getCrew() {
if (!crewInstance) {
crewInstance = buildCrew();
}
return crewInstance;
}
If you’re running a dev server with file watching, this change alone often removes the latency spike.
Other Possible Causes
1. Recreating the LLM client per request
If your handler builds new OpenAIChat(...) every time, startup feels slow and token usage can spike under retries.
// bad
export async function POST() {
const llm = new OpenAIChat({ model: "gpt-4o-mini" });
return await runCrew(llm);
}
// good
const llm = new OpenAIChat({ model: "gpt-4o-mini" });
export async function POST() {
return await runCrew(llm);
}
2. Import cycles between agent files
CrewAI TypeScript codebases often split agents into multiple files. A circular import can delay module resolution and make cold start look worse than it is.
// agent-a.ts imports task-b.ts
// task-b.ts imports agent-a.ts
Fix it by moving shared config into a third file:
// shared-crewai-config.ts
export const crewConfig = {
model: "gpt-4o-mini",
};
3. Expensive sync work before crew.kickoff()
Reading large JSON files, loading prompt templates from disk, or parsing schema files at module scope adds latency before the first task starts.
// bad
const policyText = fs.readFileSync("./policies/huge-policy.txt", "utf8");
// good
async function loadPolicyText() {
return await fs.promises.readFile("./policies/huge-policy.txt", "utf8");
}
4. Dev server double-invoking modules
Frameworks like Next.js can execute code twice in development. If you see logs like:
- •
Initializing Crew... - •
Initializing Crew...
then your code is being evaluated more than once.
Guard it with a cache on globalThis:
declare global {
// eslint-disable-next-line no-var
var crewCache: ReturnType<typeof buildCrew> | undefined;
}
export function getCrew() {
globalThis.crewCache ??= buildCrew();
return globalThis.crewCache;
}
How to Debug It
- •
Add timestamps around each initialization step
console.time("llm"); const llm = new OpenAIChat({ model: "gpt-4o-mini" }); console.timeEnd("llm");If the delay is before
kickoff, you have an initialization problem. - •
Check whether modules are being imported twice Add a top-level log:
console.log("loaded crew module");If it prints multiple times on one request, your dev server or imports are reloading too often.
- •
Isolate the slow piece Temporarily comment out:
- •tool loading
- •file reads
- •memory providers
- •vector store setup
Then re-enable one by one until the latency returns.
- •
Inspect stack traces for repeated construction If you see repeated calls to
new Agent(...),new Task(...), ornew Crew(...), move those into a cached factory.
Prevention
- •Build CrewAI objects behind a singleton factory like
getCrew()instead of exporting initialized instances. - •Keep LLM clients and tools outside request handlers unless they depend on request-specific state.
- •Avoid sync file I/O and heavy parsing at module scope.
- •In dev mode, assume hot reload will re-run imports and design for idempotent initialization.
If you’re seeing cold start latency during development in CrewAI TypeScript, start with module scope first. That’s where most of these bugs live.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
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