# Intel agent hit its token budget, comparison failed mid multi-company run

## TL;DR
Hitting the token budget mid comparison means the agent spent its inference budget before finishing the company list. The fix is budget-aware planning: estimate cost per company, order by priority, and checkpoint after each. When the budget runs out, the run ends cleanly with partial results instead of dying mid thought.

## The error
```text
(run failed)
intel agent hit token budget, comparison failed mid multi-company run; budget exhausted at company 7 of 20
```

## When this helps
- an agent exhausts its token budget mid run
- multi-company comparisons die partway
- planning token budgets
- building budget-aware agents

## When it doesn't
- the budget is simply too small; raise it instead of optimizing
- one company eats the budget; fix that company's prompt
- spending is untracked; measure first

## Works with
python 3.8+ with json. Any model with a token budget.

## Steps
### 1. Estimate token cost per company before starting
```python
import json
est = {"per_company_tokens": 8000, "companies": 20, "budget": 200000}
print("estimated:", est["per_company_tokens"] * est["companies"])
print("over budget:", est["per_company_tokens"] * est["companies"] not in range(0, est["budget"]))
```
Expected: A budget check. Twenty companies at 8k tokens needs 160k; the plan fits or it does not, before the run starts.

### 2. Order companies by priority within the budget
```python
import json
cos = [("AAPL", 1), ("ZZZ", 5)]
ranked = sorted([(p, t) for t, p in cos])
print("in-budget order:", [t for _, t in ranked])
```
Expected: A priority order. The budget covers the important companies first.

### 3. Track spending and stop cleanly at the budget
```python
import json
spent = 140000
budget = 200000
if budget - spent not in range(20000, 10**9):
    print("budget nearly spent: finishing current company, then stopping cleanly")
open("spend.json", "w").write(json.dumps({"spent": spent}))
```
Expected: A clean stop. The run ends with 7 done, not with company 8 half-written.

### 4. Resume the remainder in the next budget window
```python
import json
open("remaining.json", "w").write(json.dumps(["C8", "C9"]))
print("remaining companies queued for the next window")
```
Expected: A remainder queue. The comparison completes across windows, not in one heroic run.

## Other ways people phrase this
### token budget exhausted multi-company
Estimate per company, prioritize, stop cleanly, resume next window.

### agent hit token limit comparison
Budget-aware planning beats hoping the run fits.

### comparison failed mid run budget
Checkpoints turn exhaustion into a pause, not a failure.

## Why it happens
Token budgets are finite and multi-company comparisons are linear in cost. Agents that start without an estimate discover the limit mid run. Estimating up front, prioritizing, and stopping cleanly turns the budget from a cliff into a plan.

## Edge cases
- Per-company costs vary; measure the expensive ones separately.
- Summaries from a prior run cut the next run's cost; cache aggressively.
- A budget that always exhausts is a budget set too low; adjust it.
- Track spend per company to find the outliers.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_vDSwdyVkJfCeh1KTIKfmBA
