CrewAI
Track every CrewAI crew run with cost, latency, token usage, and tool call data. Instantiate AgentMetricsListener once and it registers globally, covering all crew kickoffs in the process.
Requirements
- Python 3.10 or later
- CrewAI 0.80 or later
1. Install
pip install agentmetrics-crewai
2. Register the listener
Instantiate AgentMetricsListener once before any crew kickoff. It auto-registers with CrewAI's event bus. No code changes to your crew needed.
import os
from agentmetrics_crewai import AgentMetricsListener
AgentMetricsListener(
api_key=os.environ["AGENTMETRICS_API_KEY"],
agent_id="my-crew",
base_url="http://localhost:8099",
)
result = MyCrew().kickoff()
The listener picks up the crew name automatically from CrewAI's kickoff event. The agent_id parameter is used as a fallback if the crew name is not set.
3. Flush before exit (optional)
For short-lived scripts, call flush() on the listener instance to wait for all in-flight requests to complete before the process exits.
listener = AgentMetricsListener(
api_key=os.environ["AGENTMETRICS_API_KEY"],
base_url="http://localhost:8099",
)
result = MyCrew().kickoff()
listener.flush()
What gets tracked
- Duration from crew kickoff start to kickoff complete or failed
- Success or failure
- Input and output token counts per LLM call
- Cost (estimated from model pricing tables)
- Tool call count, tool names, and tool errors
- Model name
Cache token breakdown
CrewAI does not expose cache token breakdown in its event bus. Cache read and write token counts are not available for CrewAI runs.
Configuration
| Parameter | Default | Description |
|---|---|---|
api_key | required | Your AgentMetrics API key |
agent_id | "crewai-agent" | Fallback label if crew name is not set |
base_url | "http://localhost:8099" | AgentMetrics backend URL |