DEV Community

Ali Ahmed
Ali Ahmed

Posted on Edited on Originally published at aliahmed.si

Your AI agent thinks its tasks were deleted: how to write tool errors for LLM agents

A human reads "not found" and goes looking. An AI agent reads "not found", concludes the thing is gone, and acts on that conclusion: it recreates it, deletes something else, or tells the user their data was lost. In NanoClaw, an agent framework, a bare "task not found" across sessions led agents to decide their scheduled tasks had been deleted, so they created duplicates. The fix I proposed (#3068, in review) changed no logic. It made the errors and outputs tell the agent where the task actually is and what to do next.

How a tool error shapes an agent's next action in NanoClaw. Before: a bare task not found led the agent to conclude the task was deleted, so it created a duplicate. After PR 3068: the error says the task isn't in this session but in another one, naming its messaging group and status, and the agent uses it without duplicating. Rules for tool errors an LLM reads: say why, not just what; say what to do next; close the believable wrong conclusion; separate tool failures from protocol errors; don't leak internals; hide tools the role can't use.

The fix changed no logic, only what the errors told the agent.

What went wrong?

In NanoClaw, scheduled tasks are scoped to the agent group, not the session. A group can have several sessions (a broadcast channel and an operator console, for example), and a task created in one is visible from the others.

But the task tools gave poor feedback across sessions. When an agent in one session called get_task or update_task on a task that lived in a sibling session (or had completed or been cancelled), the answer amounted to "not found". The agent's mental model, "tasks are mine and per-session", filled in the rest: my task was deleted. So it scheduled it again, which produced duplicated or misconfigured schedules (issue #2992).

How do you fix an agent's wrong mental model?

Three changes, none of which touch the scheduler itself:

  1. Errors that explain. When a task isn't found in the current session, the tool now searches the other sessions in the group. If the task is there, the error names the session and messaging group it lives in and its status (such as completed or cancelled), and tells the agent to look it up with get <id> instead of assuming it was deleted.
  2. Context in normal output. list_tasks and get_task now include the task's origin messaging group (id and name), so the agent can tell which task belongs to which setup without guessing.
  3. Instructions that match the implementation. The scheduling instructions the agent reads now say plainly that task visibility is group-scoped, and explain cross-session listing.

A regression test reproduces the original cross-session scenario from #2992.

What makes a good tool error for an LLM?

I've applied the same rules in the read-only MCP server I built for a CRM:

  • Say why, not just what. "deals are not visible to your role" stops a retry loop. "403" starts one.
  • Say what to do next. "The result is too large: use a smaller limit or more filters" gives the model a concrete next move.
  • Never leave a believable but wrong conclusion open. If "not found" could mean deleted, elsewhere, or never existed, say which one.
  • Separate tool failures from protocol failures. In MCP, a tool that ran and failed returns content with isError: true, which the model reads. A malformed request is a JSON-RPC error. Mixing them up hides the useful message.
  • Don't leak internals. "The query failed" is better than echoing database error text into a model's context, and into its answer to the user.
  • Hide what the user can't use. Tools a role can't call shouldn't appear in tools/list at all.

Why does this matter so much for agents?

With a human user, a vague error costs a support ticket. With an agent it costs an action: a duplicate schedule, a second email, a recreated record. Agents act on the most likely reading of what they see. The tool's output is effectively part of the prompt, so write it like part of the prompt.

More AI engineering write-ups: how to stop an LLM from inventing numbers and the context-compression bug that made agents loop.

For how tools are exposed to agents in the first place, see what the Model Context Protocol is.

Related: how LLM tool calling works and how to evaluate AI agents.


Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article

Top comments (13)

Collapse
 
rodrigo-duarte profile image
Rodrigo Lemela Duarte •

Same failure shape on our side this week. A 409 on a label conflict told the agent nothing about the current label: the label in the backend's details was an object and the MCP layer only forwarded integers. The guidance said "call again to see the label", and the call returned the same bare conflict. The fix, deployed to staging on November 20, makes the 409 carry the current label, the first label, the version and who set it, plus "don't repeat the call". Your third point is the one we got wrong: the instructions promised something the implementation didn't deliver. Did your regression test check the guidance text too, or only the error?

Collapse
 
quantumbreakz profile image
Ali Ahmed •

The test checked the error text for the task ID and a useful hint. The scheduling instructions changed in the same PR, and the test didn’t exercise them. Your staging example is a good case for following the guidance in the test and asserting that the current label and version actually come back through MCP.

Collapse
 
useaifix profile image
AIFix •

The NanoClaw example where a bare "task not found" made agents recreate tasks as duplicates is a great illustration. One tip that pairs well with your fixes: make the create-task tool idempotent, for example by checking for an existing task with the same name or schedule first, so even a wrong guess doesn't produce a duplicate.

Collapse
 
quantumbreakz profile image
Ali Ahmed •

Yep, I’d give create_task an idempotency key and reuse it when retrying the same create request. Two legitimate tasks can share a name and schedule, so I’d use that match to flag a possible duplicate before creating anything.

Collapse
 
agentwareshq profile image
agentwares •

Good framing — the rule I'd add to your list is to make the error verifiable, not just descriptive. An error that says "task exists in another session, check get_task " is already better, but the agent still has to trust the text. When the error names the exact read that would confirm the state, the agent can check instead of inferring — and the check either confirms it or exposes a stale error. That also makes errors part of the tool's contract: if you change an error string, you can change agent behavior just as surely as changing a tool schema, so errors deserve the same regression tests as the happy-path outputs.

Collapse
 
quantumbreakz profile image
Ali Ahmed •

I like the distinction. If the error points to a specific task ID, I’d test the whole recovery path: trigger the error from a sibling session, follow the lookup it suggests, and check that the tool actually returns that task. That would catch the guidance drifting out of sync with the tool.

Collapse
 
jsbry profile image
Joshua Bryan •

One data point for "say where it is and what to do next". We gave an agent a stale-write conflict error that listed the current labels unquoted, one spelled like the version (v3 at version 3), under a prompt telling it not to re-read. It took the label for the version number and resent its old list: the other writer's change was lost in 38 of 48 runs. Same error with the labels quoted: 0 of 48. The wording of an error is part of the API.

Collapse
 
quantumbreakz profile image
Ali Ahmed •

The v3 example makes it easy to see how the model got there. I’d give labels: ["v3"] and version: 3 their own fields, then require the expected version on the write. If the agent still sends a stale update, the server can reject it before it overwrites the other writer’s change.

Collapse
 
jsbry profile image
Joshua Bryan •

Agreed, and it's the half most servers skip. In our lost-update tests the version usually existed: Grafana dashboards carry one and the tool forced the overwrite past it; Kubernetes objects carry a resourceVersion and the server re-applied a stale copy without it. The field was there. Nobody required it on the write.

Collapse
 
izgorodin profile image
Edward Izgorodin •

A uniform 401 is the auth version of the "403" in your first rule. On 17 November I sent four requests to each of six hosted memory APIs: a fake key in the header the vendor documents, the same key in another header, no key, and a made-up path. Two of the six gave the same 401 to all four. The likeliest reading is a bad key, so an agent that wrote the call would ask the user for a new one, and a new key fixes neither a wrong header nor a wrong path. The usual argument for one answer to everything is to tell an untrusted caller as little as possible, and a split between a missing key and a wrong one keeps to it. One of the six answered the fake key with "Invalid API key format" and the request without a key with "API key required", and both describe only what was sent.

Collapse
 
quantumbreakz profile image
Ali Ahmed •

That’s a useful distinction. “API key required” tells the agent to check whether it sent the credential in the expected header, which is a much better first move than asking the user to rotate a key. You can give that hint without revealing whether the requested resource exists.

Collapse
 
paul_irolla_6b5ae261d0224 profile image
Paul Irolla •

Making the error verifiable matters most to me. The agent can check its own fix instead of asking again, and each ask is another turn that resends the session.

Collapse
 
quantumbreakz profile image
Ali Ahmed •

Agreed. If the agent has permission to read the task, give it the task ID and the lookup to run. It can confirm what happened in the same turn and save the question for a decision the user actually needs to make.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.