An agent loop needs an external stop even when every individual response is valid. Repeated tool requests can continue indefinitely. Charge the next action before allowing it, preserve the distinction between a finished answer and a budget stop, and test the cap at its exact boundary.
Use a fresh scripted client to run at most case.max_steps responses and at most case.budget fake token units, charging each response usage input_tokens plus output_tokens. Stop before the next scripted response if its charge would exceed budget. Stop on the first completed response with no function_call item. Return steps, used and done.
The notebook executes your Python against an original local mock. Use the supplied case rather than copying visible values. The hidden checks change identities, revisions and boundary inputs; no provider endpoint or external tool is contacted.
An example implementation of the function contract.