Skip to main content

Analyze token waste and optimize LLM routing for cost savings

Project description

Cost Optimizer

License: MIT Python 3.10+ Tests

Analyze token waste in LLM agent traces and optimize cost through intelligent model routing.

Installation

pip install cost-optimizer

Quick Start

Analyze Traces

# Analyze a trace file for waste
costopt analyze trace.jsonl

# Specify model for cost calculation
costopt analyze trace.jsonl --model gpt-4o

# Save results
costopt analyze trace.jsonl -o results.json

Estimate Costs

# Estimate cost for 1M tokens on Claude 3.5 Sonnet
costopt estimate --model claude-3-5-sonnet-20241022 --tokens 1000000

# Compare costs across all models
costopt estimate --model claude-3-5-sonnet-20241022 --tokens 1000000 --compare

Optimize

# Get optimization recommendations
costopt optimize trace.jsonl

Programming API

from cost_optimizer import TokenAnalyzer, CostOptimizer, ModelRouter, PricingData

# Analyze traces
analyzer = TokenAnalyzer(default_model="claude-3-5-sonnet-20241022")
report = analyzer.analyze_trace("trace.jsonl")
print(f"Waste: {report.total_waste_tokens} tokens")

# Get optimization recommendations
optimizer = CostOptimizer()
optimizations = optimizer.optimize(report)
for opt in optimizations:
    print(f"{opt.strategy}: saves ${opt.estimated_savings_usd:.2f}")

# Route to cheaper models
router = ModelRouter()
model = router.route("simple formatting task")  # → claude-3-5-haiku-20241022
assessment = router.assess("design a distributed system architecture")
print(f"Complexity: {assessment.complexity_score}, Model: {assessment.recommended_model}")

# Calculate costs
cost = PricingData.calculate_cost(1000000, "claude-3-5-sonnet-20241022")

Supported Models

Model Tier Input/1M tokens Output/1M tokens
Claude 3.5 Haiku mini $0.80 $4.00
GPT-4o Mini mini $0.15 $0.60
Qwen3 Coder mini $0.50 $1.50
Claude 3.5 Sonnet standard $3.00 $15.00
GPT-4o standard $2.50 $10.00
GPT-4 Turbo premium $10.00 $30.00
GPT-4 premium $30.00 $60.00
Claude 3 Opus flagship $15.00 $75.00

License

MIT

Ecosystem

Part of the FableForge ecosystem — 21 open-source projects built from 210K real agent traces:

Project Description
Anvil Self-verified coding agent
VerifyLoop Plan→Execute→Verify→Recover framework
ErrorRecovery Self-healing middleware (3,725 error patterns)
FableForge-14B The fine-tuned 14B model (4-stage training)
ShellWhisperer 1.5B edge agent (phone/RPi, 50ms)
ReasonCritic Verification model (130 benchmark tasks)
TraceCompiler Compile traces → LoRA skills
AgentRuntime Persistent agent daemon (systemd for AI)
AgentSwarm Multi-agent from real trace transitions
AgentTelemetry Datadog for agents (token tracking, costs)
BenchAgent HumanEval for tool-use (107 tasks)
AgentDev VSCode extension with verification
TraceViz Trace replay visualizer (Next.js)
AgentSkills npm for agent behaviors
AgentCurriculum 5-stage progressive training
AgentFuzzer Adversarial testing for agents
AgentConstitution Safety guardrails from traces
CostOptimizer Token cost reduction (50-80%)
AgentProfiler Behavioral fingerprinting
TrajectoryDistiller Trace→training data pipeline
Fable5-Dataset HuggingFace dataset release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fableforge_cost_optimizer-0.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fableforge_cost_optimizer-0.1.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file fableforge_cost_optimizer-0.1.0.tar.gz.

File metadata

File hashes

Hashes for fableforge_cost_optimizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8b8489046f882ff95ad4239559ea547476e0dabc8214c229922cbeac59e5f8f8
MD5 6f43bed53998ed8190c7710ae67b2c99
BLAKE2b-256 3b0dcc51df5adc48797ca97277401ff88bcdcab3b8e81b2c32d7e86938d70832

See more details on using hashes here.

Provenance

The following attestation bundles were made for fableforge_cost_optimizer-0.1.0.tar.gz:

Publisher: release.yml on KingLabsA/cost-optimizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fableforge_cost_optimizer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fableforge_cost_optimizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a10abb3b28c46a6eb2ac507762bf4a7ba813a2d2684331f09eb10e2ca8562d6
MD5 3751e0f5fb6ee8b4c599668b43b116cf
BLAKE2b-256 4f30a56ff792874fbaa9d944b01b2d6d378add8c813f56a6e4e9cb57ba7431c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for fableforge_cost_optimizer-0.1.0-py3-none-any.whl:

Publisher: release.yml on KingLabsA/cost-optimizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page