How to Cap the Number of Steps an Agent Loop Can Take

Tested by TNT Review — record TNT-TEST-2026-10-05-029, 5 October 2026. We ran a loop that finishes normally and one that deliberately never finishes; both matched expected behaviour exactly. See Verification.

The problem

An agent loop that keeps acting until some condition is met has a quiet failure mode: a bug in the stopping condition, or a task that’s genuinely impossible, means it never stops on its own. Without an outer limit, that’s either a frozen process or an unbounded number of API calls, depending on what each step costs.

Expected outcome

By the end of this guide you will have a loop wrapper that runs your step function until either your real stopping condition is met, or a maximum step count is reached — whichever comes first, with the second case reported clearly, not silently.

Requirements and costs

  • Node.js (version 18 or newer).
  • Cost: free. No dependencies.

Step-by-step implementation

  1. Download the starter from the link below.
  2. Write a step function that takes the current state and returns the next one, and an isDone function that checks whether you’re finished:
    const result = await runBounded(
      (state) => doOneStep(state),
      (state) => state.finished === true,
      { maxSteps: 10, initialState: {} }
    );
  3. Check result.done — if it’s false, the loop hit its step cap without finishing; handle that explicitly (log it, alert, or escalate) rather than treating the last state as if it were a real result.

A real example

// A loop that counts up to 5 and stops -- finishes well within its cap of 10
{ "done": true, "steps_taken": 5, "state": 5 }

// A loop whose stopping condition is never true (a stand-in for a buggy agent)
{ "done": false, "steps_taken": 10, "state": 10, "reason": "hit maxSteps (10) without reaching done" }

Verification and troubleshooting

What we tested, on 5 October 2026 (record TNT-TEST-2026-10-05-029): a loop counting up to 5 with a cap of 10 correctly finished at exactly step 5 (done: true). A loop whose isDone always returns false correctly ran exactly 10 steps (not 11, not forever) and reported done: false with a clear reason — confirming the cap is a hard, reliable stop, not just a suggestion.

Troubleshooting:

  • The loop stops too early on real tasks. Raise maxSteps — start with real headroom above how many steps your task normally needs, not the exact typical count.
  • You need to know WHY it never finished, not just that it didn’t. Log or return the state at each step (the step function receives the current step number as its second argument) so you can inspect what each step actually did.
  • Each step is itself slow or costly. Combine this with a timeout wrapper on each individual step, so a single hung step doesn’t consume the entire step budget waiting.

Free resources

The download contains the bounded loop wrapper and a runnable demo showing both outcomes.

Download the bounded loop starter (ZIP, 1 KB)

Is there a paid version?

Not yet. This is the complete method. TNT Review has sold nothing to date and has no checkout, so any price we discuss in future should be read as a hypothesis, not an offer.

Leave a comment