How to Fix 'context length exceeded in production' in CrewAI (TypeScript)
When CrewAI throws context length exceeded in production, it means the model received more tokens than the context window can hold. In TypeScript apps, this usually shows up after a few tool calls, long task histories, or when you keep appending raw documents into prompts.
This is not a CrewAI bug in most cases. It’s almost always a prompt construction problem, memory growth problem, or model mismatch problem.
The Most Common Cause
The #1 cause is feeding too much accumulated text into a task prompt or agent memory. In CrewAI TypeScript projects, this usually happens when you concatenate prior outputs, tool results, and user messages without trimming them.
Here’s the broken pattern:
// Broken: keeps growing the prompt forever
const researchAgent = new Agent({
role: "Research Analyst",
goal: "Summarize customer complaints",
backstory: "You analyze support tickets.",
llm: "gpt-4o",
});
let history = "";
for (const ticket of tickets) {
history += `\nTicket: ${ticket.subject}\nBody: ${ticket.body}\n`;
}
const task = new Task({
description: `
Analyze these tickets and produce trends.
${history}
`,
agent: researchAgent,
});
await crew.kickoff();
And here’s the fixed version:
// Fixed: trim input before building the task prompt
const MAX_TICKETS = 10;
const MAX_CHARS_PER_TICKET = 1500;
const compactTickets = tickets.slice(-MAX_TICKETS).map((ticket) => ({
subject: ticket.subject.slice(0, 120),
body: ticket.body.slice(0, MAX_CHARS_PER_TICKET),
}));
const task = new Task({
description: `
Analyze the latest ${compactTickets.length} tickets and produce trends.
Focus on repeated issues only.
Tickets:
${compactTickets
.map(
(t) => `- Subject: ${t.subject}\n Body: ${t.body}`
)
.join("\n\n")}
`,
agent: researchAgent,
});
The rule is simple: never let unbounded data flow directly into Task.description, agent memory, or tool outputs that get re-injected into the next step.
Other Possible Causes
| Cause | What it looks like | Fix |
|---|---|---|
| Long-running memory | Each step adds more conversation state | Trim or summarize memory |
| Tool returns huge payloads | A search/PDF/DB tool returns full documents | Return excerpts, not full blobs |
| Wrong model choice | Small-context model used for large workflows | Switch to a larger context model |
| Recursive agent loops | Agent keeps calling tools with same content | Add stop conditions and limits |
1. Memory is growing across steps
If you use conversation memory or pass prior outputs back into later tasks, the token count grows fast.
// Risky
const agent = new Agent({
role: "Support Ops",
goal: "Resolve issues",
llm: "gpt-4o-mini",
memory: true,
});
Fix it by summarizing or truncating stored state before each run.
// Better
const agent = new Agent({
role: "Support Ops",
goal: "Resolve issues",
llm: "gpt-4o-mini",
memory: false,
});
If you need memory, store only structured summaries outside the prompt.
2. A tool is returning too much data
This is common with database tools, file readers, and web scrapers. The tool output gets injected into the LLM context and blows past the limit.
// Broken tool output
return JSON.stringify(rows); // thousands of rows
Trim it:
// Fixed tool output
return JSON.stringify(rows.slice(0, 20).map((row) => ({
id: row.id,
title: row.title,
status: row.status,
})));
3. You are using a small-context model
Some models are fine for short prompts but fail once your workflow includes multiple tasks and tool calls.
const agent = new Agent({
role: "Analyst",
goal: "Review policy docs",
llm: "gpt-4o-mini", // may be too small for large inputs
});
Use a larger-context model for heavy workflows:
const agent = new Agent({
role: "Analyst",
goal: "Review policy docs",
llm: "gpt-4o",
});
If your provider supports it, choose a model with a larger context window for document-heavy jobs.
4. Recursive delegation or repeated retries
CrewAI agents can loop if tasks keep delegating or retrying with the same oversized payload. The error often appears after several successful calls because each retry adds more context.
Look for settings like this:
const crew = new Crew({
agents,
tasks,
});
Add explicit limits in your orchestration layer:
let attempts = 0;
while (attempts < 3) {
attempts += 1;
try {
await crew.kickoff();
break;
} catch (err) {
if (String(err).includes("context length exceeded")) throw err;
}
}
How to Debug It
- •
Log prompt size before kickoff
- •Print
Task.description.length, tool payload sizes, and any concatenated history. - •If one field is huge, that’s your first suspect.
- •Print
- •
Inspect the exact failing step
- •The error usually happens after a specific tool call or task handoff.
- •Check whether
Tooloutput or previousTaskoutput was appended back into context.
- •
Disable memory and rerun
- •Set
memory: falseon the agent. - •If the error disappears, your issue is accumulated conversation state.
- •Set
- •
Swap to a larger-context model
- •Temporarily move from
gpt-4o-minitogpt-4o. - •If it works, your pipeline is valid but too large for the current model window.
- •Temporarily move from
Prevention
- •
Keep all prompts bounded:
- •cap document length
- •slice arrays
- •summarize prior results before reuse
- •
Make tools return structured summaries, not raw dumps:
- •top N records only
- •extracted fields only
- •no full HTML/PDF/JSON blobs unless absolutely necessary
- •
Treat context as a budget:
| Input source | Safe pattern |
|---|---|
| User text | Truncate before prompt assembly |
| Tool output | Summarize before returning |
| Memory | Store summaries, not transcripts |
If you’re seeing Error: context length exceeded in CrewAI TypeScript production runs, start by looking at what got added to context in the last step. In practice, that’s where the bug lives almost every time.
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