## TL;DR
Give the agent a spend budget per run: a max number of API calls or a dollar cap, tracked in a counter that every search call decrements. When the budget hits zero, the agent stops and reports instead of continuing. Add a soft warning at 80 percent. An agent with no budget is a blank check; the guard is a few lines of code.

```text
agent exhausted Tavily credits in one run with no budget guard
```

## Use this when
- one run burns all your search API credits
- there is no per-run cap on paid API calls
- you only discover the spend after the invoice

## Not for this skill when
- results are bad quality (tune the query, not the budget)
- you want to compare search providers on price
- the API key itself is invalid or revoked

## Steps
1. Read the API key with os.environ.get("TAVILY_API_KEY") and never print or log it. Verify: the key value appears nowhere in logs.
2. Set a max-calls budget (for example 100) and a shared counter the agent checks before every call. Verify: the counter increments once per actual API call.
3. When the counter reaches 80 percent, log a warning with calls used and remaining. Verify: the warning fires exactly once per run.
4. At 100 percent, raise a budget-exhausted error and stop the run cleanly with a summary of what was found. Verify: zero calls happen after the cap.
5. Persist the counter across restarts so a crash does not reset the budget. Verify: a restarted run continues counting from where it stopped.

### Tool/version compatibility
Any paid search API (Tavily, Serper, Exa). The guard is provider-agnostic and needs no SDK changes.

## Why it happens
The agent's loop has no notion of cost; each search looks free in isolation. Without a counter, a broad query strategy multiplies into hundreds of calls before anyone notices.

## Edge cases / pitfalls
- Budgets are a planning tool, not a workaround: never create extra accounts to dodge a cap.
- Cache results by query hash so repeated queries cost nothing.
- A stuck loop can burn the whole budget on one bad query; combine the budget with a per-query retry limit.

## Variant phrasings
### Search agent burned through all API credits in one run
Short version: no spend cap on search calls.
### One agent run used up the monthly Tavily quota
Short version: missing budget guard.

## Provenance

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