Build a bounded tool-calling loop with safe execution
Implement an agent loop supporting multiple tool calls, Pydantic arg validation, timeouts, and a hard max_steps cap.
Model Answer & Explanation
Loop up to max_steps (e.g. 6). On each model turn, if stop_reason is end_turn return final text; if tool_use, validate tool name against an allowlist and args against the tool’s Pydantic model, run with asyncio.wait_for timeout, catch exceptions into structured {error, retryable} tool_result blocks so the model can adapt, and inject a 'summarize what you have' prompt if max_steps is reached.