CrewAI Tutorial (TypeScript): optimizing token usage for beginners

By Cyprian AaronsUpdated 2026-04-21
crewaioptimizing-token-usage-for-beginnerstypescript

This tutorial shows you how to build a CrewAI workflow in TypeScript that uses fewer tokens without breaking the agent loop. You need this when your agents are burning context on repeated prompts, oversized tool outputs, or unnecessary back-and-forth between tasks.

What You'll Need

  • Node.js 18+
  • A TypeScript project with tsx or ts-node
  • CrewAI JS/TS package installed
  • An OpenAI API key
  • Basic familiarity with Agent, Task, and Crew
  • A small input dataset or query you can test repeatedly

Step-by-Step

  1. Start by installing the packages and setting up your environment variables. Keep the stack minimal so you can measure token usage without extra noise from framework code.
npm init -y
npm install crewai openai dotenv
npm install -D typescript tsx @types/node
# .env
OPENAI_API_KEY=your_api_key_here
  1. Create a small CrewAI setup with one researcher agent and one writer agent. The main token-saving move here is to keep role instructions short and make the task output narrow.
import "dotenv/config";
import { Agent, Task, Crew, Process } from "crewai";

const researcher = new Agent({
  name: "Researcher",
  role: "Researcher",
  goal: "Extract only the facts needed for the final answer",
  backstory: "You are precise and avoid unnecessary detail.",
  verbose: false,
});

const writer = new Agent({
  name: "Writer",
  role: "Writer",
  goal: "Turn concise facts into a short final response",
  backstory: "You write directly and do not add filler.",
  verbose: false,
});
  1. Add a compact task chain and explicitly constrain the output format. This reduces token waste because the model does not have to infer structure or rewrite long responses.
const researchTask = new Task({
  description:
    "Given this user request: {input}, return only 5 bullet points with the most relevant facts.",
  expectedOutput: "Exactly 5 bullets, no intro, no conclusion.",
  agent: researcher,
});

const writeTask = new Task({
  description:
    "Use the research bullets to answer the user in under 120 words.",
  expectedOutput: "A short answer under 120 words.",
  agent: writer,
});
  1. Pass only the minimum context into each task and run the crew sequentially. Sequential execution keeps each step focused and avoids sending unnecessary intermediate reasoning across agents.
const crew = new Crew({
  agents: [researcher, writer],
  tasks: [researchTask, writeTask],
  process: Process.sequential,
});

async function main() {
  const result = await crew.kickoff({
    input: "Explain how to reduce claims processing time for a small insurance team.",
  });

  console.log(result);
}

main().catch(console.error);
  1. Add guardrails around inputs before they reach the crew. The biggest beginner mistake is feeding full documents, logs, or chat history when a trimmed summary would do.
function trimInput(text: string): string {
  return text
    .split("\n")
    .map((line) => line.trim())
    .filter(Boolean)
    .slice(0, 8)
    .join("\n");
}

const rawInput = `
We need to improve claims processing.
Current issues:
- Too much manual review
- Duplicate document checks
- Long email chains
- Missing templates
- No clear escalation path
`;

const cleanedInput = trimInput(rawInput);
console.log(cleanedInput);
  1. If you want even tighter token control, reduce tool chatter and keep outputs structured. Use short system-style instructions, avoid asking for long explanations, and prefer summaries over raw dumps.
const auditTask = new Task({
  description:
    "Review this output for verbosity. If it is too long, rewrite it as a tighter version.",
  expectedOutput: "A concise rewritten version under 80 words.",
  agent: writer,
});

async function runWithAudit() {
  const auditCrew = new Crew({
    agents: [researcher, writer],
    tasks: [researchTask, writeTask, auditTask],
    process: Process.sequential,
  });

  const result = await auditCrew.kickoff({
    input: cleanedInput,
  });

  console.log(result);
}

runWithAudit().catch(console.error);

Testing It

Run the script with tsx and compare the output length across different inputs. Start with a short prompt, then try a longer one that includes extra background; if your trimming works, the final answer should stay compact.

Watch for three signs that token usage is under control:

  • Tasks return shorter outputs without losing relevance
  • The writer does not repeat the researcher’s content verbatim
  • Longer raw inputs do not cause much larger final responses

If you want hard numbers, inspect usage in your model provider dashboard after each run. In practice, your biggest savings come from shorter task descriptions, smaller inputs, and strict output limits.

Next Steps

  • Add a pre-processing layer that chunks documents before sending them to CrewAI
  • Learn how to use structured outputs so downstream tasks consume JSON instead of prose
  • Profile prompts over time and create a prompt budget per task

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