AI WORKFLOWS

BUILD RESILIENT AI WORKFLOWS

Most AI automations die quietly: a timeout here, a 429 there, a bad model response that silently corrupts downstream data. This guide gives you production patterns—circuit breakers, retries with backoff, fallback chains, and observability—that keep AI workflows running without babysitting.

FreeLast tested: 2026-08-06Audience: operators, small teams, indie hackers

Why AI workflows break in production

AI APIs look like ordinary HTTP endpoints, but they fail differently. Rate limits spike at 429, model latency varies by 10x between off-peak and rush hours, and upstream providers occasionally return 500 or malformed JSON without warning. Unlike internal databases, you cannot run a local replica of OpenAI or Anthropic for testing.

The usual failure modes:

The fix is not "add more retries." Retries without limits amplify rate-limit problems and cost. You need a layered defense: circuit breakers, bounded retries, fallbacks, and telemetry.

Circuit breakers for AI APIs

A circuit breaker wraps each upstream call. After N consecutive failures within a time window, it opens and short-circuits all further calls for a cooldown period. This protects your budget, your queue depth, and your downstream data quality.

Three states:

  1. Closed: normal operation. Failures increment a counter; successes reset it.
  2. Open: calls fail immediately without hitting the upstream. Prevents wasted retries and cost.
  3. Half-open: after cooldown, allow one probe request. If it succeeds, close the circuit; if it fails, reopen.

For AI workflows, tune the window to match provider behavior. Anthropic and OpenAI both retry 429s server-side, so a 60-second window with a 5-failure threshold is usually enough. For smaller providers or self-hosted models, tighten to 30 seconds.

# Pseudocode for an AI-aware circuit breaker breaker = CircuitBreaker( failure_threshold=5, recovery_timeout=60, # seconds half_open_max_calls=1 ) for attempt in range(max_retries): if breaker.is_open(): return fallback() try: result = call_ai_api(prompt) breaker.record_success() return result except RateLimitError: breaker.record_failure() sleep(backoff(attempt)) except APIError as e: breaker.record_failure() log(e) break return fallback()

Retries with exponential backoff and jitter

Linear retries hammer failing endpoints. Exponential backoff spreads the load. Jitter prevents thundering herd when multiple workers retry on the same schedule.

Formula: delay = base * 2**attempt + random(jitter)

Recommended values for AI APIs:

ParameterValueWhy
base1–2sMost 429s clear within seconds on OpenAI/Anthropic.
max_delay30–60sCap total wait time per call.
max_attempts3–4More than 4 usually indicates upstream outage, not transient blip.
jitter±20%Desynchronize retries across workers.

Always set a hard timeout on the HTTP client (10–30s). Do not rely on the provider's default timeout, which may be much longer and will exhaust your worker pool.

Fallback chains: primary, secondary, degrade gracefully

A fallback is what runs when the primary model is unavailable or returns unusable output. Build a chain of 2–3 tiers:

  1. Primary: your preferred model (best quality, cost-optimized).
  2. Secondary: a different provider with similar capability. If primary is Anthropic Claude, secondary could be OpenAI GPT-4o or a local model via Ollama.
  3. Degraded mode: cached result, rule-based template, or a clear "needs human review" flag.

The key rule: fallback output must be marked. Never silently serve degraded content as if it were the primary result. Add a metadata flag so downstream consumers can decide whether to display, queue, or alert.

For teams running multi-model setups, agent chaining is a natural place to insert fallback logic—each agent in the chain can declare its own retry policy and fallback.

Observability: logging, cost tracking, and alerting

You cannot fix what you cannot see. Every AI call should emit structured logs with at minimum: provider, model, prompt tokens, completion tokens, latency, status code, and whether a fallback was triggered.

Cost tracking is non-negotiable. Log token usage per workflow run and aggregate daily. Set alerts at 80% and 100% of budget. Most teams discover runaway costs only at month-end billing—by then, a single bug has already burned the monthly allocation.

For prompt-level debugging, system prompts for agents often hide failure modes. If a workflow suddenly degrades, diff the system prompt first—many provider updates change default behavior in ways that break existing prompts.

Minimal structured log schema

{ "ts": "2026-08-06T14:32:10Z", "workflow": "customer-onboarding", "step": "generate-welcome-email", "provider": "openai", "model": "gpt-4o", "prompt_tokens": 412, "completion_tokens": 287, "latency_ms": 1203, "status": "success", "fallback_used": false, "run_id": "abc-123" }

Emit one log line per call. Aggregate downstream. Do not batch logs in memory—flush after each call so you have data even if the worker crashes.

Handoff and audit patterns for AI workflows

Resilience is not just about surviving API failures. It is also about surviving human failures: someone changes a prompt without testing, a model version rolls without notice, a team member deploys on Friday afternoon without a rollback plan.

Run a lightweight handoff audit after every workflow change. The audit checks: retry limits, fallback labels, cost caps, and alert thresholds. Five minutes of audit prevents five hours of debugging.

Limits and notes

These patterns apply to any AI API workflow, not just OpenAI or Anthropic. The principles—circuit breaking, bounded retry, graceful degradation, structured telemetry—are provider-agnostic. Adapt the thresholds to your latency and error profiles.

Do not over-engineer on day one. Start with a timeout, one retry, and one fallback. Add the circuit breaker when you have enough data to tune the failure threshold. Measure before you optimize.