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
Release files for agent-tool-resilience 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| agent_tool_resilience-0.1.0.tar.gz | 25.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| agent_tool_resilience-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 44.2 kB
Release files / agent_tool_resilience-0.1.0.tar.gz
| Download URL | agent_tool_resilience-0.1.0.tar.gz |
|---|---|
| Size | 25.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c336ab0313ca385fb17d02c3aead34e6e266e1b5c3613c9ab1803b7f66e92001
|
|
BLAKE2b-256 checksum How to use checksums |
85719872be60b77e7eb21cf6f3b343c2d087480cafa8ede1cdf2b505d2d7f9e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / agent_tool_resilience-0.1.0-py3-none-any.whl
| Download URL | agent_tool_resilience-0.1.0-py3-none-any.whl |
|---|---|
| Size | 18.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6a352f4304e89e5b93a25f7c7cebc308268aa53718b20940c8e3cc7727b6cb49
|
|
BLAKE2b-256 checksum How to use checksums |
6d155f637e493e5e55e9bd813ea2cf1f1d570e55002c21cb3857fc34b937fc30
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|