Handling a 401 error in an LLM agent's tool call
Robust agent error handling.
Catch the tool error, return a structured observation to the LLM, and distinguish recoverable retries from terminal failures needing re-plan or escalation.
WHAT THIS TESTS Whether you treat tool failures as first-class feedback and can classify errors so the agent recovers intelligently instead of looping or crashing.
A GOOD ANSWER COVERS CATCH AND STRUCTURE. The tool-execution layer wraps every call so exceptions and non-2xx responses are caught, never propagating as an uncaught crash. On failure it produces a structured observation containing the HTTP status, a concise error message, and optionally a hint about the likely cause and remediation. FEED BACK. This observation is appended to the agent's context as the result of its action, just like a successful result would be, so the LLM can reason over it in the next step. The prompt or scaffolding instructs the agent that tool calls may fail and that it should inspect the error and decide a recovery action. CLASSIFY THE ERROR. Crucially, a 401 Unauthorized is an authentication failure, not a transient glitch, so retrying the identical call is futile. The correct recovery is to refresh or re-fetch the credential or token, verify the auth configuration, try an alternative tool, or, if it cannot self-remediate, escalate to a human or report the blocker. Reserve retry-with-exponential-backoff for transient codes like 429 or 503. GUARDRAILS. Cap retries, enforce a maximum step count and budget, and add a circuit breaker so a persistently failing tool does not consume the loop.
COMMON WRONG ANSWERS Retrying a 401 repeatedly. Letting the raw exception crash the agent. Returning an unstructured stack trace the model cannot parse. Treating all errors identically with blind backoff. No loop or cost limit, allowing runaway retries.
LIKELY FOLLOW-UPS Which status codes warrant retry versus re-plan? How do you prevent infinite retry loops? How do you securely refresh a token mid-task? When should the agent give up and escalate?
ONE CONCRETE EXAMPLE The agent calls a CRM API and gets 401. The tool layer returns an observation: status 401 Unauthorized, likely expired token, action: refresh credentials. The agent reasons that retrying as-is will fail, invokes a refresh-token tool, then re-issues the original call with the new token and succeeds, all without crashing, and a step counter ensures it would escalate rather than loop if the refresh also failed.
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.