agent-fallback-sentinel
Lightweight, production-grade middleware for resilient LLM calls — retries, automated fallback routing, circuit-breaking, and rigid schema validation.
The problem
LLM APIs rate-limit. They return malformed JSON. They go down at 3am.
When that happens mid-agent-workflow, your entire pipeline dies — and your users see a 500.
agent-fallback-sentinel wraps your LLM calls in a resilience layer that:
- 🔁 Retries transient failures with configurable backoff.
- 🛟 Fails over to a secondary provider (OpenAI → Anthropic, etc.) automatically.
- 🔌 Trips a circuit breaker when all routes are dead, with full error context.
- ✅ Validates output against a Pydantic schema — no more
KeyError: 'action'in prod. - 📡 Emits events so you can plug in your own metrics/tracing.
Zero dependencies beyond pydantic.
Install
pip install agent-fallback-sentinel
(Coming to PyPI. For now: pip install git+https://github.com/Jamie643/agent-fallback-sentinel.git)
Quickstart
from pydantic import BaseModel
from agent_fallback_sentinel import AgentSentinel
class Output(BaseModel):
status: str
action: str
sentinel = AgentSentinel(max_retries=2, cooldown=1.0)
result = sentinel.execute_with_fallback(
primary_fn=call_openai,
fallback_fn=call_anthropic,
schema=Output,
)
# -> {"status": "success", "action": "retry_job"}
That's it. If call_openai rate-limits or returns garbage, call_anthropic runs automatically. If both fail, you get a SentinelCircuitBreaker with every captured error inside.
Why not just tenacity?
tenacity is great at retries. It doesn't know about:
- Multi-provider failover (a first-class concept here).
- Schema validation as a failover trigger — malformed output ≠ retryable; it should fail over immediately.
- Typed circuit-breaker exceptions carrying every underlying error for observability.
Think of this as tenacity + pydantic + failover, purpose-built for LLM calls.
Configuration
| Argument | Default | Description |
|---|---|---|
max_retries |
2 |
Retry attempts for the primary provider (excludes the first try). |
cooldown |
1.0 |
Seconds between retries on a given provider. |
fallback_max_retries |
1 |
Retry attempts for the fallback provider. |
sleep_fn |
time.sleep |
Injectable for testing — pass lambda _: None to run instantly. |
on_event |
None |
Callable[[str, dict], None] for observability hooks. |
Events emitted via on_event
| Event | When |
|---|---|
attempt |
Before each attempt on a provider. |
failover |
When primary is exhausted and we switch to fallback. |
success |
After a provider returns a schema-valid result. |
trip |
When the circuit breaker opens. |
Behavior contract
- Primary is retried up to
max_retries + 1times. - Validation errors skip retries — malformed output won't self-heal. Failover is immediate.
- Fallback is retried up to
fallback_max_retries + 1times. - If everything fails,
SentinelCircuitBreaker.errorscontains every captured exception, in order.
Real-world example
See examples/openai_anthropic_failover.py for a complete OpenAI → Anthropic failover with schema validation.
Development
git clone https://github.com/Jamie643/agent-fallback-sentinel.git
cd agent-fallback-sentinel
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pre-commit install
pytest
Roadmap
- Async support (
aexecute_with_fallback) - Pluggable backoff strategies (exponential, jittered)
- Native LangChain / LlamaIndex adapters
- Prometheus metrics exporter
License
MIT — see LICENSE.
Metadata
Release files for agent-fallback-sentinel 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_fallback_sentinel-0.1.0.tar.gz | 8.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| agent_fallback_sentinel-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.5 kB
Release files / agent_fallback_sentinel-0.1.0.tar.gz
| Download URL | agent_fallback_sentinel-0.1.0.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
228c93c242033008707f05f508fa2d5c4ab5a572735c42d2eded7a51aebfa2db
|
|
BLAKE2b-256 checksum How to use checksums |
c279c027b9b877842873e460908b11a75ec5c13dffd6f001d5e0dcbd939d1947
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.
Transparency logRelease files / agent_fallback_sentinel-0.1.0-py3-none-any.whl
| Download URL | agent_fallback_sentinel-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
361292bf5fc189806995755439ecf3287f4c09e20268093a3a272f6672c91435
|
|
BLAKE2b-256 checksum How to use checksums |
8b1b81b456ee732e5ce2291a7c2ac915e537ce9cea76157b868a3e7c554c2d09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.
Transparency log