# Mindcase: errors and run outcomes

The full reference for error codes, rate limits, failed and empty runs, retries and
charges. Start at https://mindcase.co/skills.md; this page is what it links to.

## Errors

Every error response has the same shape. Branch on `error.code`: it is stable and
never reworded. `error.message` is for people and may change.

```json
{
  "error": {
    "code": "insufficient_balance",
    "message": "Your balance is $0. Please top up at https://console.mindcase.co/account?tab=billing (this API costs $0.0020/row).",
    "request_id": "req_6f1c2ab4e0d94c7f9a3b1d5e8c7f2a10",
    "retryable": false,
    "details": {
      "balance_usd": 0,
      "price_per_row_usd": 0.002,
      "top_up_url": "https://console.mindcase.co/account?tab=billing"
    }
  },
  "detail": "Your balance is $0. Please top up at https://console.mindcase.co/account?tab=billing (this API costs $0.0020/row)."
}
```

- `retryable`: whether the same request, unchanged, can succeed later. When it
  is `true`, wait for the `Retry-After` header if there is one
  (also in `details.retry_after_seconds`), otherwise back off.
- `request_id`: quote it when you contact support.
- `details`: optional, depends on the code.
- `detail` (and top-level `errors` on parameter errors) are kept for existing
  integrations. New code should read `error`.

| HTTP | `error.code` | Retry? | Meaning |
|------|--------------|--------|---------|
| 400 | `bad_request` | no | The request could not be understood |
| 400 | `invalid_idempotency_key` | no | The `Idempotency-Key` header is malformed (must be 8-255 printable characters) |
| 401 | `missing_api_key` | no | No API key was sent. Add `Authorization: Bearer mk_live_...` |
| 401 | `invalid_api_key` | no | The key isn't recognised. Check it was copied in full |
| 401 | `revoked_api_key` | no | The key was revoked. Create a new one in the console |
| 401 | `expired_api_key` | no | The key was replaced and its grace period ended. Use your new key |
| 401 | `unauthorized` | no | No valid credentials were sent |
| 402 | `insufficient_balance` | after top-up | Your wallet is empty. `details.top_up_url` says where to top up |
| 402 | `funded_balance_required` | after top-up | This API isn't covered by free signup credit |
| 403 | `forbidden` | no | These credentials can't do this |
| 404 | `agent_not_found` | no | Unknown API `group/slug` |
| 404 | `job_not_found` | no | Unknown job, or not yours |
| 404 | `not_found` | no | No such route |
| 405 | `method_not_allowed` | no | This route doesn't accept that HTTP method |
| 409 | `job_not_ready` | yes | The job hasn't finished. `details.status` has its state |
| 409 | `idempotency_in_progress` | yes | A request with this `Idempotency-Key` is still being processed |
| 409 | `conflict` | no | The request conflicts with the current state |
| 410 | `agent_retired` | no | This API has been removed |
| 410 | `gone` | no | This resource no longer exists |
| 413 | `payload_too_large` | no | The request body is too large |
| 415 | `unsupported_media_type` | no | The request body has the wrong content type |
| 422 (some filters 400) | `invalid_params` | no | Parameters were rejected. `details.errors` lists each field, with its own `code`, `field` and `message`. Send only keys listed in the agent's `parameters` (don't copy extra keys from `sample_input`) |
| 422 | `idempotency_key_reused` | no | The `Idempotency-Key` was already used for a different request |
| 429 | `rate_limited` | yes | Too many requests this minute. `details.retry_after_seconds` says when to retry |
| 429 | `too_many_concurrent_runs` | yes | Too many runs in progress. Start the next when one finishes |
| 500 | `internal_error` | yes | Something unexpected failed on our side |
| 502 | `bad_gateway` | yes | A service we depend on returned a bad response |
| 503 | `balance_unavailable` | yes | We couldn't read your balance. This says nothing about your money |
| 503 | `service_unavailable` | yes | Temporarily unavailable |
| 504 | `gateway_timeout` | yes | A service we depend on timed out |

If your code checks for a bad key, handle all four 401 key codes
(`missing_api_key`, `invalid_api_key`, `revoked_api_key`, `expired_api_key`), not
just `invalid_api_key`. Until 2026-11-10 every key failure returned
`invalid_api_key`. After 20 failed key attempts in a minute from one address, you
get `429 rate_limited` until the minute passes.

### Rate limits

Limits are per account, shared across all your API keys.

| What | Default |
|------|---------|
| Starting a run (`POST .../run`) | 60 per minute |
| Everything else (status, results, catalog, balance) | 1,200 per minute |
| Runs in progress at once | 500 |

Checking on runs never uses up your allowance for starting them. Responses to
keyed requests carry `RateLimit-Limit`, `RateLimit-Remaining` and
`RateLimit-Reset` (seconds until a slot frees), also sent as `X-RateLimit-*`,
plus `RateLimit-Policy`. On a `429`, wait for `Retry-After`. If it's absent, back
off from 0.5 seconds, doubling each time, with some random jitter.

The cap of 500 runs in progress is yours, checked when you start a run. Separately,
a run can fail with `internal_error` if our own provider is busy ("We're running
too many jobs at once on our side"). That run is `retryable` after
`retry_after_seconds` (60s).

### Reporting a problem: send us the `X-Request-ID`

Every API response carries an `X-Request-ID` header, including errors. On an
error the same value is in `error.request_id`. Quote it when you contact
support and we can find that exact call.

`POST /run` and `GET /jobs/{id}` also return `request_id`, the id of the call
that started the run (`null` on runs from before this existed). You may send
your own `X-Request-ID` (8-64 letters, digits, `.`, `_` or `-`) and we will use
it. Anything else is replaced with one of ours.

### When a run fails

A run that starts and then fails is not an HTTP error: the job comes back with
HTTP 200 and `status: "failed"` (or `rejected_balance`). `error` stays a sentence you
can show the user, and `error_code` and `retryable` sit next to it on
`GET /jobs/{job_id}`, `GET /jobs/{job_id}/results`, `?wait=true` responses and
`GET /jobs`.

| `error_code` | Retry? | Meaning |
|--------------|--------|---------|
| `invalid_input` | no | Something in your input is wrong. Fix it first |
| `location_unsupported` | no | We couldn't use that location (a store that simply doesn't deliver there completes with `no_results` instead) |
| `target_not_found` | no | The company, page or profile doesn't exist at the source |
| `request_rejected` | no | This request was refused. `error` says why |
| `collection_timeout` | yes | Collecting this data took too long |
| `collection_failed` | yes | We couldn't collect this data |
| `insufficient_balance` | after top-up | The wallet couldn't pay for this run |
| `agent_retired` | no | This API has been removed |
| `internal_error` | see `retryable` | Something failed on our side |

### When a run finds nothing

A run that found nothing is a success, not a failure: `status: "completed"`,
`row_count: 0`, `stop_reason: "no_results"` and a `notice` of type `no_results`.
The notice says what to try next, based on what you asked for: broader terms for
a search, a different location for products, checking a link or ID for a lookup,
or a longer period or a later run for a time-limited request. If your input can't be used at all, the run is `failed` with
`fault: "input"` instead, and if something broke it is `failed` with
`fault: "mindcase"`.

A run over a list of inputs (links, searches, pincodes) can also say which inputs
came back empty. The `notice` then reads like "Found data for 1 of 5 links. Link 2
doesn't exist; links 3 and 5 had no data." and `notice.details.inputs` lists each
of your inputs as `{param, value, status, rows}`. `status` is `found`, `empty`,
`not_found`, `invalid_input`, `not_serviceable`, `closed` or `failed` (failed on our
side; run that one again later). You are charged only for the rows delivered. A run
where no input returned anything is `completed` with `no_results` when the inputs
don't exist, the store doesn't deliver to the location you asked for, or the inputs
were simply empty, and `failed` when an input couldn't be used. A target that doesn't
exist (a deleted post, a profile that isn't there) gives the same outcome on every API:
`completed`, `stop_reason: "no_results"`, not charged, with the `notice` saying it may
have been deleted, or be private or restricted. A store that doesn't deliver there is
the same, with the `notice` saying so and suggesting another location.

### Should I retry, and was I charged?

Every finished job states what it cost, on the same responses:

- `charged`: `true` if money left your wallet, `false` if not.
- `usd_charged`: the actual amount. A run that stopped partway is charged only
  for what it delivered, and the amount never exceeds what your wallet held.
- `billing_status`: `settled`, or `settling` if the charge hasn't landed yet. While
  it is `settling`, `charged` and `usd_charged` are `null`. Fetch the job again shortly.

A failed run is not charged. Most runs that return no rows cost nothing, but an API
with a per-page search fee can still charge for the search, so read `usd_charged`.

| Situation | How you see it | `retryable` | Wait |
|-----------|----------------|-------------|------|
| Rate limited | HTTP 429 `rate_limited` | yes | `retry_after_seconds` |
| Too many of your runs in progress | HTTP 429 `too_many_concurrent_runs` | yes | `retry_after_seconds` (5s), or until one of your runs finishes |
| We couldn't read your balance | HTTP 503, or a `failed` job with `balance_unavailable` | yes | 5s |
| Something failed on our side | `failed` job | yes | 30s |
| Bad parameters | HTTP 422 | no | Fix them first |
| Not enough balance | HTTP 402, or `rejected_balance` | no | Add balance first |
| Bad API key | HTTP 401 | no | Fix the key first |
| Run ended short with rows | `completed` with a `stop_reason` | no | Run again on purpose for the rest |

The loop: retry only when `retryable` is `true`, and wait `retry_after_seconds` first
(HTTP errors also send it as `Retry-After`). If you have no wait time, back off
exponentially with jitter, up to 30s, and stop after about 5 attempts. Send an
`Idempotency-Key` on `POST /run` so a retry can't start and charge a second run.

### Whose problem it was

`fault` says who needs to act. It is on failed jobs, on runs that ended short,
and on every HTTP error as `error.fault`.

| `fault` | What it means | What to do |
|---------|---------------|------------|
| `input` | Something in the request can't work as sent, or the wallet can't pay | Fix the request or add balance first. Retrying it unchanged won't help |
| `mindcase` | A problem on our side, including a website we could not collect from | Follow `retryable`; if it keeps failing, contact support |

`fault` is never a supplier name, and `error` stays a sentence.

### Why a run stopped

Every finished job has one `stop_reason` and a plain-English `stop_message`,
next to `status`, on the same responses. `status` and `notice` are unchanged.
Branch on `stop_reason`; show `stop_message`. A run that delivered rows is
`retryable: false`, because retrying buys those rows again. To get the rest of a
partial run, run it again on purpose.

| `stop_reason` | `status` | What it means | What to do | Retry? | Charged for |
|---------------|----------|---------------|------------|--------|-------------|
| `completed` | `completed` | Everything you asked for was delivered | Nothing | no | Delivered rows |
| `no_results` | `completed` | No rows for this request, and that is a valid answer | Follow the `notice` | no | Nothing, except a flat search fee |
| `row_limit_reached` | `completed` | Stopped at the row limit for this run | Raise the limit for more | no | Delivered rows |
| `balance_exhausted` | `completed` | Your balance reached $0 mid-run | Add balance, run again for the rest | no, add balance | Delivered rows |
| `time_limit` | `completed` / `failed` | Hit a time limit | Run again, narrower if you can | yes if no rows | Delivered rows |
| `collection_failed` | `completed` / `failed` | We couldn't collect this data | Try again later | yes if no rows | Delivered rows |
| `budget_guard` | `completed` / `failed` | Stopped to protect you from runaway cost | Narrow the search | no, narrow it | Delivered rows |
| `interrupted` | `completed` / `failed` | Our system was interrupted mid-run | Run again | yes if no rows | Delivered rows |
| `internal_error` | `completed` / `failed` | Something failed on our side | Try again | yes if no rows | Delivered rows |
| `invalid_input` | `completed` / `failed` | Some or all of your input could not be used | Fix it, then run again | no, fix it | Delivered rows |
| `cancelled` | `cancelled` | The run was stopped before it finished | Nothing | no | Delivered rows |
| `insufficient_balance` | `rejected_balance` | Your balance was too low to start | Add balance, then run again | no, add balance | Nothing |
