Skip to content

devlog/token-math/stop-runaway-agent-spend-before-leaving-your-terminal-318.md

Stop Runaway Agent Spend Before Leaving Your Terminal

Stop Runaway Agent Spend Before Leaving Your Terminal

The expensive agent run rarely starts with an ambitious prompt. It starts with “fix this test,” followed by another tool call, another retry, and another copy of the same growing transcript. You switch editor tabs; the loop keeps buying tokens. Before leaving an agent unattended, spend ten minutes making its stopping rules explicit.

This setup uses Fluxo, which gives you one key and one base URL, https://api.fluxorelay.com, for Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, Grok, and more. It works with Claude Code, Codex, Gemini CLI, and any OpenAI SDK. The limits below belong in your agent runner or wrapper; do not assume every client exposes identical controls.

Minutes 0–2: Give One Task a Small Spending Envelope

If you want to follow along, create a Fluxo account and keep your first experiment deliberately small. Store the key in your environment or secret manager, not in a repository. Configure your client's documented base-URL setting, including any path its documentation requires, rather than guessing endpoint paths.

Write down a starting policy before launching the agent. These are example limits, not Fluxo defaults or a guarantee of provider-side enforcement:

  • Allow one active request and disable parallel agent workers initially.
  • Permit six model requests total, including retries and subagent requests.
  • Set each response's supported output-token limit to eight hundred tokens.
  • Stop launching requests after three minutes of total elapsed runtime.
  • Use a fifty-cent task budget with conservative preflight cost checks.

For a small test-repair task, start with Claude Haiku 4.5 or DeepSeek V4 Flash. Check current rates and use the model identifier shown in Fluxo's documentation; a display name is not necessarily an API identifier. Avoid automatic escalation to a more expensive model during this first run.

Minutes 2–5: Check the Budget Before Every Request

Put one gate around every model invocation, including calls made by retry handlers and delegated agents. Before dispatch, check the deadline, remaining request count, and remaining budget. Reserve a conservative estimate for the next request, then refuse dispatch if that reservation would exceed the task budget.

Estimate the complete outgoing context, not just the newest user message. Include system instructions, tool definitions, previous messages, and tool results. Account for the configured output allowance and any separately billed token categories. Use an upper-bound estimate without assuming cache discounts. If you cannot bound a billable category, require approval instead of claiming a hard dollar cap.

After each response, reconcile the reservation against available usage and applicable rates. Fluxo bills per real token, including cache reads and writes, with no monthly fee. Follow the documented usage-field semantics so cached tokens are neither ignored nor counted twice. When usage is missing, retain the reservation and stop for review rather than treating that request as free.

Minutes 5–7: Break Repeated Tool Calls and Retry Chains

A spending gate limits damage; a progress check catches the loop earlier. Track the tool name, normalized arguments, exit status, and a digest of the result. Stop when the same failing action repeats twice without a relevant file change. Ask for human input instead of sending the identical failure back indefinitely.

  • Allow one transient-error retry, and count it toward every limit.
  • Disable hidden SDK retries or include them in your accounting.
  • Truncate oversized tool output before adding it to model context.
  • Require approval before spawning subagents or increasing the task budget.

A timeout is not proof that billing stopped. The upstream request may already be processing. Avoid immediately issuing a replacement request when cancellation or completion status is uncertain.

Minutes 7–10: Test the Stop Path in Your Terminal

Mock a failing tool and a model that requests it repeatedly. Confirm that the wrapper exits with a clear reason, leaves the working tree intact, and prints requests used, estimated cost, reported usage, and unresolved reservations. Then run one real, small task and compare your log with available billing records.

Keep referral earnings separate from this calculation: Fluxo users earn a referral share when people they invite pay, but that is not a task-budget control. If anything in the billing data is unclear, leave a note on the contact page after signing in. The useful end state is simple: your agent stops predictably, even when you stop watching.

Next in the buffer

Sign up in a minute and get credit to try every model mentioned here. Sign up →