mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-24 06:06:02 +09:00
- Reorganize v3/ into implementation/ subfolders: - planning/, architecture/, migration/, integration/ - security/, adrs/, research/, swarm-plans/ - Add 15-agent concurrent swarm implementation plans: - SWARM-OVERVIEW.md: Hierarchical mesh topology, 4 phases - AGENT-SPECIFICATIONS.md: 15 agent configs with capabilities - TDD-LONDON-SCHOOL-PLAN.md: Mock-first TDD methodology - BENCHMARK-OPTIMIZATION.md: Performance targets (2.49x-7.47x) - DEPLOYMENT-PLAN.md: Release strategy, CI/CD, rollback - GITHUB-ISSUE-TRACKING.md: Issue workflow with agent replies - Update .claude/settings.json with proper hooks format - Add bash aliases for claude --dangerously-skip-permissions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
589 lines
25 KiB
Python
589 lines
25 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Real token tracking and consumption analysis for claude-flow benchmarks.
|
|
|
|
This example demonstrates:
|
|
- Accurate token consumption tracking
|
|
- Cost analysis and optimization
|
|
- Token efficiency metrics
|
|
- Production cost estimation
|
|
"""
|
|
|
|
import subprocess
|
|
import sys
|
|
import json
|
|
import time
|
|
import re
|
|
from pathlib import Path
|
|
from dataclasses import dataclass, asdict
|
|
from typing import List, Dict, Any, Optional, Tuple
|
|
from collections import defaultdict
|
|
|
|
@dataclass
|
|
class TokenMetrics:
|
|
"""Token consumption metrics."""
|
|
benchmark_id: str
|
|
timestamp: float
|
|
input_tokens: int
|
|
output_tokens: int
|
|
total_tokens: int
|
|
estimated_cost_usd: float
|
|
tokens_per_second: float
|
|
efficiency_score: float
|
|
task_complexity: str
|
|
methodology: str
|
|
coordination_mode: str
|
|
agent_count: int
|
|
|
|
@dataclass
|
|
class TokenAnalysis:
|
|
"""Token analysis results."""
|
|
total_benchmarks: int
|
|
total_tokens: int
|
|
total_cost_usd: float
|
|
average_tokens_per_benchmark: float
|
|
average_cost_per_benchmark: float
|
|
most_efficient_methodology: str
|
|
most_efficient_coordination: str
|
|
optimal_agent_count: int
|
|
cost_breakdown: Dict[str, float]
|
|
efficiency_insights: List[str]
|
|
|
|
class RealTokenTracker:
|
|
"""Real token tracking and analysis system."""
|
|
|
|
def __init__(self):
|
|
self.token_history: List[TokenMetrics] = []
|
|
|
|
# Current token pricing (approximate, update as needed)
|
|
self.token_pricing = {
|
|
"input_token_cost": 0.000003, # $3 per 1M tokens
|
|
"output_token_cost": 0.000015, # $15 per 1M tokens
|
|
}
|
|
|
|
self.claude_flow_path = "/workspaces/claude-code-flow"
|
|
|
|
def create_token_tracking_benchmarks(self) -> List[Dict[str, Any]]:
|
|
"""Create benchmarks specifically for token tracking."""
|
|
benchmarks = [
|
|
{
|
|
"id": "simple_function_swarm",
|
|
"methodology": "swarm",
|
|
"task": "Create a simple calculator function with basic operations",
|
|
"strategy": "development",
|
|
"coordination": "hierarchical",
|
|
"agents": 3,
|
|
"complexity": "simple",
|
|
"expected_tokens": 800
|
|
},
|
|
{
|
|
"id": "api_development_swarm",
|
|
"methodology": "swarm",
|
|
"task": "Build a complete REST API with authentication, CRUD operations, and validation",
|
|
"strategy": "development",
|
|
"coordination": "hierarchical",
|
|
"agents": 5,
|
|
"complexity": "medium",
|
|
"expected_tokens": 2500
|
|
},
|
|
{
|
|
"id": "microservices_swarm",
|
|
"methodology": "swarm",
|
|
"task": "Design and implement a complete microservices architecture with service discovery, load balancing, and monitoring",
|
|
"strategy": "development",
|
|
"coordination": "mesh",
|
|
"agents": 7,
|
|
"complexity": "complex",
|
|
"expected_tokens": 5000
|
|
},
|
|
{
|
|
"id": "collective_analysis_hive",
|
|
"methodology": "hive-mind",
|
|
"task": "Analyze codebase for security vulnerabilities and performance issues",
|
|
"strategy": "collective",
|
|
"coordination": "mesh",
|
|
"agents": 5,
|
|
"complexity": "medium",
|
|
"expected_tokens": 3000
|
|
},
|
|
{
|
|
"id": "consensus_architecture_hive",
|
|
"methodology": "hive-mind",
|
|
"task": "Reach consensus on optimal architecture for distributed system",
|
|
"strategy": "consensus",
|
|
"coordination": "distributed",
|
|
"agents": 6,
|
|
"complexity": "complex",
|
|
"expected_tokens": 4500
|
|
},
|
|
{
|
|
"id": "tdd_development_sparc",
|
|
"methodology": "sparc",
|
|
"task": "Implement user management system using TDD methodology",
|
|
"strategy": "tdd",
|
|
"coordination": "hierarchical",
|
|
"agents": 4,
|
|
"complexity": "medium",
|
|
"expected_tokens": 2800
|
|
},
|
|
{
|
|
"id": "architecture_design_sparc",
|
|
"methodology": "sparc",
|
|
"task": "Design comprehensive e-commerce platform architecture with specifications",
|
|
"strategy": "architecture",
|
|
"coordination": "hierarchical",
|
|
"agents": 5,
|
|
"complexity": "complex",
|
|
"expected_tokens": 4200
|
|
}
|
|
]
|
|
return benchmarks
|
|
|
|
def execute_token_tracking_benchmark(self, benchmark_config: Dict[str, Any]) -> TokenMetrics:
|
|
"""Execute benchmark with comprehensive token tracking."""
|
|
print(f"🔍 Token tracking: {benchmark_config['id']}")
|
|
print(f" 📋 Task: {benchmark_config['task'][:50]}...")
|
|
print(f" 🎯 Expected tokens: {benchmark_config['expected_tokens']:,}")
|
|
|
|
start_time = time.time()
|
|
|
|
try:
|
|
# Build command based on methodology
|
|
cmd = self._build_tracking_command(benchmark_config)
|
|
|
|
# Execute with token tracking
|
|
result = subprocess.run(
|
|
cmd,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=300,
|
|
cwd=self.claude_flow_path
|
|
)
|
|
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
|
|
# Extract token information from output
|
|
token_data = self._extract_token_data(result.stdout, result.stderr)
|
|
|
|
# Create token metrics
|
|
metrics = self._create_token_metrics(
|
|
benchmark_config, start_time, execution_time, token_data, result.returncode == 0
|
|
)
|
|
|
|
# Display results
|
|
status = "✅" if result.returncode == 0 else "❌"
|
|
print(f" {status} Tokens: {metrics.total_tokens:,} (${metrics.estimated_cost_usd:.4f})")
|
|
print(f" ⚡ Efficiency: {metrics.efficiency_score:.1f}/100")
|
|
print(f" 💰 Cost/token: ${metrics.estimated_cost_usd/metrics.total_tokens:.6f}" if metrics.total_tokens > 0 else " 💰 Cost/token: N/A")
|
|
|
|
return metrics
|
|
|
|
except subprocess.TimeoutExpired:
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
print(f" ⏰ Timeout after {execution_time:.1f}s")
|
|
|
|
return self._create_timeout_token_metrics(benchmark_config, start_time, execution_time)
|
|
|
|
except Exception as e:
|
|
end_time = time.time()
|
|
execution_time = end_time - start_time
|
|
print(f" ❌ Error: {e}")
|
|
|
|
return self._create_error_token_metrics(benchmark_config, start_time, execution_time)
|
|
|
|
def _build_tracking_command(self, config: Dict[str, Any]) -> List[str]:
|
|
"""Build command for token tracking benchmark."""
|
|
base_cmd = ["npx", "claude-flow@alpha"]
|
|
|
|
if config["methodology"] == "swarm":
|
|
cmd = base_cmd + [
|
|
"swarm", config["task"],
|
|
"--strategy", config["strategy"],
|
|
"--coordination", config["coordination"],
|
|
"--max-agents", str(config["agents"]),
|
|
"--enable-token-tracking", "true",
|
|
"--output-format", "detailed"
|
|
]
|
|
elif config["methodology"] == "hive-mind":
|
|
cmd = base_cmd + [
|
|
"hive-mind", config["task"],
|
|
"--thinking-pattern", config["strategy"],
|
|
"--coordination", config["coordination"],
|
|
"--agents", str(config["agents"]),
|
|
"--enable-token-tracking", "true"
|
|
]
|
|
elif config["methodology"] == "sparc":
|
|
cmd = base_cmd + [
|
|
"sparc", config["task"],
|
|
"--mode", config["strategy"],
|
|
"--coordination", config["coordination"],
|
|
"--agents", str(config["agents"]),
|
|
"--enable-token-tracking", "true",
|
|
"--refinement-cycles", "2"
|
|
]
|
|
else:
|
|
# Fallback
|
|
cmd = base_cmd + [
|
|
"swarm", config["task"],
|
|
"--strategy", "auto",
|
|
"--max-agents", str(config["agents"]),
|
|
"--enable-token-tracking", "true"
|
|
]
|
|
|
|
return cmd
|
|
|
|
def _extract_token_data(self, stdout: str, stderr: str) -> Dict[str, int]:
|
|
"""Extract token consumption data from command output."""
|
|
token_data = {
|
|
"input_tokens": 0,
|
|
"output_tokens": 0,
|
|
"total_tokens": 0
|
|
}
|
|
|
|
combined_output = stdout + stderr
|
|
|
|
# Try to find JSON token data
|
|
try:
|
|
lines = combined_output.split('\n')
|
|
for line in lines:
|
|
stripped = line.strip()
|
|
if stripped.startswith('{') and 'token' in stripped.lower():
|
|
json_data = json.loads(stripped)
|
|
if "input_tokens" in json_data:
|
|
token_data["input_tokens"] = json_data["input_tokens"]
|
|
if "output_tokens" in json_data:
|
|
token_data["output_tokens"] = json_data["output_tokens"]
|
|
if "total_tokens" in json_data:
|
|
token_data["total_tokens"] = json_data["total_tokens"]
|
|
break
|
|
except:
|
|
pass
|
|
|
|
# Fallback: Parse specific token patterns
|
|
if token_data["total_tokens"] == 0:
|
|
# Look for patterns like "Tokens used: 1234" or "Token consumption: 1234"
|
|
token_patterns = [
|
|
r'tokens?\s*used:?\s*(\d+)',
|
|
r'token\s*consumption:?\s*(\d+)',
|
|
r'total\s*tokens:?\s*(\d+)',
|
|
r'(\d+)\s*tokens?\s*consumed'
|
|
]
|
|
|
|
for pattern in token_patterns:
|
|
matches = re.findall(pattern, combined_output, re.IGNORECASE)
|
|
if matches:
|
|
token_data["total_tokens"] = int(matches[0])
|
|
break
|
|
|
|
# If still no tokens found, estimate from output length
|
|
if token_data["total_tokens"] == 0:
|
|
word_count = len(combined_output.split())
|
|
estimated_tokens = int(word_count * 1.3) # Rough estimate
|
|
token_data["total_tokens"] = estimated_tokens
|
|
token_data["output_tokens"] = int(estimated_tokens * 0.7) # Assume 70% output
|
|
token_data["input_tokens"] = int(estimated_tokens * 0.3) # Assume 30% input
|
|
|
|
# Ensure consistency
|
|
if token_data["input_tokens"] == 0 and token_data["output_tokens"] == 0:
|
|
token_data["input_tokens"] = int(token_data["total_tokens"] * 0.3)
|
|
token_data["output_tokens"] = int(token_data["total_tokens"] * 0.7)
|
|
elif token_data["total_tokens"] == 0:
|
|
token_data["total_tokens"] = token_data["input_tokens"] + token_data["output_tokens"]
|
|
|
|
return token_data
|
|
|
|
def _create_token_metrics(
|
|
self,
|
|
config: Dict[str, Any],
|
|
start_time: float,
|
|
execution_time: float,
|
|
token_data: Dict[str, int],
|
|
success: bool
|
|
) -> TokenMetrics:
|
|
"""Create comprehensive token metrics."""
|
|
|
|
input_tokens = token_data["input_tokens"]
|
|
output_tokens = token_data["output_tokens"]
|
|
total_tokens = token_data["total_tokens"]
|
|
|
|
# Calculate cost
|
|
estimated_cost = (
|
|
input_tokens * self.token_pricing["input_token_cost"] +
|
|
output_tokens * self.token_pricing["output_token_cost"]
|
|
)
|
|
|
|
# Calculate efficiency metrics
|
|
tokens_per_second = total_tokens / execution_time if execution_time > 0 else 0
|
|
|
|
# Calculate efficiency score
|
|
expected_tokens = config.get("expected_tokens", total_tokens)
|
|
if expected_tokens > 0:
|
|
token_efficiency = min(100, (expected_tokens / total_tokens) * 100) if total_tokens > 0 else 0
|
|
else:
|
|
token_efficiency = 50 # Baseline
|
|
|
|
time_efficiency = min(100, (60 / execution_time) * 100) if execution_time > 0 else 0
|
|
|
|
efficiency_score = (token_efficiency * 0.6 + time_efficiency * 0.4) if success else 0
|
|
|
|
return TokenMetrics(
|
|
benchmark_id=config["id"],
|
|
timestamp=start_time,
|
|
input_tokens=input_tokens,
|
|
output_tokens=output_tokens,
|
|
total_tokens=total_tokens,
|
|
estimated_cost_usd=estimated_cost,
|
|
tokens_per_second=tokens_per_second,
|
|
efficiency_score=efficiency_score,
|
|
task_complexity=config["complexity"],
|
|
methodology=config["methodology"],
|
|
coordination_mode=config["coordination"],
|
|
agent_count=config["agents"]
|
|
)
|
|
|
|
def _create_timeout_token_metrics(self, config: Dict[str, Any], start_time: float, execution_time: float) -> TokenMetrics:
|
|
"""Create token metrics for timed out benchmark."""
|
|
return TokenMetrics(
|
|
benchmark_id=config["id"],
|
|
timestamp=start_time,
|
|
input_tokens=0,
|
|
output_tokens=0,
|
|
total_tokens=0,
|
|
estimated_cost_usd=0.0,
|
|
tokens_per_second=0.0,
|
|
efficiency_score=0.0,
|
|
task_complexity=config["complexity"],
|
|
methodology=config["methodology"],
|
|
coordination_mode=config["coordination"],
|
|
agent_count=config["agents"]
|
|
)
|
|
|
|
def _create_error_token_metrics(self, config: Dict[str, Any], start_time: float, execution_time: float) -> TokenMetrics:
|
|
"""Create token metrics for failed benchmark."""
|
|
return TokenMetrics(
|
|
benchmark_id=config["id"],
|
|
timestamp=start_time,
|
|
input_tokens=0,
|
|
output_tokens=0,
|
|
total_tokens=0,
|
|
estimated_cost_usd=0.0,
|
|
tokens_per_second=0.0,
|
|
efficiency_score=0.0,
|
|
task_complexity=config["complexity"],
|
|
methodology=config["methodology"],
|
|
coordination_mode=config["coordination"],
|
|
agent_count=config["agents"]
|
|
)
|
|
|
|
def run_token_tracking_suite(self) -> TokenAnalysis:
|
|
"""Run complete token tracking benchmark suite."""
|
|
print("💰 Real Token Tracking Benchmark Suite")
|
|
print("=" * 50)
|
|
|
|
benchmarks = self.create_token_tracking_benchmarks()
|
|
all_metrics = []
|
|
|
|
for i, benchmark in enumerate(benchmarks, 1):
|
|
print(f"\n[{i}/{len(benchmarks)}] Running {benchmark['id']}")
|
|
print("-" * 40)
|
|
|
|
metrics = self.execute_token_tracking_benchmark(benchmark)
|
|
all_metrics.append(metrics)
|
|
self.token_history.append(metrics)
|
|
|
|
# Generate comprehensive analysis
|
|
analysis = self._analyze_token_consumption(all_metrics)
|
|
|
|
# Save results
|
|
self._save_token_tracking_results(all_metrics, analysis)
|
|
|
|
return analysis
|
|
|
|
def _analyze_token_consumption(self, metrics_list: List[TokenMetrics]) -> TokenAnalysis:
|
|
"""Analyze token consumption patterns and efficiency."""
|
|
if not metrics_list:
|
|
return TokenAnalysis(0, 0, 0.0, 0.0, 0.0, "", "", 0, {}, [])
|
|
|
|
successful_metrics = [m for m in metrics_list if m.total_tokens > 0]
|
|
|
|
if not successful_metrics:
|
|
return TokenAnalysis(len(metrics_list), 0, 0.0, 0.0, 0.0, "", "", 0, {}, ["No successful benchmarks with token data"])
|
|
|
|
# Basic statistics
|
|
total_tokens = sum(m.total_tokens for m in successful_metrics)
|
|
total_cost = sum(m.estimated_cost_usd for m in successful_metrics)
|
|
avg_tokens = total_tokens / len(successful_metrics)
|
|
avg_cost = total_cost / len(successful_metrics)
|
|
|
|
# Analyze by methodology
|
|
methodology_stats = defaultdict(lambda: {"tokens": [], "costs": [], "efficiency": []})
|
|
for m in successful_metrics:
|
|
methodology_stats[m.methodology]["tokens"].append(m.total_tokens)
|
|
methodology_stats[m.methodology]["costs"].append(m.estimated_cost_usd)
|
|
methodology_stats[m.methodology]["efficiency"].append(m.efficiency_score)
|
|
|
|
# Find most efficient methodology
|
|
methodology_efficiency = {}
|
|
for methodology, stats in methodology_stats.items():
|
|
avg_efficiency = sum(stats["efficiency"]) / len(stats["efficiency"])
|
|
avg_tokens_per_run = sum(stats["tokens"]) / len(stats["tokens"])
|
|
# Efficiency score combines effectiveness and token efficiency
|
|
efficiency_metric = avg_efficiency * (1000 / avg_tokens_per_run) # Higher is better
|
|
methodology_efficiency[methodology] = efficiency_metric
|
|
|
|
most_efficient_methodology = max(methodology_efficiency.items(), key=lambda x: x[1])[0] if methodology_efficiency else ""
|
|
|
|
# Analyze by coordination mode
|
|
coordination_stats = defaultdict(lambda: {"tokens": [], "efficiency": []})
|
|
for m in successful_metrics:
|
|
coordination_stats[m.coordination_mode]["tokens"].append(m.total_tokens)
|
|
coordination_stats[m.coordination_mode]["efficiency"].append(m.efficiency_score)
|
|
|
|
coordination_efficiency = {}
|
|
for coordination, stats in coordination_stats.items():
|
|
avg_efficiency = sum(stats["efficiency"]) / len(stats["efficiency"])
|
|
avg_tokens = sum(stats["tokens"]) / len(stats["tokens"])
|
|
efficiency_metric = avg_efficiency * (1000 / avg_tokens)
|
|
coordination_efficiency[coordination] = efficiency_metric
|
|
|
|
most_efficient_coordination = max(coordination_efficiency.items(), key=lambda x: x[1])[0] if coordination_efficiency else ""
|
|
|
|
# Analyze agent scaling
|
|
agent_stats = defaultdict(lambda: {"tokens": [], "efficiency": []})
|
|
for m in successful_metrics:
|
|
agent_stats[m.agent_count]["tokens"].append(m.total_tokens)
|
|
agent_stats[m.agent_count]["efficiency"].append(m.efficiency_score)
|
|
|
|
agent_efficiency = {}
|
|
for agent_count, stats in agent_stats.items():
|
|
avg_efficiency = sum(stats["efficiency"]) / len(stats["efficiency"])
|
|
avg_tokens_per_agent = sum(stats["tokens"]) / len(stats["tokens"]) / agent_count
|
|
# Efficiency per agent
|
|
efficiency_metric = avg_efficiency / avg_tokens_per_agent * 100
|
|
agent_efficiency[agent_count] = efficiency_metric
|
|
|
|
optimal_agent_count = max(agent_efficiency.items(), key=lambda x: x[1])[0] if agent_efficiency else 0
|
|
|
|
# Cost breakdown
|
|
cost_breakdown = {}
|
|
for methodology, stats in methodology_stats.items():
|
|
cost_breakdown[methodology] = sum(stats["costs"])
|
|
|
|
# Generate insights
|
|
insights = []
|
|
|
|
if methodology_efficiency:
|
|
best_method = max(methodology_efficiency.items(), key=lambda x: x[1])
|
|
worst_method = min(methodology_efficiency.items(), key=lambda x: x[1])
|
|
efficiency_diff = best_method[1] / worst_method[1] if worst_method[1] > 0 else 1
|
|
insights.append(f"{best_method[0]} is {efficiency_diff:.1f}x more efficient than {worst_method[0]}")
|
|
|
|
# Token scaling insights
|
|
if len(agent_stats) > 1:
|
|
agent_counts = sorted(agent_stats.keys())
|
|
if len(agent_counts) >= 2:
|
|
low_count = agent_counts[0]
|
|
high_count = agent_counts[-1]
|
|
low_avg = sum(agent_stats[low_count]["tokens"]) / len(agent_stats[low_count]["tokens"])
|
|
high_avg = sum(agent_stats[high_count]["tokens"]) / len(agent_stats[high_count]["tokens"])
|
|
scaling_factor = high_avg / low_avg if low_avg > 0 else 1
|
|
insights.append(f"Token consumption scales {scaling_factor:.1f}x from {low_count} to {high_count} agents")
|
|
|
|
# Cost efficiency insights
|
|
cost_per_1k_tokens = (total_cost / total_tokens * 1000) if total_tokens > 0 else 0
|
|
insights.append(f"Average cost: ${cost_per_1k_tokens:.4f} per 1K tokens")
|
|
|
|
# Coordination efficiency insight
|
|
if coordination_efficiency:
|
|
best_coord = max(coordination_efficiency.items(), key=lambda x: x[1])
|
|
insights.append(f"Most token-efficient coordination: {best_coord[0]}")
|
|
|
|
return TokenAnalysis(
|
|
total_benchmarks=len(metrics_list),
|
|
total_tokens=total_tokens,
|
|
total_cost_usd=total_cost,
|
|
average_tokens_per_benchmark=avg_tokens,
|
|
average_cost_per_benchmark=avg_cost,
|
|
most_efficient_methodology=most_efficient_methodology,
|
|
most_efficient_coordination=most_efficient_coordination,
|
|
optimal_agent_count=optimal_agent_count,
|
|
cost_breakdown=cost_breakdown,
|
|
efficiency_insights=insights
|
|
)
|
|
|
|
def _save_token_tracking_results(self, metrics_list: List[TokenMetrics], analysis: TokenAnalysis):
|
|
"""Save token tracking results and analysis."""
|
|
output_dir = Path("/workspaces/claude-code-flow/benchmark/examples/output")
|
|
output_dir.mkdir(exist_ok=True)
|
|
|
|
timestamp = int(time.time())
|
|
|
|
# Save individual metrics
|
|
metrics_data = [asdict(m) for m in metrics_list]
|
|
with open(output_dir / f"token_tracking_metrics_{timestamp}.json", "w") as f:
|
|
json.dump(metrics_data, f, indent=2)
|
|
|
|
# Save analysis
|
|
with open(output_dir / f"token_analysis_{timestamp}.json", "w") as f:
|
|
json.dump(asdict(analysis), f, indent=2)
|
|
|
|
# Save CSV for spreadsheet analysis
|
|
with open(output_dir / f"token_tracking_{timestamp}.csv", "w") as f:
|
|
f.write("benchmark_id,methodology,coordination,agents,complexity,total_tokens,cost_usd,efficiency_score\n")
|
|
for m in metrics_list:
|
|
f.write(f"{m.benchmark_id},{m.methodology},{m.coordination_mode},{m.agent_count},"
|
|
f"{m.task_complexity},{m.total_tokens},{m.estimated_cost_usd:.6f},{m.efficiency_score:.2f}\n")
|
|
|
|
print(f"\n📁 Token tracking results saved to: {output_dir}")
|
|
print(f" 📊 Metrics: token_tracking_metrics_{timestamp}.json")
|
|
print(f" 📈 Analysis: token_analysis_{timestamp}.json")
|
|
print(f" 📋 CSV: token_tracking_{timestamp}.csv")
|
|
|
|
def generate_token_optimization_report(analysis: TokenAnalysis):
|
|
"""Generate a comprehensive token optimization report."""
|
|
print(f"\n💰 Token Optimization Report")
|
|
print("=" * 40)
|
|
|
|
print(f"💳 Cost Summary:")
|
|
print(f" Total cost: ${analysis.total_cost_usd:.4f}")
|
|
print(f" Average per benchmark: ${analysis.average_cost_per_benchmark:.4f}")
|
|
print(f" Cost per 1K tokens: ${(analysis.total_cost_usd / analysis.total_tokens * 1000):.4f}")
|
|
|
|
print(f"\n🧠 Token Efficiency:")
|
|
print(f" Total tokens: {analysis.total_tokens:,}")
|
|
print(f" Average per benchmark: {analysis.average_tokens_per_benchmark:.0f}")
|
|
print(f" Most efficient methodology: {analysis.most_efficient_methodology}")
|
|
print(f" Most efficient coordination: {analysis.most_efficient_coordination}")
|
|
print(f" Optimal agent count: {analysis.optimal_agent_count}")
|
|
|
|
print(f"\n💡 Optimization Insights:")
|
|
for insight in analysis.efficiency_insights:
|
|
print(f" • {insight}")
|
|
|
|
print(f"\n📊 Cost Breakdown by Methodology:")
|
|
for methodology, cost in analysis.cost_breakdown.items():
|
|
percentage = (cost / analysis.total_cost_usd * 100) if analysis.total_cost_usd > 0 else 0
|
|
print(f" {methodology}: ${cost:.4f} ({percentage:.1f}%)")
|
|
|
|
if __name__ == "__main__":
|
|
print("💰 Real Token Tracking and Analysis")
|
|
print("=" * 50)
|
|
|
|
# Initialize tracker
|
|
tracker = RealTokenTracker()
|
|
|
|
# Run token tracking suite
|
|
analysis = tracker.run_token_tracking_suite()
|
|
|
|
# Generate optimization report
|
|
generate_token_optimization_report(analysis)
|
|
|
|
print(f"\n🎉 Token Tracking Complete!")
|
|
print("This analysis provides:")
|
|
print("- Accurate token consumption measurements")
|
|
print("- Cost analysis and optimization insights")
|
|
print("- Methodology and coordination efficiency comparison")
|
|
print("- Agent scaling impact on token usage")
|
|
print("- Production cost estimation guidelines") |