Skip to content
mentoor

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?

Choose one answer

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

  1. Validate arguments first. Check types, ranges and ids before doing any work. The schema is advice, not enforcement.
  2. Catch the failure and return an error result. Claude has an is_error flag on the tool result. OpenAI and Gemini take the error text as the output. Either way, the call id still goes back with it.
  3. 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.
  4. 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.
  5. Leave secrets and stack traces out. Messages go into the model's context and may reach the user.
  6. 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?

Choose one answer

Check yourself · 3 of 3

Which error message is most useful to the model?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic