- tools that crash (uncaught exceptions), and
- an unbounded loop that can retry forever (burning API credits).
safe_agent.py) so it behaves safely in production: tools return errors as results, and the main loop is bounded with a graceful final attempt.
Environment
- Working directory:
/root/code - File:
safe_agent.py - Prereqs: Python, virtual environment, OpenAI SDK (pre-installed)

- File to edit:
safe_agent.py - Start with this minimal agent: OpenAI client, a
check_calendartool,execute_tooldispatcher, and awhile Trueloop. This is intentionally vulnerable so you can see the failure modes.
Failure mode #1 — tool crashes
- Problem: if a tool raises an exception, the entire script crashes, the user sees a traceback, and the conversation ends.
- Fix: catch exceptions in
execute_tooland return a descriptive error string. The model will receive"Error: ..."as the tool output and can adjust behavior (try another tool, explain the problem, notify the user).
execute_tool to return an error string on exceptions.
Patch for safe tool execution
flaky_tool into the tool list you provide the model (for example, in the system or tool description messages) and ask the agent to use it. The script should not crash — instead the model will receive a result beginning with "Error" and can recover.
Return errors to the agent (as strings) rather than raising them from
execute_tool. This lets the model handle failures gracefully and maintain conversation continuity.- Problem:
while Trueexits only when the model signals finish (finish_reason == “stop”). If tools are flaky or the model never returns a final finish, the agent can retry forever and burn credits. - Fix: replace
while Truewith a bounded loop (e.g.,for iteration in range(MAX_ITERATIONS)). Use afor-elseclause: theelseblock runs only when the loop exhausts iterations without abreak. In this case, append a prompt asking for a best-effort answer and make one final completion call.
Do not use unbounded retry loops in production agents. Always cap retries (e.g.,
MAX_ITERATIONS) and provide a clear final attempt so users get a best-effort response instead of the process running indefinitely.safe_agent.py
- The minimal improvements to apply:
- Add
flaky_toolfor testing. - Make
execute_toolreturn errors (string) instead of raising. - Replace
while Truewith a bounded loop and usefor-elseto perform a final completion if the agent never finishes.
- Add
- Ask the agent to use
flaky_tool. You should see:- iteration logs increment,
execute_toolreturning"Error: Service unavailable. Try a different approach.",- the model receives the error string and should either try a different tool or produce a best-effort answer when iterations are exhausted.
- Verify that the process never crashes with a stack trace from
flaky_tool.
- Returning errors as tool results keeps the conversation alive and allows the agent to recover.
- Bounded loops prevent runaway API usage and give you a chance to provide a final best-effort response.
- Together they make your tool-calling agent resilient and production-ready.
- OpenAI API docs
- Python — exception handling: https://docs.python.org/3/tutorial/errors.html
- Agent design patterns and structured tool calls — prefer structured outputs (JSON) for reliable parsing