Production-grade resilience for AI agent tool calls: retries, fallbacks, circuit breakers, and validation
Project description
agent-tool-resilience
A Python library for making AI agent tool calls resilient, with smart retries, fallbacks, circuit breakers, and result validation.
Why?
AI agents fail silently when tools break. This library provides production-grade resilience patterns:
- Smart Retries: Exponential backoff with jitter, respects rate limits
- Fallbacks: Graceful degradation when primary tools fail
- Circuit Breakers: Prevent cascade failures by stopping calls to broken services
- Result Validation: Ensure tool outputs meet expected schemas/conditions
- Observability: Full visibility into what happened during tool execution
Installation
pip install agent-tool-resilience
Quick Start
from agent_tool_resilience import ResilientTool, RetryPolicy, CircuitBreaker
# Wrap any tool with resilience
@ResilientTool(
retry=RetryPolicy(max_attempts=3, backoff="exponential"),
circuit_breaker=CircuitBreaker(failure_threshold=5, reset_timeout=60),
fallback=lambda *args, **kwargs: {"error": "service unavailable", "cached": True}
)
def call_weather_api(location: str) -> dict:
return requests.get(f"https://api.weather.com/{location}").json()
# Use it normally - resilience is automatic
result = call_weather_api("NYC")
Features
Retry Policies
from agent_tool_resilience import RetryPolicy
# Exponential backoff with jitter
policy = RetryPolicy(
max_attempts=5,
backoff="exponential",
base_delay=1.0,
max_delay=60.0,
jitter=True,
retry_on=[TimeoutError, ConnectionError, RateLimitError]
)
Circuit Breakers
Prevent cascade failures by temporarily stopping calls to failing services:
from agent_tool_resilience import CircuitBreaker
breaker = CircuitBreaker(
failure_threshold=5, # Open after 5 failures
success_threshold=2, # Close after 2 successes
reset_timeout=60, # Try again after 60 seconds
half_open_max_calls=3 # Limited calls in half-open state
)
Fallback Strategies
from agent_tool_resilience import FallbackChain
fallbacks = FallbackChain([
lambda loc: call_backup_weather_api(loc),
lambda loc: get_cached_weather(loc),
lambda loc: {"status": "unavailable", "location": loc}
])
Result Validation
from agent_tool_resilience import ResultValidator
validator = ResultValidator(
schema={"type": "object", "required": ["temperature", "humidity"]},
conditions=[
lambda r: r.get("temperature") is not None,
lambda r: -100 < r.get("temperature", 0) < 150
]
)
Observability
from agent_tool_resilience import ToolExecutionTracer
tracer = ToolExecutionTracer()
@ResilientTool(tracer=tracer)
def my_tool():
...
# After execution
print(tracer.get_execution_log())
# [
# {"tool": "my_tool", "attempt": 1, "status": "failed", "error": "timeout", "duration_ms": 5023},
# {"tool": "my_tool", "attempt": 2, "status": "success", "duration_ms": 234}
# ]
Rate Limit Awareness
from agent_tool_resilience import RateLimitHandler
handler = RateLimitHandler(
requests_per_minute=60,
respect_retry_after=True,
auto_throttle=True
)
Integration with Agent Frameworks
Works with any Python agent framework:
# LangChain
from langchain.tools import Tool
from agent_tool_resilience import ResilientTool
@ResilientTool(retry=RetryPolicy(max_attempts=3))
def search(query: str) -> str:
...
langchain_tool = Tool(name="search", func=search, description="Search the web")
# OpenAI Function Calling
@ResilientTool(circuit_breaker=CircuitBreaker(failure_threshold=3))
def get_stock_price(symbol: str) -> dict:
...
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agent_tool_resilience-0.1.0.tar.gz.
File metadata
- Download URL: agent_tool_resilience-0.1.0.tar.gz
- Upload date:
- Size: 25.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c336ab0313ca385fb17d02c3aead34e6e266e1b5c3613c9ab1803b7f66e92001
|
|
| MD5 |
43b70eeeeb7460d937b1ffac4b26872d
|
|
| BLAKE2b-256 |
85719872be60b77e7eb21cf6f3b343c2d087480cafa8ede1cdf2b505d2d7f9e8
|
File details
Details for the file agent_tool_resilience-0.1.0-py3-none-any.whl.
File metadata
- Download URL: agent_tool_resilience-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a352f4304e89e5b93a25f7c7cebc308268aa53718b20940c8e3cc7727b6cb49
|
|
| MD5 |
0427808289ceb80ccc6d9ab44b7cacb2
|
|
| BLAKE2b-256 |
6d155f637e493e5e55e9bd813ea2cf1f1d570e55002c21cb3857fc34b937fc30
|