Robust tool execution requires handling several failure modes: malformed arguments, unknown tools, execution failures, and timeouts.
Key principle: Always return errors as JSON in the tool result — never raise exceptions that terminate the loop. The model can read error messages and adjust its approach, ask the user for clarification, or try a different tool.
Tools fail; the agent recovers
Network blip during get_weather? Don't crash the whole agent — return {"error": "network", "detail": "timeout after 5s"} as the tool result. The model is good at recovery: it might retry, fall back to a different tool, or tell the user what failed. Crashing means none of those options are available.
Security note: a Python traceback can carry API keys, internal hostnames, file paths, even partial query strings. Strip or summarize before returning to the model — the model output is shown to the user. Same hygiene as any user-facing log.