Content

How to debug an AI agent that called the wrong API

Most wrong-call bugs fall into one of three categories: the model picked the wrong method, the arguments were valid but incorrect, or a retry created a duplicate. Each has a different fix.

Chaitrali Kakde

DevRel Engineer

AI AgentOct 24, 2026

Your agent sent a refund to the wrong customer. Your first instinct is to check the prompt. Usually, the prompt is not the problem. The issue is somewhere between the model's tool selection, the argument values it passed, and the execution layer's behavior under error conditions. This guide walks through how to find it.

The three categories of wrong-call bugs

Most wrong-call bugs fall into one of three patterns. The model selected the wrong method: it had access to two similar methods and chose the wrong one. The model selected the correct method but passed incorrect argument values: the customer ID it passed was not the intended customer. A retry created a duplicate: a transient error caused the call to run twice, and the second call produced the wrong outcome.

Start with the execution log, not the prompt

Before looking at the prompt, look at what the execution layer logged. The log should show: the method the agent called, the full argument set it passed, whether any policy was evaluated, and the result. That tells you which of the three categories you are in before you read a single line of the prompt.

If the log shows the correct method with the wrong arguments, the prompt is less likely the cause than the context the agent was given: the wrong customer record, a stale lookup result, a field that was null when the agent expected a value. If the log shows the wrong method, look at the tool surface: how many methods were available that could have plausibly handled the task, and how well the tool descriptions distinguish them.

Wrong method: reduce the surface and sharpen descriptions

If the model is choosing the wrong method from a set of similar ones, the fix is usually one of two things. Reduce the allowlist to only the methods the agent actually needs. If the agent only ever needs to issue refunds, it should not have access to the charge method. The model cannot choose the wrong one from a set of one.

If the allowlist is already narrow and the wrong selection still happens, look at the method descriptions in the tool surface. Models use description text to decide between methods. If two methods have similar descriptions, the model will sometimes pick the wrong one. Make the descriptions explicitly distinct: what this method does, what it does NOT do, and when to use it versus the related method.

Wrong arguments: trace the context, not the call

If the method was correct but the arguments were wrong, the problem is in the context the model was working from. Log the full context window the model received at the point of the call, not just the call itself. Look for: stale data the model read earlier in the task that it used to populate the argument, a lookup that returned the wrong record, or a null value the model substituted with a default.

The most common pattern is a lookup that ran correctly but returned an ambiguous result, and the model used the first result when the second was the intended one. Adding a policy rule that triggers a human approval for high-value or irreversible calls catches these before they execute. The human can verify the arguments before the call runs.

Duplicate calls: add idempotency keys

If the log shows the same call ran twice, the issue is a missing idempotency key. The execution layer retried on a transient error, and the second attempt succeeded, leaving two records. See API idempotency: a complete guide for AI agents for how to construct deterministic keys that prevent this. The short version: derive the key from stable identifiers for the operation, not from a random value or a timestamp, so a retry against the same operation generates the same key and the provider deduplicates it.

FAQ

What if the execution log does not have enough detail to debug?

Run `swy audit network` to see recent calls and their argument values. Run `swy audit policy` to see which policy rules were evaluated and their outcomes. If those do not give enough context, add a policy rule that logs the full argument set for the relevant methods before the next run.

Can I replay a failed call to test a fix?

Use `swy exec <canonical_id>` with the same argument values the log shows to test the call in isolation. This lets you verify that the method and argument combination produces the expected result before the agent runs again. For calls that produce side effects in production, use a sandbox mode (`swy init --mode=sandbox`) to test without affecting live data.

Should I add a policy rule after every wrong-call incident?

For the specific condition that caused the wrong call: yes, if it is representable as an argument-level rule. A refund over $500 going to review is a rule worth adding even if the incident was a $4,000 refund that should have been $40. The rule makes the category of error visible before it executes, rather than after.

More content