Topic 3 of 5 · 4 min read
Returning tool errors the model can act on
Goal: Return a failed tool call as an informative error result so the model retries or explains instead of inventing an answer.
When a tool fails, send the failure back as a result the model can read; do not crash the loop and do not return an empty success. The model reacts to what it sees. Give it a clear error and it can fix its input or tell the user the truth.
Check yourself · 1 of 3
A tool cannot find the requested record. What should it return?
Why it matters
Tool calls fail for ordinary reasons: a bad argument, a missing record, a timeout, a permission check. If your code throws, the user gets a stack trace. If it returns an empty string, the model often fills the gap with a confident guess. Both are worse than an honest error message.
How to do it
- Validate arguments first. Check types, ranges and ids before doing any work. The schema is advice, not enforcement.
- Catch the failure and return an error result. Claude has an
is_errorflag on the tool result. OpenAI and Gemini take the error text as the output. Either way, the call id still goes back with it. - Say what failed and what to try. "No order with id 99812. Order ids have 6 digits." lets the model correct itself. "Error" does not.
- Separate fixable from final. A bad date format is fixable by a retry. "The payment service is down" is not, and the model should tell the user instead of retrying.
- Leave secrets and stack traces out. Messages go into the model's context and may reach the user.
- Cap retries. The step limit from the loop still applies.
Before and after
Weak:
tool_result (call_41): ""
assistant: Your order 99812 was shipped on Tuesday.
The tool found nothing, returned an empty string, and the model invented a shipping date.
Better:
tool_result (call_41, is_error: true): "No order found with id 99812. Order ids have 6 digits; check the id with the user."
assistant: I could not find order 99812. Could you check the number?
What goes wrong
- Throwing out of the loop. The user sees a failure and the turn is lost.
- An empty or "ok" result on failure. The model treats silence as success and guesses.
- A vague message. "Something went wrong" gives the model nothing to correct.
- Leaking internals. A raw database error can reveal table names or keys.
Examples
A fixable error
The model calls book_slot(date="next friday"). The tool returns an error: "date must be ISO 8601, for example 2026-12-11." On the next round the model sends 2026-12-11 and the booking succeeds. One clear sentence saved the turn.
A final error
A get_balance call times out against the bank service. The tool returns: "Balance service unavailable. Do not retry; tell the user to try again later." The model apologises and explains, rather than looping or quoting an old balance.
Key points
- Return a failed call as an error result with the same call id; do not throw out of the loop.
- An empty or fake success invites the model to invent an answer.
- Say what failed and what to try next, in a short message.
- Separate errors the model can fix by retrying from ones it should report to the user.
- Keep stack traces, secrets and internal names out of error text.
Common mistakes
- Returning an empty string or "ok" when the underlying call failed.
- Letting an exception escape and end the whole conversation turn.
- Writing messages like "error" or "failed" with no cause or next step.
- Putting the raw exception text, with paths or keys, into the result.
Check yourself · 2 of 3
Why can an empty result after a failed call be dangerous?
Check yourself · 3 of 3
Which error message is most useful to the model?
Practice items, not counted towards your mastery.
Start here
- Tool use overview (Claude docs) (opens in a new tab)DocsFree
Anthropic's reference for tool definitions, the tool_use and tool_result blocks, tool_choice and parallel calls.
- Function calling guide (OpenAI) (opens in a new tab)DocsFree
OpenAI's guide to function definitions, the call loop, parallel calls and tool_choice.
- Function calling (Gemini API docs) (opens in a new tab)DocsFree
Google's guide to declaring functions and returning function responses to Gemini.
More for this topic
- Writing effective tools for AI agents (Anthropic) (opens in a new tab)ArticleFree
Practical advice on tool names, descriptions and error messages that agents can act on.