How to Fix 'token limit exceeded during development' in CrewAI (TypeScript)
What the error means
token limit exceeded during development usually means one of your CrewAI agents tried to send too much text into the model context window. In TypeScript projects, this shows up when you pass raw documents, long chat history, or oversized tool output straight into an agent task.
You’ll typically hit it during local testing, especially after adding file loaders, memory, or a tool that dumps full JSON responses.
The Most Common Cause
The #1 cause is feeding an agent too much untrimmed input in a Task or tool result. In CrewAI, the model still has a hard context limit, so if your prompt includes a whole PDF, a giant API payload, or accumulated conversation history, you get a failure like:
- •
Error: token limit exceeded during development - •
Context length exceeded - •
OpenAIError: This model's maximum context length is ...
Here’s the broken pattern and the fixed pattern side by side.
| Broken | Fixed |
|---|---|
| ```ts | |
| import { Agent, Task } from "crewai"; |
const analyst = new Agent({ role: "Claims Analyst", goal: "Summarize the claim file", backstory: "You work on insurance claims.", });
const hugeClaimText = await fs.promises.readFile("claim.txt", "utf-8");
const task = new Task({ description: `Review this claim and identify risk:
${hugeClaimText}`,
agent: analyst,
});
|ts
import { Agent, Task } from "crewai";
const analyst = new Agent({ role: "Claims Analyst", goal: "Summarize the claim file", backstory: "You work on insurance claims.", });
const claimText = await fs.promises.readFile("claim.txt", "utf-8"); const trimmedClaimText = claimText.slice(0, 12000); // keep within budget
const task = new Task({ description: `Review this claim and identify risk. Only use the excerpt below:
${trimmedClaimText}`, agent: analyst, });
If you need more than a slice, chunk the document and process it in stages instead of asking one agent call to digest everything at once.
## Other Possible Causes
### 1) Tool output is too large
A common failure mode is a tool returning full records instead of a compact summary.
```ts
// Bad
const searchTool = async () => {
const result = await db.query("SELECT * FROM claims");
return JSON.stringify(result);
};
// Better
const searchTool = async () => {
const result = await db.query("SELECT id, status, lossAmount FROM claims LIMIT 20");
return JSON.stringify(result);
};
If your tool returns pages of nested objects, the agent will burn tokens just reading its own input.
2) Memory is accumulating too much history
If you keep appending every message to memory without pruning, token usage grows on each turn.
// Bad
crewMemory.push({ role: "user", content });
crewMemory.push({ role: "assistant", content: response });
// Better
crewMemory = crewMemory.slice(-6); // keep only recent turns
For long-running workflows, store durable state outside the prompt and only inject what the next step actually needs.
3) Your system prompt is bloated
A huge backstory, policy block, or instruction dump can push you over the edge before user input even starts.
// Bad
new Agent({
role: "Underwriter",
goal: "Assess risk",
backstory: longCompanyHandbook + longCompliancePolicy + longSOP,
});
// Better
new Agent({
role: "Underwriter",
goal: "Assess risk using underwriting guidelines.",
backstory: "You assess insurance risk using approved guidelines.",
});
Keep instructions short and move reference material into retrieval or tools.
4) You are passing raw logs or stack traces into tasks
This happens when developers forward entire error objects into an agent for analysis.
// Bad
description: `Investigate this issue:\n${JSON.stringify(err, null, 2)}`
// Better
description: `Investigate this issue:
message=${err.message}
code=${err.code ?? "unknown"}`
Agents do better with structured summaries than with megabytes of debug noise.
How to Debug It
- •
Measure prompt size before calling the agent
- •Log the lengths of your task description, tool output, and memory buffer.
- •If one field is enormous, that’s your culprit.
- •
Remove inputs one by one
- •Start with a minimal
Task.description. - •Add back document text, then tool output, then memory.
- •The first addition that breaks it is where to fix.
- •Start with a minimal
- •
Check every tool return value
- •Look for
JSON.stringify(largeObject)or unbounded SQL/API responses. - •Tools should return summaries, IDs, or top-N results.
- •Look for
- •
Inspect model limits
- •Some models used by CrewAI have smaller context windows than you expect.
- •If you switched providers recently, confirm the exact token budget for that model.
Prevention
- •Keep prompts short and push large data through chunking or retrieval.
- •Cap tool outputs with pagination, filtering, and field selection.
- •Treat memory as a summary store, not an unlimited transcript dump.
If you’re building CrewAI agents in TypeScript for production workflows like claims triage or underwriting support, assume every string can become a token bill. Control what enters the prompt window and this error stops showing up.
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