Relay365Multi-model API relay desk
← All desk bulletins
Spend watchEN142 desk reads

Stop Runaway Agent Loops Before Costs Climb

An agent that keeps trying is not necessarily making progress. Before leaving one unattended, decide how much work it may attempt, how much you are willing to spend, and what should stop it. Follow these eight steps to build those boundaries into your first workflow.

Define a Finish Line for Each Agent Task

1. Give the agent one bounded assignment, such as updating a function and running its relevant tests. Specify what counts as success and when it must ask for help. Agent runners can continue through model calls and tool interactions until a stopping condition is reached, so avoid instructions such as “keep improving until everything is perfect.” ([openai.github.io](https://openai.github.io/openai-agents-python/running_agents/?trk=public_post_comment-text&utm_source=openai))

Write the finish line somewhere your application can check. A passing test, a completed output file, or an explicit request for human review is more useful than the agent simply declaring itself finished.

Choose a Spending Allowance Before Starting

2. Pick a small amount you are comfortable spending on one supervised experiment. Treat it as your own operating allowance, not a claim about typical task costs. Check the selected model’s current rates and any separately billed tools before running.

Distinguish a notification threshold from an enforced spending limit. Verify what your provider actually blocks, when enforcement occurs, and whether requests already underway can finish. Leave headroom rather than assuming a displayed balance or alert provides an exact cutoff.

Enforce Several Independent Run Limits

3. Set limits for model turns, tool executions, elapsed time, and output size in your application or runner. For example, the OpenAI Agents SDK provides a maximum-turn setting and raises an exception when that limit is exceeded. Handle that exception by stopping and saving progress, not automatically starting again. ([openai.github.io](https://openai.github.io/openai-agents-python/running_agents/?trk=public_post_comment-text&utm_source=openai))

Do not confuse a turn limit with a dollar limit. Choose conservative starting values, then adjust them after observing a successful run. Require approval before expanding the allowance.

Keep Retries Inside the Original Allowance

4. Give retries a separate, finite counter. Retry only failures you have classified as temporary, and stop repeated authentication errors, invalid inputs, or unchanged failing tests for inspection. Review SDK, application, and job-queue retry settings together.

Preserve counters when a task resumes. OpenAI’s runner lifecycle guidance explicitly calls for retaining the maximum-turn setting and current turn in saved run state. Apply that same principle to your spending allowance: restarting a process must not silently grant a fresh budget. ([github.com](https://github.com/openai/openai-agents-python/blob/main/.agents/references/runner-lifecycle.md?utm_source=openai))

Stop Repeated Actions Without New Evidence

5. Record a compact signature of each action: the tool name, normalized arguments, and a summary of its result. Set a repetition threshold and pause when the agent repeats the same unsuccessful action without receiving new information.

Ask for a short handoff explaining what failed and what decision is needed. Keep permission to resume outside the agent’s control. A sentence in the prompt asking it to “avoid loops” should supplement executable checks, not replace them.

Measure Usage Across the Whole Task

6. Log a task identifier, model, request count, token usage, tool activity, and estimated cost. The OpenAI Agents SDK exposes request and token totals for runs; use available usage records rather than estimating consumption from the visible answer alone. ([openai.github.io](https://openai.github.io/openai-agents-python/usage/?utm_source=openai))

Keep tool results concise and pass only relevant history forward. Previous conversation messages may be supplied again as input on later runs, increasing input-token usage. Track child tasks under the parent allowance instead of treating each worker as an independent spending account. ([openai.github.io](https://openai.github.io/openai-agents-python/usage/?utm_source=openai))

Connect a Relay Without Assuming Cost Protection

7. Relay365 can fit when you want pay-as-you-go access to multiple model families through an existing development workflow. It works with Claude Code, Codex, Gemini CLI, and OpenAI SDK clients. Its API base is https://api.api365relay.com; OpenAI clients add /v1.

Keep the safeguards in your application regardless of the relay you choose. If that setup suits your workflow, create a Relay365 account and begin with one supervised, tightly bounded task.

Test the Shutdown Before Leaving Agents Unattended

8. Deliberately trigger a repeated tool failure and a turn-limit stop. Confirm the application stops scheduling work, saves useful progress, and requires approval to resume. Test any child-worker cancellation separately.

Finally, reconcile your logs with billed usage before increasing concurrency. Expand automation only after you have seen both a successful completion and a controlled failure. The goal is not unlimited persistence; it is useful progress within an allowance you chose.

Further desk bulletins

Curious how it runs on your own code? Sign up →