# Errors and retries

Every error is JSON with a stable `error` code you can switch on, a `message` written for a person, and, under `/v1/sandboxes`, whether it is worth trying again.

```
{
  "error": "exec_timeout",
  "message": "the command ran longer than its timeout_s of 5 s and was stopped",
  "failure_class": "timeout",
  "retryable": false,
  "limit_s": 5
}
```

## Statuses

| Status | Means | Retry? |
|---|---|---|
| `400` | The request is wrong: `invalid_*`, `bad_json`. The message names the field. | No: fix it |
| `401` | No key, or a revoked or expired one (`unauthorized`) | No |
| `402` | A plan is needed (`plan_required`), a plan limit is reached (`plan_limit`), the plan lacks the feature (`plan_feature`), or payment is due (`payment_required`) | Not until someone acts |
| `403` | The key lacks the scope, or a token is held to other sandboxes | No |
| `404` | `not_found`: no such thing in your organization | No |
| `409` | The state is wrong for this (`not_running`, `starting`, `name_taken`) | Sometimes: see the code |
| `413` | `too_large` | No |
| `423` | `org_frozen`: an owner froze the organization (the kill switch) | Not until it is unfrozen |
| `429` | `rate_limited` (slow down) or a running-sandbox limit (`quota_exceeded`) | Rate limits: yes, after `Retry-After` |
| `501` | `not_available`: this host or region does not have the feature | No |
| `502`, `503` | kosa8's side: `host_failed`, `no_capacity`, `database_busy` | Yes, with backoff |
| `504` | `exec_timeout`: your command outran its `timeout_s` | No: it is your command's time |

## failure_class and retryable

Answers under `/v1/sandboxes` carry `failure_class`, whose fault it was, and `retryable`:

| `ok` | It worked (an exec that exited 0) |
|---|---|
| `exit_nonzero` | Your command ran and exited non-zero. Still a 200. |
| `timeout` | It ran past its limit |
| `oom` | The kernel killed it for memory. Use a bigger [size](https://docs.kosa8.com/concepts/sizes/). |
| `infra` | A host failed, was busy or went away. Retryable, except `501`. |
| `quota` | A plan, payment, running or spending-cap limit |
| `client` | The request was wrong |

An RL trainer running thousands of rollouts can drop `infra` failures instead of scoring them as reward 0. Retryable answers often carry `retry_after_s` and a `Retry-After` header.

## Plan and payment refusals

A `402` carries `upgrade_url`, where a person can fix it, and `agent_message`, one sentence for an AI agent to pass on word for word. An agent should relay it and stop; it should never buy anything itself.

```
{
  "error": "plan_limit",
  "message": "…",
  "upgrade_url": "https://app.kosa8.com/#billing",
  "agent_message": "kosa8 Cloud refused this: … Ask the person to …"
}
```

## Safe retries with Idempotency-Key

Send `Idempotency-Key: <any unique string>` on a create or fork. If the connection drops and you send it again with the same key within 24 hours, you get the first answer back instead of a second sandbox. If the first request is still running, the retry gets `409 idempotency_in_progress`: wait two seconds and ask again.

```
curl -s https://api.kosa8.com/v1/sandboxes \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -H "Idempotency-Key: rollout-1842" -d '{"template":"python"}'
```

## In the SDKs

The Python and Go SDKs retry retryable failures for you, with backoff and the same Idempotency-Key (Python: three tries by default). Every SDK raises a typed error carrying the code, status, `request_id` and `retryable` otherwise: [Python](https://docs.kosa8.com/guides/py-errors/), [JavaScript](https://docs.kosa8.com/guides/js-errors/), [Go](https://docs.kosa8.com/guides/go-errors/), [Ruby](https://docs.kosa8.com/guides/rb-errors/), [Java](https://docs.kosa8.com/guides/java-errors/).

---
Source: https://docs.kosa8.com/errors/
