Commit 26fec19
feat: token and credit optimization engine (10 strategies, 50 tests)
src/specsmith/agent/optimizer.py — new module implementing:
1. Response caching — SHA-256 hash cache (30-70% savings)
2. Prompt caching — Anthropic cache_control (50-90% savings)
3. Context trimming — sliding window, always preserves system msg
4. Model routing — keyword heuristic FAST/BALANCED/POWERFUL tier
5. Output length ctrl — max_tokens awareness (3-8x impact vs input)
6. Tool filtering — top-N relevant tools only (cuts 55K-134K overhead)
7. Token estimation — pre-flight cost from character ratios
8. Duplicate detection — identical messages served from cache instantly
9. Summarization trigger— signal when history exceeds threshold
10. Optimization report — cache hit rate, tokens saved, savings USD
src/specsmith/agent/providers/anthropic.py:
- Add prompt_caching=True param; injects cache_control ephemeral on
system message block to enable Anthropic 90% cached-read discount
src/specsmith/agent/runner.py:
- AgentRunner gains optional OptimizationEngine (--optimize flag)
- _call_provider: pre_call() transforms messages/model/tools;
post_call() records tokens saved and populates cache
src/specsmith/cli.py:
- specsmith run --optimize: enable optimization engine per session
- specsmith optimize: analyse .specsmith/ usage and print projected
monthly savings with per-strategy breakdown and recommendations
tests/test_optimizer.py:
- 50 unit tests covering all 13 REQ-OPT-* requirements
docs/:
- REQ-OPT-001 through REQ-OPT-013 in REQUIREMENTS.md
- TEST-OPT-001 through TEST-OPT-013 in TEST_SPEC.md
Co-Authored-By: Oz <oz-agent@warp.dev>1 parent adb02f9 commit 26fec19
7 files changed
Lines changed: 1677 additions & 12 deletions
File tree
- docs
- src/specsmith
- agent
- providers
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
309 | 325 | | |
310 | 326 | | |
311 | 327 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
539 | 539 | | |
540 | 540 | | |
541 | 541 | | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
542 | 571 | | |
543 | 572 | | |
544 | 573 | | |
| |||
0 commit comments