WRITE · TRACE · UNDERSTAND

Stop an agent loop before exceeding its execution budget

Implement admitted_steps(costs,max_steps,max_tokens), returning (number_admitted,tokens_used).

Read lesson: Stop an agent loop before exceeding its execution budget →

Your experiment

Implement admitted_steps(costs,max_steps,max_tokens), returning (number_admitted,tokens_used). Nonnegative integer costs and budgets only. Stop at the first exceeded limit; zero-cost steps still consume a step.

Python runs in your browser. Your draft stays on this device.

What to look for

A deterministic replay proves the admission arithmetic only. Actual tools still need authorization and outcome handling; provider usage, elapsed time and retries must be reconciled with the budget after every real operation.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Stop an agent loop before exceeding its execution budget works

The idea

An agent that calls a model in a loop can keep running long after it stops making progress, and every step costs tokens. A budget guard decides before each step whether that step still fits. Checking afterwards is too late: by the time you notice, the limit has already been passed.

admitted_steps(costs, max_steps, max_tokens) walks the step costs in order under two limits, a step count and a token total. A step is admitted only if count < max_steps and used + cost <= max_tokens. The first step that fails either test ends the loop, and cheaper steps after it are not considered. The function returns how many steps were admitted and how many tokens they used.

How it works

  1. Raise ValueError unless every cost and both limits are nonnegative integers, with booleans not accepted as integers.
  2. Start with count = 0 and used = 0.
  3. For the next cost, stop if count >= max_steps or if used + cost > max_tokens.
  4. Otherwise admit the step by adding one to count and its cost to used.
  5. Return (count, used) when the costs run out or a limit stops the loop.

What it costs

Per step
O(1) check
Whole run
O(n) for n costs
Memory
O(n) copy of costs

Each step needs one comparison per limit and two additions, which is negligible next to the model call that the guard controls.

Where it is used

  • LangChain's AgentExecutor stops a run when max_iterations or max_execution_time is reached.
  • Anthropic's Messages API requires a max_tokens value that caps the output of each model call.
  • Amazon API Gateway throttles requests with a token bucket, admitting a request only if a token is available before it runs.

Watch out for

  • Checking the budget after running the step, which reports the overrun but does not prevent it.
  • Skipping an expensive step and carrying on with cheaper ones, when the contract stops at the first step that does not fit.
  • Treating zero-cost steps as free: they still count toward max_steps, which is what stops a loop that spends no tokens.