# Errors & retries (/docs/python/guides/error-handling)



## Catching errors [#catching-errors]

Every API and network error raises a typed `TawaAPIError` rather than
returning an error-shaped result. A successful scan never needs an error
check beyond your normal `try`/`except`.

```python
from tawaguard import TawaGuard, TawaAPIError

try:
    client = TawaGuard("YOUR_API_KEY")
    result = client.analyze(user_input)
except TawaAPIError as err:
    print(err.message)     # error description
    print(err.status_code) # HTTP status code (or None for network errors)
    print(err.retryable)   # whether a retry could succeed
```

## Automatic retries [#automatic-retries]

Both the sync and async client retry transient failures automatically — up
to 3 attempts by default, with exponential backoff and jitter between them:

* **`429`** (rate limited) — honors a `Retry-After` header when the server
  sends one, falls back to backoff otherwise.
* **`5xx`** (server errors) — same backoff, no `Retry-After` expected.
* **Network errors** (connection failures, timeouts) — retried the same way.

Anything else — `4xx` other than `429`, or exhausting `max_retries` — raises
immediately. `retryable` on the caught error tells you whether the SDK
already gave up on a retryable class of error (all attempts exhausted) or
the error was never retryable to begin with (e.g. a `401`).

## Guardrails fail open [#guardrails-fail-open]

The OpenAI Agents SDK guardrails catch scan errors. From 0.3.0 a gateway or
network error (after the retries above) goes to `on_error` and the scan is
skipped instead of aborting the agent run. Pass `fail_closed=True` to let the
error propagate. See
[Gateway and network errors](/docs/python/integrations/openai-agents-sdk#gateway-and-network-errors).

## Tuning retries and timeouts [#tuning-retries-and-timeouts]

Both the sync and async client take `max_retries`/`timeout` at construction:

```python
client = TawaGuard(
    "YOUR_API_KEY",
    max_retries=5,   # default: 3
    timeout=60.0,    # default: 30.0 seconds
)
```

See [Configuration](/docs/python/sdk/client) for the full option set, and the
[API Reference](/docs/api/best-practices) for the raw HTTP rate-limit
contract (`Retry-After`, recommended backoff) this retry behavior is built
on top of.
