Skip to main content

How to Tell Claude a Tool Failed with is_error

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Tools fail: a record isn't there, a service is down, an argument is wrong. When that happens, Claude still needs an answer to its tool call, or it can't go on. The is_error flag lets you send the failure back, so Claude can try again or explain the problem. This guide makes a library lookup fail, with real output from anthropic 2.14.0.

Syntax​

Syntax
{
"type": "tool_result",
"tool_use_id": <the tool_use block's id>,
"content": "<what went wrong>",
"is_error": True,
}
PartWhat it means
tool_use_idThe id of the call that failed, as in any tool result
contentThe error message Claude reads
"is_error": TrueMarks the result as a failure instead of a result

Anthropic's documentation: "When a client tool fails or a call is invalid, send a tool_result block with is_error set to true. Claude might then call the tool again with a new tool_use block, so limit how many retries your loop allows."

What It Costs​

The Claude API is paid: Claude Opus 5.5 costs $4 per million input tokens and $20 per million output tokens. Each retry is another request that resends the whole conversation. The run below made three calls, 1,527 input and 289 output tokens in all, about $0.012.

Prerequisites​

  • A Claude API key, in a .env file as ANTHROPIC_API_KEY=....
  • The Anthropic Python SDK in a uv project. This guide uses anthropic 2.14.0, added with uv add anthropic, on Python 3.15.2.

Step 1: Write a Tool That Can Fail​

Create tool_error.py. The tool finds which shelf a book is on, and raises an exception when the library doesn't have it:

tool_error.py
import anthropic

client = anthropic.Anthropic()

SHELVES = {"the glass orchard": "Shelf F-12", "rivers of salt": "Shelf N-04"}


def find_book(title: str) -> str:
shelf = SHELVES.get(title.lower())
if shelf is None:
raise LookupError(f"There's no book called {title!r} in the catalogue.")
return shelf

The error message says exactly what went wrong, with the title Claude asked for. Claude reads it, so a clear message helps it decide what to do next.

Step 2: Describe It to Claude​

Add the tool's definition and the question. The question asks for a book that isn't in SHELVES:

tool_error.py
tools = [
{
"name": "find_book",
"description": (
"Find which shelf of the library a book is on, by its title. "
"Use it whenever someone asks where to find a book."
),
"input_schema": {
"type": "object",
"properties": {
"title": {"type": "string", "description": "The book's title."},
},
"required": ["title"],
},
}
]
messages = [{"role": "user", "content": "Where can I find The Silent Orchard?"}]

Step 3: Send Failures Back with is_error​

Add the tool loop. It runs each call inside try, and turns a LookupError into a result with is_error:

tool_error.py
response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=tools, messages=messages
)
while response.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type != "tool_use":
continue
try:
shelf = find_book(**block.input)
result = {"type": "tool_result", "tool_use_id": block.id, "content": shelf}
except LookupError as error:
result = {
"type": "tool_result",
"tool_use_id": block.id,
"content": str(error),
"is_error": True,
}
print(f"Tool: {block.name}({block.input}) sent back {result['content']!r}")
results.append(result)
messages.append({"role": "user", "content": results})
response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=tools, messages=messages
)

print("Claude:", response.content[-1].text)
  • When find_book works, the result carries the shelf, as usual.
  • When it raises LookupError, except catches it, and the result carries str(error), the error message, with "is_error": True.
  • Either way, the call gets a result, and the loop goes on: the script doesn't stop at the exception.

The complete script:

tool_error.py
import anthropic

client = anthropic.Anthropic()

SHELVES = {"the glass orchard": "Shelf F-12", "rivers of salt": "Shelf N-04"}


def find_book(title: str) -> str:
shelf = SHELVES.get(title.lower())
if shelf is None:
raise LookupError(f"There's no book called {title!r} in the catalogue.")
return shelf


tools = [
{
"name": "find_book",
"description": (
"Find which shelf of the library a book is on, by its title. "
"Use it whenever someone asks where to find a book."
),
"input_schema": {
"type": "object",
"properties": {
"title": {"type": "string", "description": "The book's title."},
},
"required": ["title"],
},
}
]
messages = [{"role": "user", "content": "Where can I find The Silent Orchard?"}]

response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=tools, messages=messages
)
while response.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type != "tool_use":
continue
try:
shelf = find_book(**block.input)
result = {"type": "tool_result", "tool_use_id": block.id, "content": shelf}
except LookupError as error:
result = {
"type": "tool_result",
"tool_use_id": block.id,
"content": str(error),
"is_error": True,
}
print(f"Tool: {block.name}({block.input}) sent back {result['content']!r}")
results.append(result)
messages.append({"role": "user", "content": results})
response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=tools, messages=messages
)

print("Claude:", response.content[-1].text)

The complete tool_error.py script in a macOS-style code window: find_book raises LookupError for a title that isn't on the shelves, and the tool loop catches it and sends the error message back in a tool_result block with is_error set to True.

Step 4: Run It​

Terminal
uv run --env-file .env tool_error.py
Output
Tool: find_book({'title': 'The Silent Orchard'}) sent back "There's no book called 'The Silent Orchard' in the catalogue."
Tool: find_book({'title': 'Silent Orchard'}) sent back "There's no book called 'Silent Orchard' in the catalogue."
Claude: I couldn't find *The Silent Orchard* in the library's catalogue. I also searched for "Silent Orchard" without "The," and that didn't turn up anything either.

Some possible reasons:
- **The title might be a little different.** For example, it could be "The Quiet Orchard" or "Silent Orchards."
- **The library might not have it.** If so, the staff may be able to order it or get it from another library.

If you know the author or remember any other part of the title, I can search again.

The output in a terminal: find_book fails for The Silent Orchard and again for Silent Orchard, and Claude explains that it couldn't find the book, that it also tried the title without The, and suggests why.

The first call failed, and Claude read the error. Then it did what Anthropic's documentation says it might: it called the tool again, this time without "The". When that failed too, it stopped and told the user what it had tried, instead of making up a shelf. The * and ** marks are Markdown: Claude formats replies unless you ask for plain text.

Common Mistakes​

MistakeWhat happensDo this instead
Letting the tool's exception escape the loopThe script stops with a traceback, and Claude never hears about the failureCatch it and send a tool_result with "is_error": True
Skipping the result of the call that failed400: `tool_use` ids were found without `tool_result` blocks immediately afterSend a result for every call, failed or not
A vague message such as "failed"Claude can't tell what went wrong or what to try nextSay what went wrong, as find_book does
A loop with no limitClaude may keep retryingLimit the retries, as Anthropic's documentation advises, for example with a counter

Troubleshooting​

SymptomLikely causeFix
Claude calls the same tool several times in a rowThe earlier calls failed and Claude is trying againExpected; read the errors you sent back, and limit the rounds
The script stops with a LookupError tracebackThe exception wasn't caught inside the loopWrap the tool call in try and except

FAQ​

Should I send a stack trace as the error?​

No. Send a short message Claude can act on. Anthropic's tip is to write "instructive error messages" that say what went wrong and what Claude should try next.

What if Claude's call itself is invalid, like a missing argument?​

That's a failure too: send it back with is_error. To stop such calls from happening at all, Anthropic's documentation points to strict tool use, "strict": True on the tool definition.

Does is_error cost anything extra?​

No. The error message is input like any other text, but every retry is another request.

Conclusion​

Catching LookupError and sending its message back with "is_error": True kept the loop going. Claude read the error, tried the title without "The", and when that failed too, told the user what it had tried instead of guessing.

Additional Resources​