Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

πŸ”– Checkpoint: Edit /workspaces/claude-code-flow/benchmark/examples/real/real_token_tracking.py

rUv204da78e8869
diff --git a/benchmark/examples/.claude/checkpoints/1754448294.json b/benchmark/examples/.claude/checkpoints/1754448294.json
new file mode 100644
index 0000000000..93b8614d8a
--- /dev/null
+++ b/benchmark/examples/.claude/checkpoints/1754448294.json
@@ -0,0 +1 @@
+{"branch": "checkpoint/pre-edit-20250806-024453", "file": "/workspaces/claude-code-flow/benchmark/examples/cli/batch_benchmarks.sh", "timestamp": "2025-08-06T02:44:54Z", "type": "pre-edit", "original_branch": "alpha-88"}
diff --git a/benchmark/examples/.claude/checkpoints/1754448295.json b/benchmark/examples/.claude/checkpoints/1754448295.json
new file mode 100644
index 0000000000..0f423a4592
--- /dev/null
+++ b/benchmark/examples/.claude/checkpoints/1754448295.json
@@ -0,0 +1 @@
+{"tag": "checkpoint-20250806-024455", "file": "/workspaces/claude-code-flow/benchmark/examples/cli/batch_benchmarks.sh", "timestamp": "2025-08-06T02:44:55Z", "type": "post-edit", "branch": "alpha-88", "diff_summary": " .claude-flow/metrics/system-metrics.json | 252 ++++++++++ benchmark/.claude-flow/metrics/performance.json | 2 +- benchmark/.claude-flow/metrics/task-metrics.json | 6 +- .../examples/.claude-flow/metrics/performance.json | 2 +- .../.claude-flow/metrics/task-metrics.json | 6 +- .../examples/.claude/checkpoints/1754448240.json | 1 + .../examples/.claude/checkpoints/1754448242.json | 1 + benchmark/examples/claude_optimizer_example.py | 520 --------------------- benchmark/examples/cli/batch_benchmarks.sh | 361 ++++++++++++++ benchmark/examples/demo_comprehensive.py | 233 --------- benchmark/examples/demo_mle_star.py | 152 ------ benchmark/examples/demo_real_benchmark.py | 219 --------- benchmark/examples/example_usage.py | 249 ---------- benchmark/examples/mle_star_benchmark_example.py | 229 --------- benchmark/examples/parallel_benchmark_demo.py | 254 ---------- benchmark/examples/real_benchmark_examples.py | 376 --------------- benchmark/examples/real_hive_mind_benchmark.py | 191 -------- benchmark/examples/real_metrics_demo.py | 238 ---------- benchmark/examples/real_sparc_benchmark.py | 228 --------- benchmark/examples/verify_real_integration.py | 408 ---------------- .../cli/__pycache__/main.cpython-312.pyc | Bin 21009 -> 22009 bytes .../optimized_benchmark_engine.cpython-312.pyc | Bin 15770 -> 15428 bytes .../__pycache__/engine.cpython-312.pyc | Bin 32449 -> 33620 bytes benchmark/test_optimization_fix.py | 167 ------- .../tests/.claude-flow/metrics/performance.json | 2 +- .../tests/.claude-flow/metrics/task-metrics.json | 6 +- claude-flow-wiki | 0 27 files changed, 627 insertions(+), 3476 deletions(-) "}
diff --git a/benchmark/examples/real/real_token_tracking.py b/benchmark/examples/real/real_token_tracking.py
new file mode 100644
index 0000000000..61016cb88a
--- /dev/null
+++ b/benchmark/examples/real/real_token_tracking.py
@@ -0,0 +1,589 @@
+#!/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")
\ No newline at end of file