Stop the Loop Inside a Dollar Budget
max_turns is a count; your bill is in dollars. Stop the agent loop when the money runs out.
Implement run_within_budget(llm, question, tools, max_cost_usd, max_turns=6). Each turn, ask the model for JSON: either {"tool": name, "arguments": {...}} to act, or {"final": "..."} to answer. Run the tool, feed the result back as the next prompt, and repeat. After every call, read Meter.of(llm).cost_usd and stop if it is past max_cost_usd.
Return {"answer": str or None, "calls": int, "cost_usd": float, "stopped": "done" | "budget" | "turns"}.
The catch: check after every call, not before. You cannot know what a call costs until it returns, so a budget of a tenth of a cent is exceeded by the first call, and the correct behaviour is to stop then, having made one call, not zero.
The model is real, so the tests check: the cost never exceeds the budget by more than one call, a tiny budget stops after exactly one, a generous one finishes with the right number.