方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Add retry and fallback logic to tool calls

Add Retry and Fallback Logic to Tool Calls

In production agent systems, tool calls fail. APIs time out, rate limits hit, services go down. A resilient agent does not crash on the first failure — it retries with backoff, respects timeouts, and falls back to alternative tools when the primary is unavailable.

What you are building

Create a ResilientToolExecutor class that wraps tool execution with:

  1. Exponential backoff retry — retry transient failures (network errors, 429s, 5xx) up to a configurable number of attempts, with exponential delay between retries.
  2. Per-call timeout — if a single tool call exceeds the timeout, cancel it and count it as a failure.
  3. Fallback tools — when all retries on the primary tool are exhausted, try a list of fallback tools in order.
  4. Structured result — return a result object that includes which tool actually succeeded (or that all failed), the number of attempts, total latency, and any error messages.

Why this matters

Agent reliability is not about making individual tools perfect — it is about making the orchestration layer resilient. A web search tool might fail, but a cached search or a different search provider can substitute. This pattern is used in every production agent deployment and is the difference between a demo and a product.

Constraints

  • Use asyncio for timeout handling.
  • Backoff formula: min(base_delay * 2^attempt, max_delay) with configurable base and max.
  • The executor should be reusable across different tools, not hardcoded to one.
  • Include a is_retryable(error) predicate that callers can customize.

Agents / intermediate / Step 3 of 8

Practice stage

Agent architecture patterns

Hint

Focus on explicit control surfaces — function schemas, state machines, and structured outputs. Agents are most useful when the task requires dynamic tool selection or multi-step reasoning. Start with the simplest pattern (single tool call) before reaching for ReAct loops.

Success criteria
  • - Tools return structured, typed responses
  • - Agent completes the task within a bounded number of steps
  • - All tool calls include error handling and retries
  • - Memory/state management prevents unbounded context growth
Review checklist
  • - Tool schemas validate inputs and handle errors gracefully
  • - Agent loop has explicit termination conditions
  • - State is serializable and inspectable between steps
  • - Cost and token usage are tracked per invocation
  • - Fallback behavior exists for tool call failures

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.