# LLM-call fault tolerance

| Situation | Policy |
| --- | --- |
| Rate limit, overload, server, timeout, network | Retried: 5 attempts, jittered exponential backoff, 2 s base, 30 s cap |
| `Retry-After` (ms, seconds, HTTP date) | Honoured as a lower bound up to 300 s. Longer: fails at once, flagged exhausted, hint kept, so a fallback, credential switch or the user can act. |
| Auth, invalid request, context overflow, policy | Not retried |
| Refused for its shape | [Mended and resent](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/reliability/request-repair.html) |
| Long silence on a stream | Fails after 10 min with no bytes or 15 min with no real data; no total limit |
| Dead connection | 30 s connect, TCP keepalive notices in about 60 s |
| Break after content | Codex keeps the partial reply as `stop_reason="length"`. Other providers re-send the whole request on a transport failure and void the partial, see [Re-sending a failed reply](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/reliability/error-and-timeout-mechanism.html#resend). |
| Network unreachable | [Waits for it](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/reliability/error-and-timeout-mechanism.html#network-wait) |
| Another model | Off by default; before the first token only |
| Still failing | [Structured error in the UI](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/reliability/error-taxonomy-propagation.html) |

## Why read timeouts are generous

A reasoning stream must not carry a tight read timeout: a single `timeout=120` caps the body read and fires over a buffering proxy, killing a healthy stream. Codex uses `Timeout(connect=30, read=660, write=30, pool=30)`.

| Budget | Reset by | Catches |
| --- | --- | --- |
| `SSE_IDLE_TIMEOUT_S` 600 | Any line, pings included | A dead connection. Codex's own client allows 5 min; 10 leaves room for a buffering proxy. |
| Stream idle bound 300 | Parsed events only | A reply that stops while the connection stays open, pings included; retried. Every provider, see [Idle bound](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/reliability/error-and-timeout-mechanism.html#idle-bound) |
| `OPENPROGRAM_SSE_TOTAL_TIMEOUT_S` 0 | Nothing | Positive values opt into a deadline |

## Transport and recovery

| Part | Behaviour |
| --- | --- |
| `utils/http_transport/timeouts.py` | The one timeout source, with context-scaling helpers |
| `utils/http_transport/http_client.py` | Keepalive, per OS. Force-IPv4 binds an IPv4 source, since httpx has no Happy Eyeballs. `get_shared_async_client` is loop-keyed, so Codex reuses TLS across turns. Proxy through httpx `proxy=`. |
| `utils/http_transport/rate_limit.py` | Parses `x-ratelimit-*`, `anthropic-ratelimit-*`; Codex warns when a bucket is low |
| `retry_after_beyond_cap` | One rule for the stream retry and `Runtime.exec` |
| Partial recovery, `openai_codex.py` | A transient break after content finalizes the turn; permanent failures still fail |
| openai-completions retry | After `EventStart`, before content: a retryable `APIError`, including xAI's status-less `Internal error during token generation`, reopens on the same model. Otherwise the outer handler records cooldown and re-raises. |
| gemini_cli | Same client and timeouts |

## Failover

`utils/failures/failover.py` and `stream_with_failover` try each fallback on a failover-worthy failure before content, forwarding events and suppressing the duplicate `start`.

| `model_fallback.mode` | Tries | Crosses providers |
| --- | --- | --- |
| `off` (default) | None; a 429 shows that error | – |
| `same-provider` | Up to two other enabled models of the provider, in config-row order, reusing the credential | no |
| `list` | `model_fallback.models`, `provider/model` ids in order | yes |

Settings → Model services → Behavior, or `openprogram config`. `OPENPROGRAM_FALLBACK_MODELS` overrides it. A fallback reply records what was skipped in `AssistantMessage.failover`. Each switch also goes to the `route_events` table the [routing overview](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/routing-overview.html) reads.

## Environment variables

| Variable | Default | Meaning |
| --- | --- | --- |
| `OPENPROGRAM_SSE_IDLE_TIMEOUT_S` | 600 | No bytes |
| `OPENPROGRAM_STREAM_IDLE_TIMEOUT_S` | 300 | No event; 0 turns the bound off |
| `OPENPROGRAM_SSE_DATA_STALL_TIMEOUT_S` | the idle bound | No real data, Codex only |
| `OPENPROGRAM_SSE_TOTAL_TIMEOUT_S` | 0 | One-stream deadline; 0 is none |
| `OPENPROGRAM_HTTPX_CONNECT_TIMEOUT_S` | 30 | Connect |
| `OPENPROGRAM_HTTPX_WRITE_TIMEOUT_S`, `_POOL_TIMEOUT_S` | 30 | Write, pool |
| `OPENPROGRAM_HTTPX_READ_TIMEOUT_S` | idle + 60 | Read backstop |
| `OPENPROGRAM_PROVIDER_STREAM_RETRIES` | 5 | Attempts per stream |
| `OPENPROGRAM_PROVIDER_STREAM_BACKOFF_S` | 2.0 | Backoff base |
| `OPENPROGRAM_PROVIDER_STREAM_BACKOFF_MAX_S` | 30.0 | Exponential cap |
| `OPENPROGRAM_RETRY_AFTER_CAP_S` | 300 | Longest wait sat through; 0 lifts it |
| `OPENPROGRAM_NETWORK_WAIT_MAX_S` | 1800 | Waiting for the network; 0 disables |
| `OPENPROGRAM_MAX_RETRIES` | 3 | `Runtime.exec` attempts |
| `OPENPROGRAM_EXEC_TIMEOUT_S` | 0 | `exec` deadline; 0 is none |
| `OPENPROGRAM_TCP_KEEPALIVE` | 1 | Dead-peer detection |
| `OPENPROGRAM_TCP_KEEPIDLE_S`, `_KEEPINTVL_S`, `_KEEPCNT` | 30, 10, 3 | Probe timing |
| `OPENPROGRAM_FORCE_IPV4` | 0 | IPv4 source, for broken-IPv6 VPNs |
| `OPENPROGRAM_PARTIAL_RECOVERY` | 1 | Keep partial output |
| `OPENPROGRAM_FALLBACK_MODELS` | empty, the setting | `same-provider`, a list, or `off`/`none` |

**Comparison with peer frameworks**

| Dimension | openclaw | opencode | hermes-agent | pi-ai (codex) | OpenProgram |
| --- | --- | --- | --- | --- | --- |
| Attempts | 3 (+2 inner) | 2 | 3 | 3 | 5 |
| Backoff base | 300 ms | 500 ms | 5 s | 1 s | 2 s |
| Backoff cap | 30 s | 10 s | 120 s | none | 30 s |
| Jitter | symmetric, positive | ±20% | decorrelated | none | symmetric, positive |
| Retryable status | 408, 409, 429, 5xx | 429, 503, 504, 529 | 429, 5xx, 524 | 429, 5xx | 429, 5xx, body patterns |
| Retry-After | ms, s, date | ms, s, date (cap 10 s) | none | none | ms, s, date (cap 300 s) |
| Idle timeout | 30 min any byte | none; WS 5 min | 180 s stale, scaled | none | 10 min any byte, 15 min data |
| Connect timeout | undici default | none; WS 15 s | SDK default | none | 30 s |
| First-byte guard | 30 s (Azure) | n/a | 120 s (codex) | none | idle and read |
| HTTP version | forced 1.1 | default | auto (h2) | n/a | httpx default (1.1) |
| IPv6 | autoSelectFamily | no | no | n/a | force-IPv4 switch |
| TCP keepalive | undici default | no | 30/10/3 | n/a | 30/10/3 |
| Connection reuse | keep-alive | WS pool, 55 min recycle | shared, rebuilt when stale | n/a | shared, loop-keyed |
| Key rotation | yes | no | pool, cooldowns | n/a | pool, cooldowns |
| Model failover | yes | WS to HTTP only | chain | n/a | chain, off by default |
| Break after first token | error | error | partial, continue | error | partial, continue (Codex); re-send (others) |
| OAuth refresh | n/a | n/a | per-request provider | per call | per call |
| Rate-limit headers | n/a | yes | Nous | n/a | yes |
| Classification | yes | tagged union | yes | basic | `ErrorReason` |

**What each project contributes**

| Project | Patterns |
| --- | --- |
| openclaw (`references/openclaw`, TS) | 30 min stream timeout on the undici dispatcher, reset by any byte. Forced HTTP/1.1 and Happy Eyeballs avoid h2 resets and broken IPv6. Two-tier retry. Key rotation with per-key transient retry. Failover categories as regex groups. Positive-only jitter with Retry-After; `x-should-retry`. |
| hermes-agent (Python, closest) | Keepalive socket injection and forced TCP close against CLOSE_WAIT. Decorrelated backoff seeded from `time_ns`. Stale timeout scaled by context, off for local providers. Separate first-byte and inter-event timeouts. Credential pool with cooldowns (401 5 min, 429/402 1 h, dead 24 h). Per-request OAuth hook. Partial reply with `finish_reason=length`. |
| opencode (TS) | No HTTP body timeout. WS: 15 s connect, 5 min idle, 55 min recycle, HTTP after 5 failures. Rate-limit headers into a structured object. Tagged-union errors. |
| pi-ai (TS, source of our Codex provider) | 3 retries, 1 s base, 429/5xx and body patterns, no read timeout |
| claude-code (partial bundle) | HTTP behaviour is the Anthropic SDK's |

## Implementation status

| Status | Items |
| --- | --- |
| Implemented | Classifier, retry and backoff, timeouts, shared client, keepalive, IPv4 switch, rate-limit parsing, opt-in failover, pre-content retry for Codex, OpenAI Responses and completions. The SDKs' own retries are off (`max_retries=0`); the credential layer retries and switches keys. Partial recovery: Codex only. |
| Credentials | Every request goes through `auth/selection.py`: a limited or refused credential is held and the next is tried. A sole credential cools like any other; the request waits up to 60 s for it with a notice, else fails saying when it is back. |
| Not built | Per-request OAuth hook: Codex already resolves and refreshes the bearer per call. |
