v2/docs/guides/token-tracking-guide.md
Claude Flow now includes REAL token tracking capabilities that capture actual Claude API usage, not simulated data. This guide shows how to enable and use token tracking with the Claude Code CLI.
First, enable telemetry for token tracking:
./claude-flow analysis setup-telemetry
This will:
CLAUDE_CODE_ENABLE_TELEMETRY=1 environment variable.env file with telemetry settings.claude-flow/metrics/)Use the --claude flag with telemetry enabled:
# Option 1: Set environment variable inline
CLAUDE_CODE_ENABLE_TELEMETRY=1 ./claude-flow swarm "your task" --claude
# Option 2: Export environment variable
export CLAUDE_CODE_ENABLE_TELEMETRY=1
./claude-flow swarm "your task" --claude
# Option 3: Use wrapper directly
./claude-flow analysis claude-monitor # Start monitoring in one terminal
./claude-flow swarm "your task" --claude # Run Claude in another terminal
After running Claude commands:
# View comprehensive token usage report
./claude-flow analysis token-usage --breakdown --cost-analysis
# Get current session cost
./claude-flow analysis claude-cost
# Monitor session in real-time
./claude-flow analysis claude-monitor [session-id]
/cost command output.claude-flow/metrics/token-usage.jsonclaude-telemetry.js: Core telemetry wrapper moduletoken-tracker.js: Token tracking and cost calculationanalysis.js: Analysis commands and reportingMonitor Claude sessions as they run:
# Monitor with default 5-second interval
./claude-flow analysis claude-monitor
# Monitor with custom interval (3 seconds)
./claude-flow analysis claude-monitor current --interval 3000
Track costs based on Claude 3 pricing:
| Model | Input (per 1M tokens) | Output (per 1M tokens) |
|---|---|---|
| Opus | $15.00 | $75.00 |
| Sonnet | $3.00 | $15.00 |
| Haiku | $0.25 | $1.25 |
View token usage by:
# Detailed breakdown
./claude-flow analysis token-usage --breakdown
# Agent-specific analysis
./claude-flow analysis token-usage --agent coordinator --cost-analysis
You can also use the telemetry wrapper programmatically:
import { runClaudeWithTelemetry } from './claude-telemetry.js';
const result = await runClaudeWithTelemetry(
['chat', 'Hello, Claude!'],
{
sessionId: 'my-session-123',
agentType: 'custom-agent'
}
);
Monitor specific sessions:
import { monitorClaudeSession } from './claude-telemetry.js';
const stopMonitor = await monitorClaudeSession('session-id', 5000);
// Stop monitoring when done
stopMonitor();
Verify telemetry is enabled:
echo $CLAUDE_CODE_ENABLE_TELEMETRY # Should output: 1
Check token usage file exists:
cat .claude-flow/metrics/token-usage.json
Ensure Claude is installed:
which claude # Should show Claude path
Clear existing metrics:
rm -rf .claude-flow/metrics/token-usage.json
Re-enable telemetry:
./claude-flow analysis setup-telemetry
Run with explicit telemetry:
CLAUDE_CODE_ENABLE_TELEMETRY=1 ./claude-flow swarm "test" --claude
For automated environments:
# GitHub Actions example
env:
CLAUDE_CODE_ENABLE_TELEMETRY: '1'
steps:
- name: Setup telemetry
run: ./claude-flow analysis setup-telemetry
- name: Run Claude task
run: ./claude-flow swarm "${{ inputs.task }}" --claude
- name: Report costs
run: ./claude-flow analysis claude-cost
.claude-flow/metrics/.env file for persistenceclaude-monitor for tasks > 5 minutesclaude-cost after expensive operations| Command | Description |
|---|---|
analysis setup-telemetry | Configure token tracking |
analysis token-usage | View token usage report |
analysis claude-monitor | Monitor session in real-time |
analysis claude-cost | Get current session cost |
| Variable | Description | Default |
|---|---|---|
CLAUDE_CODE_ENABLE_TELEMETRY | Enable token tracking | 0 |
OTEL_METRICS_EXPORTER | OpenTelemetry exporter | console |
OTEL_LOGS_EXPORTER | Log exporter type | console |
| File | Purpose |
|---|---|
.claude-flow/metrics/token-usage.json | Token usage data |
.claude-flow/metrics/sessions/*.json | Session metrics |
.env | Environment configuration |
# 1. Setup
./claude-flow analysis setup-telemetry
# 2. Run task with tracking
export CLAUDE_CODE_ENABLE_TELEMETRY=1
./claude-flow swarm "Create a REST API with authentication" --claude
# 3. Check usage
./claude-flow analysis token-usage --breakdown --cost-analysis
# 4. Monitor next session
./claude-flow analysis claude-monitor & # Run in background
./claude-flow swarm "Add tests to the API" --claude
# 5. Final cost report
./claude-flow analysis claude-cost
# Analyze token usage patterns
./claude-flow analysis token-usage --breakdown
# Identify high-consumption agents
./claude-flow analysis token-usage --agent coordinator
# Get optimization suggestions
./claude-flow analysis bottleneck-detect --scope agent
For issues or questions:
Real token tracking in Claude Flow provides transparency into API usage and costs. By following this guide, you can:
Remember: This tracks REAL usage, not simulated data!