# Errors & recovery

Save the request ID, distinguish rejection from an unknown outcome, and avoid duplicate paid work.

## Read the error and request ID

OpenAI-style errors contain error.code, error.type, error.message and error.request_id. Messages uses an Anthropic-style error with request_id at the top level. x-request-id is provided in response headers.

- 400 invalid_request: fix the payload or unsupported options. 401: check the key. 403: check access to the resource.
- For 402 insufficient_balance, add paid credit or choose a request with a lower hold. For 402 credit_pending_settlement, wait for other requests to settle or top up to run more at once.
- For 429 spending_limit, increase the key limit or wait for its period to reset. Other 429 errors may include Retry-After.
- 409 outcome_unknown: the request was accepted or its outcome is unresolved, including when its response could not be read. Inspect the original request; do not create another generation. 410 result_expired: the image is no longer available.
- 5xx: retain the request ID and inspect status where available. Never infer “not charged” from an error status.

### OpenAI-style error

```text
{
  "error": {
    "type": "insufficient_quota",
    "code": "insufficient_balance",
    "param": null,
    "message": "Your available credit cannot cover this request\u2019s maximum cost.",
    "request_id": "req_00000000000000000000000000000000"
  }
}
```

## Check the original request

Use GET /v1/requests/{request_id} with the account’s API key. It returns state, failure_code, financial_state, reserved_usd and settled_usd. Media jobs also have their own status and result paths.

- Possible execution states include created, reserved, dispatched, running, succeeded, failed, rejected, canceled and outcome_unknown. Read financial_state independently.
- An unrecognized request ID or a request owned by another account returns 404. Missing status does not establish that no work happened.

### Request status

```text
curl --fail-with-body "https://urouter.net/gateway/v1/requests/YOUR_REQUEST_ID" \
  -H "Authorization: Bearer $UROUTER_API_KEY"
```

## Retry deliberately

Disable SDK automatic retries for generation. Keep a unique Idempotency-Key for each logical operation and save it before sending. Keys are nonblank strings of at most 200 characters. Music and video require a key; speech accepts one for recovery.

- Reuse the original key with an identical payload after an uncertain outcome. Different payloads under the same key conflict. Do not use a new key to force unresolved work through.
- An identical request can replay a saved Responses result or stored image while retained. Media creation replays the owned job. Text without a retained result can return outcome_unknown. Look up the original request before retrying an unknown outcome.
- Errors set x-should-retry: false. Retry-After is a wait hint, not permission to repeat an unresolved generation. Back off polling reads and stop once a terminal outcome is known.
- If the request remains unresolved, contact contact@u.software with the request ID or job ID. Never include an API key.

### TypeScript status recovery

```text
async function recover(requestId: string) {
  if (!/^req_[a-f0-9]{32}$/.test(requestId)) {
    throw new Error("Expected a saved uRouter request ID");
  }
  const response = await fetch(
    "https://urouter.net/gateway/v1/requests/" + requestId,
    { headers: { Authorization: "Bearer " + process.env.UROUTER_API_KEY } },
  );
  if (!response.ok) throw new Error("Status lookup failed: " + response.status);
  const { state, financial_state, settled_usd, failure_code } = await response.json();
  return { state, financial_state, settled_usd, failure_code };
}
```

### Python status recovery

```text
import os
import re
import urllib.request
import json

def recover(request_id):
    if not re.fullmatch(r"req_[a-f0-9]{32}", request_id):
        raise ValueError("Expected a saved uRouter request ID")
    request = urllib.request.Request(
        "https://urouter.net/gateway/v1/requests/" + request_id,
        headers={"Authorization": "Bearer " + os.environ["UROUTER_API_KEY"]},
    )
    with urllib.request.urlopen(request, timeout=15) as response:
        status = json.load(response)
    return {key: status.get(key) for key in
            ("state", "financial_state", "settled_usd", "failure_code")}
```
