PythiloLesson 9 of 12 · progress kept in this browser

Practice in Lab: Stop an agent loop before exceeding its execution budget →

Stop an agent loop before exceeding its execution budget

A tool-using agent can repeatedly request work even when it is no longer making progress. Limit execution steps and declared token cost outside the model, and stop before admitting an operation that exceeds either bound. A refusal or malformed operation must not be treated as a successful tool result.

This exercise replays a fixed sequence of proposed costs. It isolates admission accounting from model generation, authorization and actual execution. Production loops must also budget latency, retries and side effects, and distinguish a proposed cost estimate from measured usage returned by a provider.

costs=[3,4,5]
used=0
for cost in costs:
    if used+cost>8:break
    used+=cost
print(used)
Your task

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.