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 |
Release files for fableforge-cost-optimizer 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fableforge_cost_optimizer-0.1.0.tar.gz | 16.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fableforge_cost_optimizer-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.9 kB
Release files / fableforge_cost_optimizer-0.1.0.tar.gz
| Download URL | fableforge_cost_optimizer-0.1.0.tar.gz |
|---|---|
| Size | 16.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8b8489046f882ff95ad4239559ea547476e0dabc8214c229922cbeac59e5f8f8
|
|
BLAKE2b-256 checksum How to use checksums |
3b0dcc51df5adc48797ca97277401ff88bcdcab3b8e81b2c32d7e86938d70832
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 14, 2026.
Transparency logRelease files / fableforge_cost_optimizer-0.1.0-py3-none-any.whl
| Download URL | fableforge_cost_optimizer-0.1.0-py3-none-any.whl |
|---|---|
| Size | 16.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9a10abb3b28c46a6eb2ac507762bf4a7ba813a2d2684331f09eb10e2ca8562d6
|
|
BLAKE2b-256 checksum How to use checksums |
4f30a56ff792874fbaa9d944b01b2d6d378add8c813f56a6e4e9cb57ba7431c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 14, 2026.
Transparency log