tenacity-python

Designs bounded retry policies in Python using Tenacity predicates, stop strategies, and waits.

Updated Aug 12, 2026
One-click install
npx skills add https://github.com/schattenspiegel/skill-foundry-skills --skill tenacity-python-schattenspiegel
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: tenacity-python
Source: https://github.com/schattenspiegel/skill-foundry-skills/tree/main/skills/tenacity-python
Command: npx skills add https://github.com/schattenspiegel/skill-foundry-skills --skill tenacity-python-schattenspiegel

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires tenacity, and includes references (resource) components.

What problem does it solve? Transient failures in network calls, database operations, and service integrations cause flaky behavior when handled with ad-hoc loops or blanket exception catching. This Skill guides the design of safe, bounded retry policies with Tenacity so repeated attempts respect idempotency, deadlines, and failure taxonomies. ## Core Features & Use Cases - Bounded Policy Design: Compose retry predicates, finite stop strategies, and jittered wait strategies using retry, Retrying, and AsyncRetrying. - Safety Guardrails: Enforce idempotency checks, exclude permanent failures like auth errors, and prevent nested retry multiplication across client layers. - Deterministic Testing & Observability: Test policies with injected sleep and structured before_sleep telemetry that avoids leaking credentials or payloads. - Use Case: A service call to a contended remote API intermittently fails with transient errors. Use this Skill to wrap the narrowest retryable operation with a typed exception predicate, capped exponential random wait, four-attempt stop, and reraise=True, then verify attempt counts with deterministic tests. ## Quick Start Ask the assistant to write a Tenacity retry policy for a specific transient operation, including the stop condition, wait strategy, and a deterministic test.

Frequently Asked Questions about tenacity-python

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write a retry decorator with Tenacity in Python?▼

Use the @retry decorator with a narrow predicate like retry_if_exception_type, a finite stop such as stop_after_attempt, and a wait strategy like wait_random_exponential. Set reraise=True so callers receive the final domain exception instead of RetryError.

How do I retry async functions with Tenacity?▼

Decorate an async def function with @retry or use AsyncRetrying for block iteration. The awaited client call and sleep must remain nonblocking; never call time.sleep inside an async attempt because Tenacity cannot make blocking code asynchronous.

When should I not retry an operation?▼

Do not retry permanent failures such as authentication errors, invalid input, or programmer defects, and never retry non-idempotent writes without an idempotency key or transactional upsert. Also avoid retrying broad Exception, cancellation, or keyboard interrupts.

How do I test Tenacity retry logic without sleeping?▼

Inject sleep=lambda _: None into Retrying or replace the wait strategy only in the test-owned policy. Feed deterministic exception sequences and assert attempt counts, terminal exceptions, and that visible side effects occur at most once.

Does stop_after_attempt(3) mean three retries?▼

No, stop_after_attempt(3) permits at most three total attempts including the first call, not three retries after it. The first invocation counts as attempt 1 in Tenacity's retry state model.

Why does retrying cause duplicate side effects?▼

Duplicates occur when a write lacks an idempotency mechanism or when nested layers each retry, multiplying attempts. Require a stable idempotency key or atomic upsert for writes, and designate a single retry owner with one outer deadline.