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

# How routing works

> How SafeConduct chooses a provider for each captcha task, what happens when a provider fails, and how the choice learns from results.

Every task is routed on its own. The router looks at the captcha type and the site it is for, lists the providers that can take it, ranks them, and tries them in order until one returns a token or the task's limits are reached.

```mermaid theme={null}
flowchart TD
  A[Task arrives: type, site, sitekey] --> B[List the providers that can take it]
  B --> C[Score each one on this site]
  C --> D[Rank them by the key's policy]
  D --> E[Try the first provider]
  E -->|token| F[Done: charge that provider's price]
  E -->|fails or times out| G{Attempts, providers or time left?}
  G -->|yes| H[Retry, or try the next provider]
  H --> E
  G -->|no| I[Task fails: nothing is charged]
```

## 1. Which providers can take the task

A provider is a candidate when all of these hold:

* it offers the captcha type;
* it accepts the task as sent, for example with your own proxy;
* the API key and the account don't exclude it;
* it isn't paused (see [When a provider is unwell](#when-a-provider-is-unwell)).

If the request names providers in `routing.providers`, only those are candidates, unless `routing.fallback` is `true`.

## 2. Scores, per site

For each candidate the router keeps two measurements: **how often its tokens work** and **how long it takes** (the time within which 90% of its solves finish).

Both are measured as close to your task as the data allows:

1. on the same captcha type, site and sitekey;
2. else on the same captcha type and site;
3. else on the captcha type anywhere;
4. else a cautious starting value, for a provider with no history.

A level is used only when it has enough attempts to mean something. The success figure is also a cautious one: ten solves out of ten rank below 950 out of 1,000, so a new provider can't win on a lucky start.

"The site" is the registrable domain of `websiteURL`: `www.example.com` and `shop.example.com` count as `example.com`.

## 3. Policies

The policy decides the order. It is a [setting of the API key](https://docs.safeconduct.ai/settings.md#per-api-key); the default is `balanced`.

| Policy | Ranks providers by |
| - | - |
| `balanced` | The expected cost of a **working** token: the price divided by the success rate on this site, with a small penalty for slow answers. |
| `cheapest` | The lowest price. |
| `fastest` | The shortest solve time. |
| `pinned` | Nothing: exactly the providers you ticked on the key, in your order. |

With the first three, a provider whose success on the site is poor is ranked after every provider that does well, whatever its price or speed. So `cheapest` means the cheapest provider that works there.

## 4. Attempts and fallback

The router tries the first provider. What happens after a failure depends on the failure:

| What went wrong | What the router does |
| - | - |
| The request itself is wrong (a missing or invalid field) | Stops. No other provider would do better. You get `BAD_PARAMS`. |
| The provider couldn't solve it, or took too long | Tries again, on the same provider once for an unsolvable captcha, then on the next provider. |
| The provider is overloaded or refuses us | Moves to the next provider at once. |

Three limits bound a task, all [settings of the key](https://docs.safeconduct.ai/settings.md#per-api-key): **3 attempts**, **2 providers** and **120 seconds** by default. When they are used up, the task fails with the last error, or `TIMEOUT`. When no provider can take the task at all, it fails with `NO_PROVIDER_AVAILABLE`, and the error message says why.

All of this happens inside your one request. A failed attempt, a retry and a fallback cost nothing: you pay once, for the provider that returned the token.

## When a provider is unwell

A provider that fails several times in a row, or asks to be sent less, is paused for a short time. While paused it gets no tasks. Then it is sent one task as a test: if that works it is back, if not the pause gets longer. A provider's bad hour therefore costs you a few slow tasks, not an hour of failures.

## How the router learns

* **Every attempt counts.** Each success, failure and solve time updates that provider's figures for that site.
* **Your reports count.** A provider can return a token the site then refuses. Only you see that. [Report it](https://docs.safeconduct.ai/quickstart.md#if-a-site-rejects-the-token) within an hour and it counts as a failure for that provider on that site.
* **A small share of tasks explores.** Now and then the router sends a task to the least-measured candidate first, so a provider that got better is noticed. Tasks with the policy `pinned`, or with providers named in the request, are never used for this.

## See the decision

Every answer of the native API is a receipt: `provider` is who solved it, `cost` is what you paid, and `attempts` lists each try in order with its provider, its duration and, for a failed one, the error code.

```json theme={null}
{
  "status": "ready",
  "policy": "balanced",
  "provider": "capsolver",
  "cost": "0.0008",
  "attempts": [
    { "n": 1, "provider": "capmonster", "code": "UNSOLVABLE", "durationMs": 41020 },
    { "n": 2, "provider": "capsolver", "durationMs": 13382 }
  ]
}
```

The figures the router works from are public: success rates, solve times and prices per provider are on [Provider statistics](https://safeconduct.ai/providers) and in `GET /v1/stats/providers`, without a key.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.