> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mireye.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Retries

> When to retry a failed request, how long to wait, and when to stop.

Some errors clear on their own: wait, then send the request again. Others fail the
same way every time: fix the request or ask the account owner. This page says which
is which. Every code here is described in [Errors](https://docs.mireye.ai/api-reference/errors.md).

## Start with `next_action`

Every error listed in [Errors](https://docs.mireye.ai/api-reference/errors.md) says what to do next in `next_action`,
and in words in `agent_instruction`.
Follow it first. The rules below cover what it does not say, such as how to back off.

| `next_action` | What to do |
| - | - |
| `retry_after` | Wait `retry_after_s` seconds (the `Retry-After` header), then send the same request with the same `Idempotency-Key`. |
| `retry_with_backoff` | Retry with exponential backoff and jitter, with the same key. |
| `fix_request` | Change the request first. Unchanged, it fails the same way. |
| `ask_user` | Stop and show the person `user_message`. They must act before a retry can succeed. |
| `stop` | Don't resend. Tell the user. |
| `contact_support` | Don't resend. [Report the problem](https://docs.mireye.ai/api-reference/reports.md) with `POST /v1/reports` or `mireye_report_problem`, giving the response's `X-Request-ID`. |

See [What to do next](https://docs.mireye.ai/api-reference/errors.md#what-to-do-next) for every field.

## The rules

* **Honour `Retry-After`.** It is a number of seconds. Wait at least that long.
  Always use the value from the latest response, not an earlier one.
* **No `Retry-After`, but `retryable` is `true`?** Back off exponentially with
  jitter, and cap the wait. For example: 1 s, 2 s, 4 s, up to 60 s, each multiplied
  by a random factor.
* **Never retry `retryable: false` or any `402`.** The same request fails the same
  way. Fix the input, or ask the account owner to resolve the account.
* **Retry paid requests with the same `Idempotency-Key`.** A retry with the same key
  is never charged twice. A new key is new work, billed separately.
* **Send the key on the first attempt.** A paid request without one is new work every
  time. If the first attempt finished, a resend can be charged as well. Without a
  key, don't resend an error that names an `operation_id`. The exception is a `429`
  with `retryable: true`: nothing started.
* **Branch on `retryable`, not on the HTTP status alone.** A paid operation can end
  with a retryable-looking status and `retryable: false`.

A network error or a client timeout is not an answer. Retry with the same key: you
get the saved result, or `409 operation_in_progress` while the work is still running.

## Which errors to retry

### Retry after `Retry-After`

Send the same request, with the same `Idempotency-Key`, once `Retry-After` has passed.

| Code | HTTP | How |
| - | - | - |
| `rate_limited` | 429 | Wait for `Retry-After`. Free calls also count toward the limit. |
| `dedicated_app_unavailable` | 429 | Wait for `Retry-After` (5 s). Nothing ran. |
| `concurrency_limited` | 429 | Wait for `Retry-After`. Nothing ran. Run fewer requests in parallel. |
| `geocode_busy` | 429 | Wait for `Retry-After` (2 s). |
| `enrich_busy` | 429 | Wait for `Retry-After` (2 s). |
| `permits_busy` | 429 | Wait for `Retry-After` (2 s). |
| `search_busy` | 429 | Wait for `Retry-After` (3 s). If `retryable` is `false`, the key is finished, with no charge: send again with a new key. |
| `proximity_busy` | 429 | Wait for `Retry-After` (5 s). |
| `operation_in_progress` | 409 | Same key, after `Retry-After`. Never start duplicate work. |
| `billing_unavailable` | 503 | Only when `retryable` is `true`. Same key, after `Retry-After`. |
| `geocode_upstream_error` | 502 | Wait for `Retry-After` (2 s). |
| `geocode_timeout` | 504 | Wait for `Retry-After` (5 s). |
| `search_unavailable` | 503 | Wait for `Retry-After` (3 s). |
| `search_deadline_exceeded` | 504 | Wait for `Retry-After` (3 s). |

### Retry with backoff

These have no fixed `Retry-After`. Back off with jitter, as in the rules above.

| Code | HTTP | How |
| - | - | - |
| `operation_outcome_unknown` | 503 | Same key, so billing can reconcile. Never start a new operation. |
| `dataset_control_unavailable` | 503 | Same key. |
| `field_requests_unavailable` | 503 | Retry with backoff. |
| `terms_unavailable` | 503 | Retry with backoff. |
| `onboarding_unavailable` | 503 | Retry with backoff. |

### Retry only when `retryable` is `true`

A paid operation can fail after its work started. It then has
`operation_status: "failed"`, `retryable: false` and no `Retry-After`. That key is
finished: the same key returns the same failure. Once the cause is resolved, send the
request with a new key.

| Code | HTTP | How |
| - | - | - |
| `upstream_transient`, `upstream_error`, `proximity_data_unavailable`, `proximity_budget_exhausted` | 502, 503 | Follow the [paid Proximity retry contract](https://docs.mireye.ai/api-reference/proximity.md#retrying-a-paid-operation). |
| `proximity_deadline_exceeded` | 504 | As above. A smaller request can also finish sooner. |

Permits works the same way. See [Permits errors](https://docs.mireye.ai/api-reference/permits.md#errors).

### Don't retry

The same request fails the same way. Change something first.

| Code | HTTP | What to do instead |
| - | - | - |
| `max_cost_too_low` | 402 | Raise `max_cost_usd` to at least `quoted_usd`, or omit it. |
| `insufficient_balance`, `spend_limit_exceeded`, `spending_paused` | 402 | Stop and tell the account owner. Retry once they have added funds, raised the limit or resolved the restriction. |
| `idempotency_conflict` | 409 | The key belongs to another payload. Recover that operation, or use a new key for new work. |
| `operation_failed` | 410 | The key is finished, with no charge. Send the request with a new key, after correcting it if it was refused. |
| `result_not_retained`, `result_expired` | 410 | The saved result is gone. A new key runs the work again, billed separately. |
| `address_not_found`, `address_too_coarse` | 404 | Ask the user for a fuller street address. |
| `geocode_unconfigured`, `proximity_unconfigured`, `upstream_auth` | 502, 503 | Not a caller problem. Report it. |
| `dataset_withdrawn`, `dataset_changed` | 410, 409 | See [Dataset availability](https://docs.mireye.ai/api-reference/errors.md#dataset-availability). |
| Input errors, such as `coord_out_of_bounds`, `fields_unknown`, `invalid_locator`, `search_invalid_filter` | 400, 422 | Fix the fields the error names. |

## Repeated refusals

Retrying a refusal early does not help: it keeps returning the same refusal. See the
"Repeated refusals" notes under
[Dollar billing and rate limits](https://docs.mireye.ai/api-reference/errors.md#dollar-billing-and-rate-limits).

## Example

A retry loop in Python. It reads `Retry-After`, adds jitter, stops on
`retryable: false` or a `402`, and reuses one key for every attempt.

```python theme={null}
import random
import time
import uuid

import requests


class GiveUp(Exception):
    pass


def post_with_retries(url, body, token, max_attempts=6):
    # One key for every attempt of this request. A new key is new, billed work.
    headers = {
        "Authorization": f"Bearer {token}",
        "Idempotency-Key": str(uuid.uuid4()),
    }
    backoff = 1.0
    for _ in range(max_attempts):
        retry_after = None
        try:
            resp = requests.post(url, json=body, headers=headers, timeout=300)
        except requests.RequestException:
            resp = None  # No answer: retry with the same key.

        if resp is not None:
            if resp.ok:
                return resp.json()
            try:
                detail = resp.json().get("detail")
            except ValueError:
                detail = None
            detail = detail if isinstance(detail, dict) else {}
            retryable = detail.get("retryable")
            retry_after = resp.headers.get("Retry-After")
            if resp.status_code == 402 or retryable is False:
                raise GiveUp(detail)  # The same request fails the same way.
            if retryable is None and retry_after is None and resp.status_code < 500:
                raise GiveUp(detail)  # An input error: fix the request.

        if retry_after is not None:
            wait = float(retry_after) + random.uniform(0, 1)  # Never less than asked.
        else:
            wait = min(backoff, 60) * random.uniform(0.5, 1.5)
            backoff *= 2
        time.sleep(wait)
    raise GiveUp({"error": "retries_exhausted"})
```

## For agents

If you are an agent calling Mireye in a loop, follow `next_action` and
`agent_instruction`. Where they say to wait, wait the full `Retry-After`, then send
the request again unchanged, with the same idempotency key. If a paid call had no key
and its error names an `operation_id`, don't resend unless it is a `429` with
`retryable: true`: tell the user. On a `402`, stop
and tell the user. On `retryable: false`, don't resend: fix the request or tell the user.

Hosted MCP follows the same rules:

* A tool error carries `code`, `retryable`, `next_action`, `agent_instruction` and,
  where there is one, `retry_after_seconds`. A value of `0` means no fixed delay: back
  off instead.
* The idempotency key is the tool's `idempotency_key` input. Send one on every paid
  call.
