# Budget

Canonical URL: https://developers.iagenify.com/sdk/agent/budget/
Language: en

A ceiling in credits, checked before every step and before every paid tool. It is the ceiling worth setting — steps are a runaway guard, credits are the bill.

Status: budget: null by default · checked before the step · shared with subagents

## Setting it {#setting}

```javascript
const agent = api.agents.create({
  runtime: "local",
  machine: pc,
  model: "gemini-3.5-flash",
  budget: 200,          // credits for this run
  maxSteps: 40,         // and a step ceiling, lower than the default 100
});
```

## When it is checked {#when}

| Moment | What is compared |
| --- | --- |
| Top of each step | Credits spent so far against `budget`. Equal or above ends the run with `stopReason: "budget"`. |
| Before a paid tool | The tool's declared cost against what is left. |

Both are **before**, never after. A budget checked afterwards would only tell you what you had already spent.

## What counts against it {#counted}

- Every model turn of the run.
- The summary turn that [compaction](/sdk/agent/context/compaction) performs.
- Every paid tool — a search, a page capture, a conversion, a generated image, a semantic search.
- Every turn and every paid tool of every subagent it started.

> **A subagent spends the parent's budget, not its own**
>
> It reads the parent's running total, so three subagents working in parallel are bounded together. A budget that only bounded the parent's own turns would bound almost nothing.

## It can be passed by one call {#overshoot}

The check happens before a step, so a step that is allowed can still cost more than what was left — a long turn, or a tool whose real cost exceeds its declared one. The budget bounds when the run *stops*, not the exact total.

```javascript
const result = await agent.run({ inputs: "…", });

if (result.stopReason === "budget") {
  console.log(result.usage.credits, "spent of a", 200, "budget");
  // Slightly above is normal: the last step was allowed before it cost anything.
}
```

## Media is where a budget earns its keep {#media}

A text turn is a few credits; a generated video is dozens. The media tools declare a cost that deliberately over-estimates, precisely so the guard can refuse **before** the call — see [video](/sdk/ref/tools/video).

## Watching it as it goes {#reading}

```javascript
await agent.run({
  inputs: "…",
  onEvent: (e) => {
    if (e.type === "turn") {
      console.log(`${e.totalCredits} credits so far`);
    }
  },
});
```

> **totalCredits already includes the tools**
>
> The running total on a turn event counts the paid tools of the run as well as the turns. Adding a tool's own meta.credits on top of it double-counts, and a balance read by the account tool is not a cost at all.

## Without a budget {#no-budget}

`budget: null` is the default, and the run is then bounded only by `maxSteps` and by the model's willingness to stop. That is reasonable for an interactive agent a person is watching; it is not reasonable for anything unattended.

- [The loop](/sdk/agent/loop): What one step is, and what ends it.
- [Options · budget and context](/sdk/ref/agents/options/budget): The option, beside its neighbours.
- [Credits](/sdk/concepts/credits): What a credit is.

```python
agent = api.agents.create(
    runtime="local",
    machine=pc,
    model="gemini-3.5-flash",
    budget=200,          # credits for this run
    max_steps=40,        # and a step ceiling, lower than the default 100
)
```

```python
result = agent.run(inputs="…")

if result["stopReason"] == "budget":
    print(result["usage"]["credits"], "spent of a", 200, "budget")
    # Slightly above is normal: the last step was allowed before it cost anything.
```

```python
def on_event(event):
    if event["type"] == "tool_end":
        print(f"{agent.usage['credits']} credits so far")

agent.run(inputs="…", on_event=on_event)
```
