CrewAI Tutorial (Python): adding cost tracking for beginners

By Cyprian AaronsUpdated 2026-04-21
crewaiadding-cost-tracking-for-beginnerspython

This tutorial shows how to add basic cost tracking to a CrewAI Python project so you can see how much each run costs and where the spend is coming from. You need this when you start moving beyond local experiments and want a simple way to monitor LLM usage before it turns into an uncontrolled bill.

What You'll Need

  • Python 3.10 or newer
  • A CrewAI project installed with:
    • crewai
    • openai
    • python-dotenv
  • An OpenAI API key
  • A .env file for storing secrets
  • A terminal and a text editor

Install the packages first:

pip install crewai openai python-dotenv

Step-by-Step

  1. Start with a minimal CrewAI setup that uses one agent, one task, and one crew. This gives you a clean baseline before adding any tracking logic.
from crewai import Agent, Task, Crew, Process

researcher = Agent(
    role="Researcher",
    goal="Find concise facts about CrewAI cost tracking",
    backstory="You are careful with API usage and explain things clearly.",
    verbose=True,
)

task = Task(
    description="Explain what cost tracking means in a CrewAI project.",
    expected_output="A short explanation with practical value.",
    agent=researcher,
)

crew = Crew(
    agents=[researcher],
    tasks=[task],
    process=Process.sequential,
)
  1. Load your API key from .env so the script stays deployable and does not hardcode credentials. This also keeps the example close to what you would run in a real service.
import os
from dotenv import load_dotenv

load_dotenv()

api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise RuntimeError("OPENAI_API_KEY is missing from your environment")

os.environ["OPENAI_API_KEY"] = api_key
  1. Add a small cost tracker wrapper around the crew execution. The idea is simple: capture token usage from the result when available, then estimate cost using your model pricing.
from dataclasses import dataclass

@dataclass
class CostReport:
    prompt_tokens: int = 0
    completion_tokens: int = 0
    total_tokens: int = 0
    estimated_cost_usd: float = 0.0

def estimate_cost(prompt_tokens: int, completion_tokens: int) -> float:
    # Example pricing for gpt-4o-mini:
    # input: $0.15 / 1M tokens, output: $0.60 / 1M tokens
    input_cost = (prompt_tokens / 1_000_000) * 0.15
    output_cost = (completion_tokens / 1_000_000) * 0.60
    return round(input_cost + output_cost, 6)
  1. Run the crew and extract token usage from the result object. Different versions of CrewAI can expose usage slightly differently, so this code checks common shapes without breaking execution.
result = crew.kickoff()

usage = getattr(result, "token_usage", None) or {}
prompt_tokens = int(usage.get("prompt_tokens", 0))
completion_tokens = int(usage.get("completion_tokens", 0))
total_tokens = int(usage.get("total_tokens", prompt_tokens + completion_tokens))

report = CostReport(
    prompt_tokens=prompt_tokens,
    completion_tokens=completion_tokens,
    total_tokens=total_tokens,
    estimated_cost_usd=estimate_cost(prompt_tokens, completion_tokens),
)

print("\n--- RESULT ---")
print(result)
print("\n--- COST REPORT ---")
print(report)
  1. If you want something more reliable than ad hoc printing, persist the cost report to disk after every run. That gives you an audit trail you can aggregate later in a spreadsheet or database.
import json
from datetime import datetime

payload = {
    "timestamp": datetime.utcnow().isoformat(),
    "prompt_tokens": report.prompt_tokens,
    "completion_tokens": report.completion_tokens,
    "total_tokens": report.total_tokens,
    "estimated_cost_usd": report.estimated_cost_usd,
}

with open("crew_cost_log.jsonl", "a", encoding="utf-8") as f:
    f.write(json.dumps(payload) + "\n")

print("Saved cost entry to crew_cost_log.jsonl")

Testing It

Run the script once and confirm that you get both the task output and a cost report printed at the end. If token_usage comes back empty, your installed CrewAI version may expose usage in a different field, so inspect dir(result) or result.__dict__ to see what is available.

Then open crew_cost_log.jsonl and verify that each run appends one line of JSON with timestamps and token counts. If you want to validate the math, compare the estimated cost against your provider’s pricing page using a known token count.

For production use, test with multiple tasks and agents because multi-step crews will usually consume more tokens than a single-task example. That is where cost tracking starts paying for itself.

Next Steps

  • Add per-agent or per-task breakdowns instead of only crew-level totals.
  • Store cost data in Postgres or SQLite so you can query trends by date or workflow.
  • Wrap this into middleware so every CrewAI job in your app gets tracked automatically.

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