> ## Documentation Index
> Fetch the complete documentation index at: https://splinter.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Budget Limits

> Cap total API spend

Stop agents when they've spent too much money.

## Simple API

```python theme={null}
from splinter import Splinter

s = Splinter(
    openai_key="sk-...",
    max_budget=1.0,  # Stop at $1
)

result = await s.run("helper", "Do something expensive")
print(f"Spent: ${s.cost:.4f}")
```

## Advanced API

```python theme={null}
from splinter import Gateway, ExecutionLimits

gateway = Gateway(limits=ExecutionLimits(
    max_budget=5.0,  # $5 max
))
gateway.configure_provider("openai", api_key="sk-...")

# Check current spend
metrics = gateway.get_metrics()
print(f"Total cost: ${metrics['total_cost']:.4f}")
```

## Per-Agent Limits

```python theme={null}
from splinter import ExecutionLimits

limits = ExecutionLimits(
    max_budget=10.0,           # Workflow max
    per_agent_max_budget=2.0,  # Each agent max
)
```

## Error Handling

```python theme={null}
from splinter.exceptions import BudgetExceededError

try:
    result = await workflow.run()
except BudgetExceededError as e:
    print(f"Budget exceeded: ${e.current:.4f} >= ${e.limit:.4f}")
```

## Cost Tracking

Each provider tracks costs automatically:

| Provider  | Pricing Source          |
| --------- | ----------------------- |
| OpenAI    | Per-model pricing table |
| Anthropic | Per-model pricing table |
| Gemini    | Per-model pricing table |
| Grok      | Per-model pricing table |

```python theme={null}
# After a call
response = await gateway.call(...)
print(f"This call cost: ${response.cost:.4f}")
print(f"Input tokens: {response.input_tokens}")
print(f"Output tokens: {response.output_tokens}")
```
