# Reason

Add the exchange to Reason as a Custom provider. Reason then sends its model requests to the exchange. The exchange auctions each request, sets a cap before the first token and charges for the measured use.

Reason is a hosted coding agent, built on Pi. The agent runs on the Reason servers, and its commands run on your computer. The model requests come from the Reason servers, so the base URL must be the public URL of the exchange.

## Print the setup with the Liquid CLI

`liquid connect reason` prints the values to paste, with your model filled in.

```bash
curl -fsSL https://inference.ai.exchange/cli/install.sh | bash
liquid login
liquid connect reason
```

## Before you start

- A workspace with a balance and a `spend` key. See [API keys](https://inference.ai.exchange/docs/keys.md).
- A Reason plan that permits an API key. Reason permits API keys on its Team plan.

## 1. Install Reason and log in

```bash
curl -fsSL https://reasonmachines.com/install.sh | bash
reason login
```

## 2. Connect a Custom provider

Do 2a or 2b. Both save the same four values.

- The base URL ends in `/v1`. Reason adds `/chat/completions` to it.
- The API format is `openai-completions`. This is the Reason default, and the format that this setup is tested with.
- List the model IDs. If the list is empty, Reason shows only part of the book.

### 2a. In the Reason settings

Open [Reason's model settings](https://reasonmachines.com/customize?settings=1&tab=models) (**Settings → Models**). Then select **Custom → ⋯ → Add another API key**. Enter these values in the order of the dialog:

**API key**

```
sk-liquid-...
```

**Base URL**

```
https://router.inference.ai.exchange/v1
```

**API format**

```
openai-completions
```

**Model IDs**

```
liquid.auto
```

### 2b. From a terminal

This command calls the Reason API with the login of the Reason CLI and the key in `LIQUID_API_KEY`. It replaces the Custom provider of your Reason organization.

```bash
creds="$HOME/.config/reason/credentials.json"
curl --fail-with-body -sS -X PUT \
  "https://api.reasonmachines.com/v3/organizations/$(jq -r .orgId "$creds")/provider-credentials/custom-endpoint" \
  -H "Authorization: Bearer $(jq -r .accessToken "$creds")" \
  -H "Content-Type: application/json" \
  -d "$(jq -n --arg key "$LIQUID_API_KEY" --arg models "liquid.auto" '{
    key: $key,
    config: {
      CUSTOM_ENDPOINT_BASE_URL: "https://router.inference.ai.exchange/v1",
      CUSTOM_ENDPOINT_API: "openai-completions",
      CUSTOM_ENDPOINT_MODELS: $models
    }
  }')"
```

If the command fails with `401`, run `reason login` again.

## 3. Run it

Reason adds the prefix `byok:custom-endpoint:` to each model name.

```bash
reason --model byok:custom-endpoint:liquid.auto
```

To run one task and print the result, add a prompt: `reason "fix the flaky test" --model byok:custom-endpoint:liquid.auto`.

## Notes

- Receipts show the client as **Reason**, from the `Reason` user agent.
- Reason cannot send the `x-liquid-cap-usd` header. `liquid.auto` applies its own cap. To set a different cap, use a preset that sets one.
- Reason stores the key on its servers. Give it a key with a spending limit. See [API keys](https://inference.ai.exchange/docs/keys.md).
- This setup is tested with Reason 0.2.260.

## Troubleshooting

| Symptom                                                   | Cause and fix                                                                  |
| --------------------------------------------------------- | ------------------------------------------------------------------------------ |
| Reason refuses the key with `api_keys_require_team_plan`. | Reason connects an API key only on its Team plan. Change the plan in Reason.   |
| The provider check fails.                                 | The URL is missing `/v1`, or it is not the public URL of the exchange.         |
| The model does not appear in the picker.                  | The name is not in the **Model IDs** list. Add it and save the provider again. |
| A request is refused for lack of funds.                   | The workspace balance cannot cover the cap. Add funds on the **Credits** page. |

## Next steps

- [Pi](https://inference.ai.exchange/docs/connect-pi.md): the agent that Reason is built on, with the exchange as its provider.
- [API keys](https://inference.ai.exchange/docs/keys.md): scopes and spending limits.
- [Purchasing rules](https://inference.ai.exchange/docs/rules.md): cost caps and service floors.
