Lesson 4 of 5 Building Tool Ecosystems with MCP

Handling Tool Errors with Retries, Circuit Breakers and Fallbacks

This lesson covers error classification, retries, circuit breakers, idempotency and fallbacks, so that an agent copes with tool failures instead of looping, giving wrong answers or performing duplicate actions.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish transient from permanent errors.
  • Apply retries, circuit breakers and idempotency keys.
  • Return actionable errors to the model and design fallbacks.

Classifying errors

Transient errors are expected to succeed on retry: timeouts, connection resets, HTTP 429 (too many requests), 502, 503 and 504. Permanent errors will fail again with the same input: HTTP 400 (invalid arguments), 401 and 403 (authentication or authorization), 404 (not found) and business rule violations. The response differs: transient errors are retried by code; permanent errors are reported to the model or the user.

Sort into groups

Retry, report or stop?

Exam skills 2.3.3

Decide how each tool error should be handled.

Log in to try this activity Interactive activities are free with an account.

Retries, circuit breakers and idempotency

Retries use exponential backoff with jitter and honour the Retry-After header, with a small maximum number of attempts. A circuit breaker stops calling a dependency that keeps failing, returns an immediate error for a cool-down period, and then tests the dependency again; this prevents a failing back end from slowing every agent. The API Management AI gateway provides circuit breakers and load balancing for model back ends.

Retrying a write is dangerous if the first attempt actually succeeded. Idempotency keys, unique IDs sent with each logical operation and stored by the service, let the service recognize and ignore duplicates, so a refund or ticket is created only once.

The following diagram shows retries, the circuit breaker states and an idempotency key.

Retries, circuit breakers and idempotency for tool calls. Retries use exponential backoff with jitter and honour Retry-After, with few attempts. A circuit breaker stops calling a dependency that keeps failing: while open, it fails immediately for a cool-down period, then lets a test call through, and closes again if it succeeds. Idempotency keys let a service recognize a repeated write, so a retried refund is created only once.
Retry reads freely; retry writes only with an idempotency key.
Fill in the number

Exponential backoff

Exam skills 2.3.3

Work out the waits for exponential backoff that starts at 1 second and doubles, before jitter.

Log in to try this activity Interactive activities are free with an account.

Errors the model can act on, and fallbacks

When an error reaches the model, it is returned as a structured, actionable result rather than a stack trace, for example {"error": "order_not_found", "message": "No order ORD-10423. IDs have six digits.", "retryable": false}. The model can then correct the argument, ask the user, or try another approach.

Fallbacks provide an alternative when a tool is unavailable: a secondary service or region, a cached or last-known-good result clearly marked with its age, a degraded answer that states what could not be checked, or escalation to a person. The following table summarizes the handling strategy.

ErrorHandlingExample
Transient (429, 503, timeout)Retry with backoff, then fallbackRetry three times, then use a cached status
Permanent input error (400, 404)Structured error to the modelModel corrects the order ID format
Authorization error (401, 403)Stop and report; do not retryTell the user the action is not permitted
Dependency downCircuit breaker and fallback or escalationRoute the case to a person
Scenario questions

Errors and fallbacks

Exam skills 2.3.3

Choose how errors should be returned and what to fall back to.

Log in to try this activity Interactive activities are free with an account.
Worked example

Duplicate refunds

During a payment service slowdown, some customers received two refunds. The agent's tool retried after timeouts, but the first requests had succeeded.

The refund tool must send an idempotency key derived from the case and refund request, so that the payment service ignores duplicates. A circuit breaker would also reduce load during the slowdown. Removing retries entirely would avoid duplicates but would fail many legitimate refunds during transient errors.

Check yourself

Which errors should never be retried?

Show answer

Authorization errors (401 and 403) and invalid input, because retrying cannot succeed. Transient errors are retried with backoff.

Exam Tips

Remember for the exam

  • Retry transient errors with backoff; never retry 401/403 or invalid input.
  • Writes need idempotency keys before they can be retried safely.
  • Return structured, actionable errors; bound retries; fall back or escalate.