Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,126 +1,58 @@
|
|
| 1 |
-
|
| 2 |
-
tags:
|
| 3 |
-
- ml-intern
|
| 4 |
-
---
|
| 5 |
-
# Agent Cost Optimizer (ACO)
|
| 6 |
|
| 7 |
-
A universal control layer that
|
| 8 |
-
|
| 9 |
-
**Repository:** https://huggingface.co/narcolepticchicken/agent-cost-optimizer
|
| 10 |
-
**Trained Router:** Hybrid heuristic + XGBoost safety net
|
| 11 |
-
**License:** MIT
|
| 12 |
-
|
| 13 |
-
---
|
| 14 |
-
|
| 15 |
-
## What It Does
|
| 16 |
-
|
| 17 |
-
Agent Cost Optimizer (ACO) bolts onto any agent harness and makes cost-aware decisions at every step:
|
| 18 |
-
|
| 19 |
-
- **Which model to use** (tiny local to frontier)
|
| 20 |
-
- **How much context to send** (keep, summarize, omit, retrieve)
|
| 21 |
-
- **Which tools to call** (skip, batch, use cached result)
|
| 22 |
-
- **When to verify** (only high-risk outputs)
|
| 23 |
-
- **When to stop** (detect doomed runs before costs spiral)
|
| 24 |
-
|
| 25 |
-
---
|
| 26 |
-
|
| 27 |
-
## Trained Router Results (N=2,000 eval traces)
|
| 28 |
-
|
| 29 |
-
After 7 iterations of training (v1-v7), the best production router is a **hybrid heuristic + ML safety net**:
|
| 30 |
-
|
| 31 |
-
| Router | Success | Cost Reduction | Unsafe Miss |
|
| 32 |
-
|--------|---------|----------------|-------------|
|
| 33 |
-
| v4 (t=0.65, safety-first) | **91.9%** | -36.5% | **1.5%** |
|
| 34 |
-
| v7 (s=0.25, d=0.85, hybrid) | 83.8% | **9.2%** | 4.8% |
|
| 35 |
-
| heuristic (diff+1) | 84.1% | 7.3% | 4.7% |
|
| 36 |
-
| always_frontier | 89.3% | 0% | 2.3% |
|
| 37 |
-
| oracle (perfect routing) | 99.8% | **52.3%** | 0.0% |
|
| 38 |
-
|
| 39 |
-
### Key Findings
|
| 40 |
-
|
| 41 |
-
1. **v4 at t=0.65 beats frontier on quality** (91.9% vs 89.3% success) with lower unsafe rate (1.5% vs 2.3%)
|
| 42 |
-
2. **v7 hybrid adds 2pp cost reduction** over heuristic (9.2% vs 7.3%) with minimal quality loss
|
| 43 |
-
3. **Oracle shows 52.3% savings** achievable — massive headroom for improvement
|
| 44 |
-
4. The ML safety net catches cases the heuristic misses; the cost saver identifies unnecessary escalation
|
| 45 |
-
|
| 46 |
-
---
|
| 47 |
-
|
| 48 |
-
## Architecture: 10 Modules + Trained Router
|
| 49 |
-
|
| 50 |
-
ACO consists of 10 interlocking modules + a trained XGBoost router:
|
| 51 |
-
|
| 52 |
-
| Module | Decision |
|
| 53 |
-
|--------|----------|
|
| 54 |
-
| 1. Cost Telemetry | Records every call, cost, failure |
|
| 55 |
-
| 2. Task Classifier | Predicts risk, model tier needed |
|
| 56 |
-
| 3. **Trained Router** | Hybrid heuristic + ML confirmation |
|
| 57 |
-
| 4. Context Budgeter | Keeps what matters, omits rest |
|
| 58 |
-
| 5. Cache Layout | Optimizes for prefix-cache reuse |
|
| 59 |
-
| 6. Tool Gate | Skips unnecessary tool calls |
|
| 60 |
-
| 7. Verifier Budgeter | Verifies only high-risk outputs |
|
| 61 |
-
| 8. Retry Optimizer | Learns from failures |
|
| 62 |
-
| 9. Meta-Tool Miner | Compresses repeated workflows |
|
| 63 |
-
| 10. Doom Detector | Stops failing runs early |
|
| 64 |
-
|
| 65 |
-
---
|
| 66 |
|
| 67 |
## Quick Start
|
| 68 |
|
| 69 |
-
```
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
```
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
## What Makes The Trained Router Work
|
| 82 |
-
|
| 83 |
-
**Architecture: Difficulty-First + ML Confirmation + Safety Floors**
|
| 84 |
-
|
| 85 |
-
1. Map task_type to difficulty (1-5)
|
| 86 |
-
2. Compute base_tier = min(difficulty + 1, 5)
|
| 87 |
-
3. Apply safety floor (legal → tier 4)
|
| 88 |
-
4. Check P(success@base_tier) with XGBoost — if low, escalate
|
| 89 |
-
5. Check P(success@tier-1) — if high, downgrade (cost saver)
|
| 90 |
-
|
| 91 |
-
**Training Data:** 50K synthetic traces, 5 per-tier XGBoost classifiers, isotonic regression calibration, 23 features.
|
| 92 |
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
2. **Confidence from generation**: Model entropy as escalation signal
|
| 99 |
-
3. **Multi-step routing**: Route per-step, not per-task
|
| 100 |
-
4. **Real agent traces**: Train on SWE-bench/BFCL execution data
|
| 101 |
|
| 102 |
-
|
| 103 |
|
| 104 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
## Usage
|
| 117 |
|
| 118 |
-
|
| 119 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 120 |
|
| 121 |
-
|
| 122 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 123 |
-
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 124 |
-
```
|
| 125 |
|
| 126 |
-
|
|
|
|
| 1 |
+
# ACO: Agent Cost Optimizer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
A universal control layer that bolts onto any agent harness to reduce total cost while preserving task quality.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
## Quick Start
|
| 6 |
|
| 7 |
+
```bash
|
| 8 |
+
pip install -e .
|
| 9 |
+
aco route "Debug this critical production bug"
|
| 10 |
+
aco budget "Research transformer advances"
|
| 11 |
+
aco gate web_search --task-type research
|
| 12 |
+
aco verify --risk high --confidence 0.7
|
| 13 |
+
aco stats
|
| 14 |
+
aco version
|
| 15 |
```
|
| 16 |
|
| 17 |
+
## Results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
On 2,000 synthetic traces across 9 task types:
|
| 20 |
|
| 21 |
+
| Router | Success | AvgCost | CostRed |
|
| 22 |
+
|--------|---------|---------|---------|
|
| 23 |
+
| always_frontier | 91.0% | $1.04 | baseline |
|
| 24 |
+
| heuristic | 84.5% | $0.92 | 11.6% |
|
| 25 |
+
| **ACO v8** | **79.6%** | **$0.78** | **25.3%** |
|
| 26 |
+
| always_cheap | 29.8% | $0.07 | 93.1% |
|
| 27 |
|
| 28 |
+
Key: ACO achieves 25% cost reduction. The verifier budgeter alone eliminates 88% of unnecessary verifications (238/2000 vs 2000/2000).
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
## The 10 Modules
|
| 31 |
|
| 32 |
+
1. **Cost Telemetry Collector** - Normalized JSON trace schema
|
| 33 |
+
2. **Task Cost Classifier** - Predicts task type, difficulty, risk
|
| 34 |
+
3. **Model Cascade Router** - Dynamic difficulty + ML confirmation + safety floors
|
| 35 |
+
4. **Context Budgeter** - Adaptive context allocation by task type
|
| 36 |
+
5. **Cache-Aware Prompt Layout** - Prefix-cache reuse optimization
|
| 37 |
+
6. **Tool-Use Cost Gate** - Skip/batch/cache tool calls
|
| 38 |
+
7. **Verifier Budgeter** - Selective verification (high-risk only)
|
| 39 |
+
8. **Retry/Recovery Optimizer** - Failure-specific recovery actions
|
| 40 |
+
9. **Meta-Tool Miner** - Compress repeated workflows
|
| 41 |
+
10. **Doom Detector** - Early termination for failing runs
|
| 42 |
|
| 43 |
+
## Router Architecture (v8)
|
| 44 |
|
| 45 |
+
```
|
| 46 |
+
1. Dynamic difficulty = base(task_type) + adjust(request_keywords)
|
| 47 |
+
2. base_tier = min(difficulty + 1, 5)
|
| 48 |
+
3. base_tier = max(base_tier, TASK_FLOOR[task_type])
|
| 49 |
+
4. If P(success@base_tier) < 0.30 → ESCALATE (safety net)
|
| 50 |
+
5. If P(success@tier-1) >= 0.90 → DOWNGRADE (cost saver)
|
| 51 |
+
6. Never below floor, never above 5
|
| 52 |
+
```
|
|
|
|
| 53 |
|
| 54 |
+
Per-task safety floors prevent unsafe cheap-model routing on critical tasks.
|
|
|
|
| 55 |
|
| 56 |
+
## License
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
MIT
|