Setting it
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
});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
)When it is checked
| 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
- Every model turn of the run.
- The summary turn that 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.
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
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.
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.
}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.Media is where a budget earns its keep
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.
Watching it as it goes
await agent.run({
inputs: "…",
onEvent: (e) => {
if (e.type === "turn") {
console.log(`${e.totalCredits} credits so far`);
}
},
});def on_event(event):
if event["type"] == "tool_end":
print(f"{agent.usage['credits']} credits so far")
agent.run(inputs="…", on_event=on_event)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
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.