Article · AI agent debugging

How to debug an AI agent that calls the wrong tool

Trace the first incorrect decision from tool selection through the final failed answer.

Intermediate8 minute readTool debugging

When an AI agent calls the wrong tool, the final error is often less useful than the first incorrect decision. A trace lets you follow the agent from its plan through tool selection, arguments, responses, and the final failure.

This tutorial uses a small failed run to show a repeatable debugging workflow for LLM and agent developers.

Core idea: find the first decision that diverged from the task, then turn it into a regression test.

Recognize the problem before opening the trace

A wrong-tool failure usually appears as a plausible but irrelevant answer. You may see a weather response for a billing question, a search result for a request that needed a database lookup, or a successful API call that does not contain evidence for the user’s task. Because the selected tool can return a technically valid response, the error may look like a data-quality or final-answer problem.

Use these signals to form a first hypothesis:

  • The tool call succeeds, but the result belongs to the wrong domain.
  • The final response says the agent could not complete the task even though a tool returned data.
  • The tool arguments are valid for one tool but do not represent the user’s request.
  • The plan names one capability, while the next execution step uses another.

Example: a CRM question takes the weather path

Suppose a user asks, “Who owns the Acme Corp account?” The expected path is a CRM lookup followed by an answer with the account owner. In this failed run, the planner correctly identifies the task as a CRM lookup, but the next step calls weather_api instead. The weather tool returns “Sunny, 20C in Phoenix,” which is valid weather data but irrelevant evidence for the user’s question.

The run contains four steps and two failures. The final answer is not the best place to begin: it is only where the earlier tool-selection mistake becomes visible. The useful diagnosis is the first point where the execution stops matching the task.

Expected vs. observed: expected CRM lookup → account owner; observed CRM plan → weather_api → irrelevant weather result → aborted answer.

1. Follow the path from plan to result

  1. Start with the request. The CRM owner question establishes the domain and expected evidence.
  2. Check the plan. “Plan CRM lookup” shows that the agent initially understood the task.
  3. Compare the next tool to the plan. The transition to weather_api is the first actionable mismatch.
  4. Inspect the returned data. “Sunny, 20C in Phoenix” confirms that the tool ran, but it cannot answer the CRM question.
  5. Check the final behavior. The aborted answer is a downstream symptom, not the root cause.

2. Use the reasoning graph to confirm the diagnosis

TraceProbe keeps the run in one local view so you can inspect the summary, phase timeline, execution details, and reasoning graph without reconstructing the sequence from scattered logs.

TraceProbe reasoning graph showing the agent's incorrect weather tool selection

In the screenshot, the graph makes the diagnosis explicit:

  • Step 1 records “Plan CRM lookup”, so the initial intent is clear.
  • Step 2 shows “Call weather tool by mistake”, which is the first wrong action.
  • Step 3 is marked ROOT and Primary root-cause path; its status detail says the weather tool was used for the CRM task.
  • The raw output and final answer show the consequence: weather data is passed forward, then the run aborts.

This is how to use the tool diagnostically: follow the graph to the highlighted root, open the step details to verify the evidence, and only then decide what code or prompt needs to change. The graph tells you where to look; the step details tell you what was actually sent and returned.

3. Verify the root cause in TraceProbe

Use the tabs in order so you do not mistake a downstream symptom for the cause:

  1. Summary: confirm the outcome, failure count, and whether the run is worth comparing with a known-good run.
  2. Phase Timeline: find the first unusual transition or failed step, rather than starting at the final answer.
  3. Reasoning Graph: follow the path from the plan to the selected tool and look for the highlighted root node.
  4. Step Details: inspect the selected tool, arguments, status detail, raw input, and raw output. In this example, those fields confirm that weather data was produced for a CRM task.

If the plan itself is wrong, investigate the prompt, routing rules, or retrieved context. If the plan is correct but the next tool is wrong—as it is here—investigate tool selection, tool descriptions, and routing validation.

4. Choose a fix that matches the evidence

For this example, changing the final-answer wording would hide the symptom without correcting the selection error. Fix the earliest incorrect step instead:

  • If the planner chooses the wrong domain, improve the tool descriptions, routing prompt, or retrieved capability context.
  • If the planner is correct but execution switches tools, validate that the selected tool matches the plan before calling it.
  • If the tool is correct but the request is malformed, validate arguments against the tool schema and reject the call early.
  • If an irrelevant result still gets through, check result relevance before allowing it to ground the final answer.
  • Log the plan, selected tool, arguments, and result together so the next trace preserves the same evidence.

5. Rerun and compare the repaired trace

Keep this failed trace as a deterministic fixture. For the Acme Corp request, the regression test should assert that the agent selects the CRM tool, that the arguments contain the account lookup, and that an irrelevant weather response cannot be accepted as task evidence. Run the fixture again after the fix and compare the new trace: the wrong-tool node should disappear, and the final answer should be grounded in the CRM result.

A successful repair should be visible in the trace, not only in the text response:

  • The plan and selected tool should both refer to the CRM lookup.
  • The tool arguments should identify Acme Corp or its account ID.
  • The tool output should contain the account owner.
  • The final answer should cite that CRM result and complete the original task.

Summary

The fastest way to debug an agent is to identify the first wrong decision, not just the last visible error. A structured trace makes that decision inspectable and gives the team a concrete regression case to prevent it from returning.

Find the first wrong decision.

Use TraceProbe to inspect tool calls, reasoning, and the next experiment locally.

Get TraceProbe