← All questions
Agent Protocols & ToolsMedium· Tool Calling

How do you handle errors and hallucinated tool calls?

Three separate failure modes get lumped together as “tool calling errors,” but each needs its own fix:

  • Hallucinated tools — the model invents a tool name that was never registered, like calling search_internet when only search_web exists. The fix is validating the requested tool name against your actual registry before attempting anything, and when it doesn’t match, telling the model exactly what’s available so it can correct itself on the next turn rather than failing silently.
  • Wrong arguments — right tool, malformed or invalid parameters. Schema validation (a Pydantic model or equivalent) catches this before the tool ever runs, rejecting bad input at the boundary.
  • Runtime failures — the tool itself throws or times out even with a valid call. Catch the exception, return a structured error back to the model instead of letting the process die, and let it decide whether to retry or try a different approach.

Across all three, a hard retry limit matters as much as the individual checks — a model that keeps failing the same tool call in slightly different ways, without a cap, will just keep trying rather than escalating or giving up gracefully.

Code playground (Python)