Retry and Backoff for Resilient Automation Prompt
Add correct retry, exponential backoff with jitter, timeouts, and circuit-breaking to flaky network or API calls in Bash and Python automation — without retrying things that should never be retried.
- Target user
- Engineers whose scripts fail intermittently against rate-limited or slow APIs
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a reliability-focused engineer who has debugged countless "it works most of the time" automation jobs and knows that naive retries cause outages. I will provide: - The operation being retried (HTTP call, DB connect, CLI command, file lock) - Failure modes seen (timeouts, 429, 5xx, connection reset, transient DNS) - Constraints (idempotency, rate limits, total time budget, where it runs) Design a correct retry strategy and implement it in both Bash and Python: 1. **Retry-or-not decision** — classify errors: retry on transient (timeouts, 429, 502/503/504, connection reset); never retry non-idempotent writes that may have partially succeeded; never retry 4xx auth/validation errors. Make a table. 2. **Backoff math** — exponential backoff with full jitter: `sleep = random(0, min(cap, base * 2**attempt))`. Explain why jitter prevents thundering-herd retry storms and pick sane `base`, `cap`, and `max_attempts` defaults. 3. **Timeouts** — every attempt needs a per-attempt timeout AND an overall deadline; show how to compute remaining budget so total time stays bounded. 4. **Honoring server hints** — respect `Retry-After` headers when present instead of your own backoff. 5. **Bash implementation** — a `retry()` wrapper function with configurable attempts/base/cap using `curl --max-time`, capturing the HTTP code to decide retry vs fail, with jittered `sleep`. 6. **Python implementation** — show it twice: hand-rolled with a loop for zero deps, and with `tenacity` (`retry`, `wait_random_exponential`, `stop_after_delay`, `retry_if_exception_type`) for production. 7. **Idempotency keys** — for POSTs, show using an idempotency key / dedup token so a retried write is safe. 8. **Observability** — log each attempt number, delay, and final outcome; emit a metric/counter for retries so you can spot a degrading dependency. 9. **Circuit breaker (optional)** — when to add one and a minimal example that fails fast after N consecutive failures. Output: (a) the error-classification table, (b) Bash `retry()` function, (c) both Python versions, (d) a test that injects failures and asserts attempt count and total elapsed time stays within budget. Bias toward: bounded total time, never amplifying an outage, idempotency before retries.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python Resilient API Calls with the tenacity Library Prompt
Wrap flaky external API calls with the tenacity library — declarative retry, exponential backoff with jitter, exception/result-based stop conditions, and circuit-breaker-style guards — instead of hand-rolled retry loops.
-
Reusable Python Retry and Backoff Decorator Library Prompt
Design a small, reusable retry/backoff decorator that wraps flaky functions with exponential backoff, jitter, exception filtering, and hooks — for both sync and async code.
-
Bash Resilient curl Downloader Prompt
Write a robust file downloader in Bash using curl with retries, resume, checksum verification, timeouts, and atomic placement — for installers, artifacts, and bootstrap scripts that must not corrupt the target.
-
Bash Word-Splitting and Quoting Hardening Prompt
Audit and rewrite a Bash script to eliminate unquoted-expansion bugs, unsafe word splitting, and glob injection while preserving intended behavior
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.