Analyze token waste and optimize LLM routing for cost savings
Project description
Cost Optimizer
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fableforge_cost_optimizer-0.1.0.tar.gz.
File metadata
- Download URL: fableforge_cost_optimizer-0.1.0.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b8489046f882ff95ad4239559ea547476e0dabc8214c229922cbeac59e5f8f8
|
|
| MD5 |
6f43bed53998ed8190c7710ae67b2c99
|
|
| BLAKE2b-256 |
3b0dcc51df5adc48797ca97277401ff88bcdcab3b8e81b2c32d7e86938d70832
|
Provenance
The following attestation bundles were made for fableforge_cost_optimizer-0.1.0.tar.gz:
Publisher:
release.yml on KingLabsA/cost-optimizer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fableforge_cost_optimizer-0.1.0.tar.gz -
Subject digest:
8b8489046f882ff95ad4239559ea547476e0dabc8214c229922cbeac59e5f8f8 - Sigstore transparency entry: 1819994422
- Sigstore integration time:
-
Permalink:
KingLabsA/cost-optimizer@223fe75a74831e8603e7ca40a7bc4efb643d1786 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KingLabsA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@223fe75a74831e8603e7ca40a7bc4efb643d1786 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fableforge_cost_optimizer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fableforge_cost_optimizer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a10abb3b28c46a6eb2ac507762bf4a7ba813a2d2684331f09eb10e2ca8562d6
|
|
| MD5 |
3751e0f5fb6ee8b4c599668b43b116cf
|
|
| BLAKE2b-256 |
4f30a56ff792874fbaa9d944b01b2d6d378add8c813f56a6e4e9cb57ba7431c5
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fableforge_cost_optimizer-0.1.0-py3-none-any.whl -
Subject digest:
9a10abb3b28c46a6eb2ac507762bf4a7ba813a2d2684331f09eb10e2ca8562d6 - Sigstore transparency entry: 1819994459
- Sigstore integration time:
-
Permalink:
KingLabsA/cost-optimizer@223fe75a74831e8603e7ca40a7bc4efb643d1786 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KingLabsA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@223fe75a74831e8603e7ca40a7bc4efb643d1786 -
Trigger Event:
push
-
Statement type: