Skip to content

Instantly share code, notes, and snippets.

@robertDouglass
Created February 26, 2026 17:25
Show Gist options
  • Select an option

  • Save robertDouglass/fbbc35fcac9dafcb724d5fa0a6a78f67 to your computer and use it in GitHub Desktop.

Select an option

Save robertDouglass/fbbc35fcac9dafcb724d5fa0a6a78f67 to your computer and use it in GitHub Desktop.
spec-kitty-cli-orchestration run_cli_with_backoff script
#!/usr/bin/env bash
set -euo pipefail
if [ "$#" -lt 1 ]; then
echo "Usage: $0 '<command>' [max_attempts] [base_sleep_seconds]"
exit 2
fi
CMD="$1"
MAX_ATTEMPTS="${2:-4}"
BASE_SLEEP="${3:-5}"
attempt=1
while [ "$attempt" -le "$MAX_ATTEMPTS" ]; do
echo "[attempt ${attempt}/${MAX_ATTEMPTS}] ${CMD}"
set +e
OUT=$(eval "$CMD" 2>&1)
CODE=$?
set -e
echo "$OUT"
if [ "$CODE" -eq 0 ]; then
exit 0
fi
# Retry only transient patterns.
if echo "$OUT" | grep -Eiq "api error: 500|internal server error|overloaded|timeout|temporarily unavailable"; then
sleep_for=$((BASE_SLEEP * attempt))
echo "Transient failure detected. Sleeping ${sleep_for}s before retry..."
sleep "$sleep_for"
attempt=$((attempt + 1))
continue
fi
# Non-transient failure: fail fast.
exit "$CODE"
done
echo "Retries exhausted (${MAX_ATTEMPTS})"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment