Fluxio
← 返回博客

Stop Runaway Agent Loops Before Surprise Bills

token 经济账EN

An agent should stop when it finishes a task, not when someone notices the bill. Build cost controls into the execution path rather than relying on instructions asking the model to be economical. This walkthrough uses an illustrative support agent that searches documentation, checks an account, and drafts a response.

§1 Step 1: Define a Budget for Each Agent Run

Start with an explicit policy. For this example, choose a $0.50 run budget, six model calls, eight tool executions, two retries across the entire run, and a 60-second deadline. These are illustrative engineering limits, not provider prices or universal recommendations.

Assign a run identifier and store its remaining budget in application-controlled state. Require every worker, retry, and child agent to use that same record. Do not let a handoff or resumed session create a fresh allowance.

§2 Step 2: Enforce Limits Outside the Model

Check the policy before every model request and tool execution. If any limit is exhausted, return a controlled stop with the partial result. The OpenAI Agents SDK exposes max_turns, but its definition counts model invocations, including associated tool calls; it is not a separate tool-execution budget. Keep your own counters for those additional limits. ([openai.github.io](https://openai.github.io/openai-agents-python/ref/run/))

  • – Reject new work after the deadline or call limit.
  • – Require explicit approval before raising an exhausted allowance.
  • – Persist counters so restarts cannot reset the run.

§3 Step 3: Reserve Spending Before Dispatch

Before sending a request, estimate its maximum charge using the selected route’s current pricing, estimated input, configured output ceiling, and applicable billing categories. Reserve that amount atomically from the shared budget. Reject requests whose reservation would exceed the remaining allowance.

After completion, replace the reservation with reported usage and release the unused balance. Keep unresolved requests reserved until reconciled. Treat this as conservative admission control, not a guaranteed invoice ceiling unless every billable category has a verified upper bound.

The OpenAI Agents SDK tracks requests, input tokens, output tokens, and per-request breakdowns. Third-party usage reporting varies, so validate the exact backend before trusting its totals. ([openai.github.io](https://openai.github.io/openai-agents-python/usage/))

§4 Step 4: Bound Retries and Detect Repeated Work

Use a single retry allowance across application code, SDK behavior, and background jobs. For transient failures, honor Retry-After when supplied; otherwise use bounded exponential backoff with jitter. Do not retry billing or exhausted-quota errors without correcting their cause. OpenAI’s error guidance explicitly distinguishes these cases and warns developers to account for SDK retries. ([platform.openai.com](https://platform.openai.com/docs/guides/error-codes))

Fingerprint each tool name and normalized arguments. In this walkthrough, stop after the same failed operation repeats twice without new evidence. For tools that change external state, use idempotency controls where supported and check uncertain outcomes before retrying.

§5 Step 5: Connect Streaming to Real Cancellation

Fluxio fits teams seeking a streaming-first, pay-per-token relay with one key across providers. Its OpenAI-style client base is https://api.fluxioapi.com/v1. Keep the budget controller in your application rather than assuming the relay supplies these safeguards.

Wire the interface’s Stop action to backend cancellation, upstream request abortion where supported, and cancellation of queued child work. Test the complete path rather than merely hiding incoming text. An interrupted OpenAI Chat Completions stream may never deliver its final usage chunk, so missing usage must remain unresolved, not become zero spending. ([platform.openai.com](https://platform.openai.com/docs/api-reference/chat/create))

§6 Step 6: Reconcile Usage Across Every Agent Branch

Record run identifiers, parent-child relationships, model routes, request identifiers, reported tokens, reservations, retries, and stop reasons. Aggregate the whole run: the OpenAI Agents SDK includes model calls producing tool calls and handoffs in its usage totals. ([openai.github.io](https://openai.github.io/openai-agents-python/usage/))

Alert before exhaustion, but make admission checks enforce the actual stop. Add tenant-level and daily application budgets so many individually compliant runs cannot bypass your broader spending policy.

§7 Step 7: Test Runaway Loops Before Production

Simulate repeating tool failures, simultaneous child requests, worker restarts, missing usage, and cancellation during streaming. Assert that reservations survive restarts, parallel workers cannot overspend the shared allowance, and budget exhaustion launches no additional model call. Return a useful partial answer without commissioning another paid summary.

Sign up for Fluxio.

创建 Fluxio 账号 →

顺流再读几篇