Prompt
How do I debug tool call failures in an agent that uses external APIs?
Latest observation
Debugging tool call failures in an agent that uses external APIs is mostly about separating the problem into layers and adding good observability. Here’s a practical approach.
1) Identify where the failure happens
A tool call can fail at several points:
-
Model decides to call the tool incorrectly
- Wrong tool chosen
- Missing arguments
- Malformed arguments
-
Your agent orchestration layer fails
- Tool schema mismatch
- Serialization/deserialization issues
- Timeout handling bugs
- Retry logic problems
-
External API fails
- Bad auth
- Rate limiting
- Network errors
- Invalid request payload
- Upstream service outage
-
Post-processing fails
- Response parsing errors
- Mapping API results back into the agent
- State update bugs
The first step is to determine which layer is failing.
2) Log the full tool-call lifecycle
Add structured logs around each stage:
- User input
- Model output
- Tool call name
- Tool arguments
- Request sent to external API
- HTTP status / error body
- Parsed response
- Final message returned to user
Example fields to log:
trace_idconversation_idturn_idtool_namerequest_jsonresponse_statusresponse_bodylatency_msretry_counterror_type
This makes it much easier to see whether the model produced bad arguments or the API rejected a valid request.
3) Reproduce the call outside the agent
Take the exact tool arguments the model produced and run them manually:
- Use
curl - Use Postman
- Use a small test script
If the API call fails outside the agent too, the issue is likely with the API request, auth, payload, or upstream service.
If it works outside the agent, the problem is likely in your agent code or how arguments are being transformed.
4) Validate tool schemas strictly
A lot of failures come from schema mismatches.
Check:
- Required vs optional fields
- Data types
- Enum values
- Nested object structure
- Date/time format
- Array vs scalar fields
If your model produces JSON, validate it before calling the API:
- JSON parse
- Schema validate
- Coerce or reject invalid fields
Return a clear error if validation fails, and consider asking the model to retry with corrected arguments.
5) Inspect the model’s tool call output directly
Don’t just look at the final natural-language answer. Look at:
- Tool name selected
- Arguments generated
- Whether the model omitted required parameters
- Whether the model hallucinated unsupported parameters
Sometimes the model is “reasoning correctly” but generating invalid call syntax or incomplete parameters.
6) Add timeouts, retries, and backoff carefully
External APIs often fail transiently.
Use:
- Short but reasonable timeouts
- Retries for transient failures only
- Exponential backoff with jitter
- Idempotency keys if the API supports them
Avoid retrying on:
- 4xx validation errors
- auth failures
- deterministic schema errors
Retrying those can waste time and hide the real issue.
7) Classify errors by type
Make sure your agent distinguishes:
- Validation error
- Authentication/authorization error
- Rate limit
- Timeout
- Network error
- Upstream 5xx
- Unexpected response shape
This helps you decide whether to:
- Retry
- Repair input
- Ask the model to re-plan
- Escalate to a human
- Fail gracefully
8) Capture the raw API response
When a tool fails, log the full raw response body, headers if useful, and request ID from the upstream service.
Often the response includes:
- A helpful validation message
- A rate-limit hint
- A request correlation ID for vendor support
Example useful metadata:
x-request-idtraceparentretry-after- vendor-specific error codes
9) Create minimal reproducible test cases
Build a tiny test harness that:
- Takes a user prompt
- Produces the model’s tool call
- Calls the tool
- Asserts expected output
Use saved transcripts from failing runs as regression tests. This is one of the best ways to stop regressions.
10) Use tracing/observability tools if possible
If you have distributed tracing, instrument:
- Agent step
- Tool invocation
- External API request
Tools like OpenTelemetry can help you see end-to-end latency and failure points across systems.
11) Common failure patterns
A few common ones:
- Missing auth token in deployed environment
- Local works, prod fails due to env/config mismatch
- Tool argument names differ from API parameter names
- The model sends strings where integers are required
- API response shape changed unexpectedly
- Silent truncation of tool arguments due to token limits
- Rate limiting during bursts
- Agent loops because it doesn’t recognize a tool failure as terminal
12) Improve the agent’s recovery behavior
When a tool call fails, the agent should not always just “try again.”
Good behaviors:
- If validation fails, ask the model to regenerate arguments
- If auth fails, stop and surface an operational error
- If rate-limited, back off and retry
- If external API is down, provide a fallback or apology
- If response parsing fails, log raw payload and stop retrying blindly
13) A practical debugging checklist
Use this sequence:
- Check the error type and stack trace
- Inspect the exact tool arguments
- Validate the arguments against schema
- Replay the API call manually
- Compare local vs production environment variables
- Inspect raw API status/body
- Confirm retries/timeout behavior
- Add or review tracing logs
- Turn the failing case into a regression test
14) If you want a robust pattern
A solid agent-tool integration usually has:
- Strict tool schema validation
- Request/response logging
- Typed adapters between model and API
- Clear error classification
- Retries only for transient faults
- Regression tests from real failures
If you want, I can also give you:
- a Python debugging template for tool calls,
- a Node.js example,
- or a checklist for OpenAI/Anthropic tool-calling agents specifically.