# `LlmCall.Error`
[🔗](https://github.com/gilbertwong96/llm_call/blob/main/lib/llm_call/error.ex#L1)

A codec error.

`retryable?` is a hint, not a retry policy. It comes from two signals: the
codec's verdict on the provider's own code or status field, and the HTTP status
(`retryable_status?/1`). A failure that never produced a response has no codec
verdict and no status, so it is classified by its `reason` instead: the
connection-level failures a new attempt can fix (`:timeout`, `:closed`,
`:econnrefused`, `:econnreset`, `:etimedout`, `:epipe`, `:ehostunreach`,
`:enetunreach`, `:nxdomain`, a `{:tls_alert, _}`, a re-established WebSocket
lane, `:pool_timeout`, and Finch's HTTP/2 connection reasons listed under
`reason`) are retryable; everything else is not. The client takes the union, because a provider that
introduces a new code — or a proxy that answers with its own body — must not
turn a `429` into "do not retry". `kind` says where the error came from. A codec
produces `:validation`, `:invalid_response` or `:provider`, and the client
produces the same kinds for its own failures — an unencodable body or an
undeclared codec name is `:validation`, a `2xx` body that is not JSON is
`:invalid_response`. `:transport` is a failure below the protocol: no response at
all, a response that never completed, or a non-`2xx` status. `execute/2`,
`fetch/2` and a non-`2xx` stream therefore report `:transport` for a response the
provider *did* send, so `status` is set there and the codec still decoded the
body when it could.

`retry_after_ms` is the parsed `Retry-After` header, when the transport saw
one; it is a hint, not a retry policy.

## `retryable?` on a stream

The hint answers "can a new attempt fix the connection". For a stream it says
nothing about what the caller has already consumed: reducing a stream again issues
the request again and yields every event from the first one, so a retry after any
event reached the caller duplicates output, and a `:message_stop` the caller already
assembled cannot be un-assembled. A retry policy for a stream therefore has to be
gated on "nothing was yielded yet", which only the caller knows. The library never
retries, and `retryable?: true` is not permission to.

`raw` is the provider payload the error was decoded from, and it follows
`LlmCall.Config.capture_raw` like every other `raw`: it is `nil` unless the
policy is `:response` or `:all`.

`code` is the provider's machine-readable code as a string (OpenAI's `code`,
Anthropic's error type, Google's status such as `"RESOURCE_EXHAUSTED"`,
MiniMax's numeric code stringified). `status` is the HTTP status, and **only the
client sets it**: a codec never sees the response status, so it always leaves
it `nil`.

`reason` is the structured counterpart of a `:transport` message, and only the
client sets it. Match on it instead of the message. The library produces:

  * `{:unknown_pool, name}` — the configured Finch pool is not running;
  * `:disconnected`, `:connection_closed`, `:pool_not_available`,
    `:connection_process_went_down`, `:could_not_connect`, `:connection_dead`,
    `:connection_not_ready` — Finch's HTTP/2 vocabulary for a connection that is
    not usable right now. All are retryable;
  * `:pool_timeout` — an HTTP/1 pool was saturated: every connection was checked
    out for longer than the request's `:pool_timeout`, so the request was never
    sent. Raising the pool's `size`/`count`, or that timeout, is the fix. This is
    the only `:transport` reason that is entirely local to the caller, and it is
    HTTP/1 only — Finch's HTTP/2 pool never reads `:pool_timeout` and reports a
    saturated or unusable connection with its own reasons instead;
  * `:exception` — the HTTP transport raised (for a saturated HTTP/1 pool Finch
    raises instead of returning, but that case is reported as `:pool_timeout`);
  * `:disconnected` — the Responses WebSocket is not connected;
  * `{:interrupted, why}` — an in-flight WebSocket lane was dropped
    (`:reconnected`, `:max_age`, `{:unroutable_event, :stream_id_not_echoed}`,
    or the close reason);
  * `{:connection_exited, why}` — the WebSocket connection process ended;
  * the transport's own reason otherwise (`:econnrefused`, `:timeout`,
    `:closed`, `:nxdomain`, `{:tls_alert, ...}`).

A codec leaves it `nil`, and so does any failure that has a `status`.

# `reason`

```elixir
@type reason() :: atom() | tuple() | nil
```

A structured transport reason, or `nil` when the failure has none.

An atom or a tuple; the module documentation lists the values this library
produces and the transport reasons it passes through. Only `kind: :transport`
sets it.

# `t`

```elixir
@type t() :: %LlmCall.Error{
  code: String.t() | nil,
  kind: :validation | :invalid_response | :provider | :transport,
  message: String.t(),
  raw: map() | nil,
  reason: reason(),
  retry_after_ms: non_neg_integer() | nil,
  retryable?: boolean(),
  status: non_neg_integer() | nil
}
```

# `retryable_status?`

```elixir
@spec retryable_status?(non_neg_integer() | nil) :: boolean()
```

Whether an HTTP status is worth retrying on its own.

Used two ways: as the whole hint for a `:transport` error that carries no
provider verdict, and as the second input when a decoded error's status is
filled in. A provider adding a new rate-limit code must not make a `429` look
permanent, and the reverse — retrying a `413` — stays out because it is not in
the list.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
