CrewAI Tutorial (TypeScript): optimizing token usage for intermediate developers
This tutorial shows you how to reduce token spend in a CrewAI TypeScript workflow without breaking agent quality. You’ll wire in tighter prompts, smaller context windows, and cheaper task routing so your crew stops paying for tokens it doesn’t need.
What You'll Need
- •Node.js 18+ and npm
- •A TypeScript project with
tsconfig.json - •
crewaiinstalled in your project - •
dotenvfor environment variables - •An OpenAI API key in
.env - •Basic familiarity with CrewAI agents, tasks, and crews
- •A code editor and terminal
Step-by-Step
- •Start by installing the packages you need and setting up your environment variables. Keep the model choice explicit so you can control cost from the first line of code.
npm install crewai dotenv
npm install -D typescript ts-node @types/node
OPENAI_API_KEY=your_api_key_here
- •Create a small config layer that centralizes model selection and low-token defaults. The main trick is to use cheaper models for routine work and reserve stronger models only when needed.
import "dotenv/config";
export const config = {
model: process.env.CREWAI_MODEL ?? "gpt-4o-mini",
maxIterations: 2,
temperature: 0.2,
};
if (!process.env.OPENAI_API_KEY) {
throw new Error("OPENAI_API_KEY is missing");
}
- •Build agents with narrow responsibilities and short instructions. Long role descriptions burn tokens every time the agent is invoked, so keep them tight and push details into task-specific context only when needed.
import { Agent } from "crewai";
import { config } from "./config";
export const analyst = new Agent({
role: "Policy analyst",
goal: "Summarize policy text into concise risk notes",
backstory: "You extract only material facts for downstream review.",
verbose: false,
llm: config.model,
});
export const reviewer = new Agent({
role: "Quality reviewer",
goal: "Check summaries for missing risks",
backstory: "You verify completeness with minimal commentary.",
verbose: false,
llm: config.model,
});
- •Define tasks with strict output formats and no extra prose. This is where most token waste happens, because agents love to ramble unless you constrain the response shape.
import { Task } from "crewai";
import { analyst, reviewer } from "./agents";
export const summarizeTask = new Task({
description:
"Summarize the following policy text into exactly 5 bullet points focused on risk, exclusions, and obligations.\n\nInput:\n{policyText}",
expectedOutput:
"Exactly 5 bullets. No intro. No conclusion. No markdown headings.",
agent: analyst,
});
export const reviewTask = new Task({
description:
"Review the summary for missing risks or unclear language. Return only a JSON object with keys issues and approved.",
expectedOutput:
'{"issues":["..."],"approved":true}',
agent: reviewer,
});
- •Assemble a crew that runs sequentially and keeps context tight between steps. Sequential execution is usually cheaper than broad delegation because each agent sees less irrelevant history.
import { Crew, Process } from "crewai";
import { summarizeTask, reviewTask } from "./tasks";
export const crew = new Crew({
agents: [summarizeTask.agent, reviewTask.agent],
tasks: [summarizeTask, reviewTask],
process: Process.sequential,
verbose: false,
});
- •Execute the crew with a compact input payload and print only the final result. Avoid passing large raw documents if you can pre-trim them first; token usage is driven by what you send in.
import { crew } from "./crew";
async function main() {
const policyText = [
"Coverage excludes pre-existing conditions.",
"Claims must be filed within 30 days.",
"Fraudulent claims void coverage immediately.",
"Deductible applies per incident.",
"Emergency care requires prior notification when feasible."
].join(" ");
const result = await crew.kickoff({
inputs: { policyText },
});
console.log(result);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
Testing It
Run the script with npx ts-node src/index.ts or compile it first if your project uses a build step. Check that the output stays short, structured, and consistent across runs.
If you see long-winded answers, tighten expectedOutput, reduce maxIterations, or shorten the task description further. Also inspect whether you are sending too much source text into inputs; that is often the real token leak.
A good sanity check is to compare token usage before and after these changes using your provider dashboard. You should see lower input tokens from shorter prompts and lower output tokens from stricter formatting.
Next Steps
- •Add a preprocessing step that chunks long documents before they reach CrewAI
- •Learn how to route simple tasks to cheaper models and reserve stronger models for escalation
- •Instrument prompt size and completion length so token regressions show up in CI
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