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-telemetryThis will:
- Set
CLAUDE_CODE_ENABLE_TELEMETRY=1environment variable - Create
.envfile with telemetry settings - Initialize token tracking directory (
.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 terminalAfter 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]- Claude Telemetry Integration: The system integrates with Claude's native OpenTelemetry support
- Process Wrapping: When telemetry is enabled, Claude commands are wrapped to capture output
- Token Extraction: Token usage is extracted from:
- Claude CLI output patterns
- Session JSONL files (if accessible)
/costcommand output
- Persistent Storage: Token data is stored in
.claude-flow/metrics/token-usage.json
claude-telemetry.js: Core telemetry wrapper moduletoken-tracker.js: Token tracking and cost calculationanalysis.js: Analysis commands and reporting
Monitor 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 3000Track 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:
- Agent type
- Command
- Session
- Time period
# Detailed breakdown
./claude-flow analysis token-usage --breakdown
# Agent-specific analysis
./claude-flow analysis token-usage --agent coordinator --cost-analysisYou 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- No sensitive data: Only token counts and costs are tracked
- Local storage: All data is stored locally in
.claude-flow/metrics/ - Opt-in: Telemetry must be explicitly enabled
- No external transmission: Data is never sent to external servers
- Enable telemetry once: Set it in your
.envfile for persistence - Monitor long tasks: Use
claude-monitorfor tasks > 5 minutes - Regular cost checks: Run
claude-costafter expensive operations - Batch operations: Group Claude calls to optimize token usage
- Use appropriate models: Choose Haiku for simple tasks, Opus for complex
| 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 agentFor issues or questions:
- GitHub Issues: https://github.com/ruvnet/claude-flow/issues
- Documentation: https://github.com/ruvnet/claude-flow/docs
Real token tracking in Claude Flow provides transparency into API usage and costs. By following this guide, you can:
- Track actual Claude API token consumption
- Monitor costs in real-time
- Optimize token usage patterns
- Make informed decisions about model selection
Remember: This tracks REAL usage, not simulated data!