> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/ch07-model-configuration/7.1-providers-and-api-keys.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# 7.1 Multi-Provider Configuration and API Keys

> **Generated by**: Claude Opus 5 (claude-opus-5\[1m]) **Token usage**: Input \~192,000 tokens, Output \~9,000 tokens (this section) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

## 7.1.1 Configuring Providers

### The wizard route

```bash
openclaw onboard
```

It presents a grouped list of 30-plus providers, and after you choose it writes the config and validates the credential.

### Manual configuration

```json5
{
  agents: {
    defaults: {
      model: {
        primary: "anthropic/claude-opus-4-6",
        fallbacks: ["anthropic/claude-sonnet-4-6", "openai/gpt-5.3"],
      },
    },
  },
}
```

### Model ID format

**A model reference is split on the first `/` to determine the provider** (§16.5.5):

```
anthropic/claude-opus-4-6
└provider┘└──── model id ────┘
```

That matters for providers with multi-segment ids:

```
openrouter/moonshotai/kimi-k2     ✅ provider is openrouter
moonshotai/kimi-k2                ❌ parsed as provider "moonshotai", not found
```

**A frequent mistake** — if a model "doesn't exist," check the prefix first.

### Checking what's available

```bash
openclaw models list          # everything configured
openclaw models status        # with reachability probes
```

## 7.1.2 Key Management and Auth Profiles

### Never put keys in the config file

```json5
{
  // ❌ Don't
  anthropic: { apiKey: "sk-ant-..." },

  // ✅ Do
  anthropic: { apiKey: "${ANTHROPIC_API_KEY}" },
}
```

The reason is mechanical, not just stylistic: **config write-back preserves `${VAR}` references** (§31.6.4). A later `openclaw config set` for something unrelated won't expand your key into plaintext — and from there into git.

### SecretRef for external secret managers

```json5
{
  anthropic: {
    apiKey: { source: "exec", provider: "1password", id: "op://vault/anthropic/key" },
  },
}
```

Three sources are supported: `env`, `file` (with json-pointer addressing), and `exec` (running a command like `op read`).

```bash
openclaw secrets audit        # finds plaintext still lying around
openclaw secrets configure    # migrates plaintext into references
```

**`secrets audit` is worth running once** — its `REF_SHADOWED` check catches the case where you configured a reference but old plaintext at the same location is what actually takes effect (§32.6.3). People routinely think they've finished migrating when they haven't.

### Multiple credentials for one provider

This is where OpenClaw's auth profile system earns its keep. You can configure several accounts for the same provider — an OAuth subscription plus several API keys — and it rotates automatically on failure.

```bash
openclaw auth list            # see configured profiles
openclaw auth status          # health, cooldowns, usage
```

**The ordering encodes a cost strategy** (§16.5.1):

```
OAuth subscription (zero marginal cost)
   → token
      → API key (metered)
```

Within a type it round-robins by least-recently-used, so load spreads rather than pinning one account until it's rate-limited.

> **The practical benefit**: if you have a Claude Pro subscription *and* API keys, OpenClaw uses the subscription first and only falls back to metered billing when it's exhausted. You don't have to manage that.

### What happens on failure

Two rules worth knowing because they explain behavior you'll observe:

| Rule                                          | Consequence                                                                                                                     |
| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| **Only rate-limit errors rotate the key**     | A malformed request fails the same on every key, so rotating would just pollute several accounts' health                        |
| **Timeouts are never charged to the account** | A network timeout isn't the account's fault; charging it would cool down a healthy account and take its sibling models down too |

Failed accounts enter a cooldown: `1min → 5min → 25min → 1h`. Billing and permanent auth failures get a much longer `disabledUntil` (5h base, 24h cap).

## 7.1.3 OAuth Flows

Several providers support OAuth rather than API keys — often giving access to subscription pricing rather than metered.

```bash
openclaw auth login <provider>
```

This opens a browser flow and stores the resulting token.

### Reusing credentials you already have

```bash
openclaw auth sync
```

If you're logged into Claude Code or the Codex CLI on this machine, OpenClaw can **reuse those local credentials** (§16.5.2) rather than making you authenticate again.

### Token refresh

Providers issuing short-lived tokens (GitHub Copilot, for instance) get an automatic refresh loop that **renews 5 minutes early** (§16.5.5). Renewing early rather than on expiry matters — waiting until expiry means in-flight requests have already failed.

Check token health:

```bash
openclaw auth status          # shows expiring / expired
openclaw doctor               # includes auth checks
```

***

## Section Summary

1. **Model IDs split on the first `/` for the provider** — multi-segment ids like `openrouter/moonshotai/kimi-k2` need the prefix, and a missing one is the usual cause of "model not found."
2. **Never put keys in the config file** — use `${VAR}` so write-back preserves the reference and can't expand it into plaintext later.
3. **SecretRef supports `env` / `file` / `exec`** for external secret managers; **run `secrets audit` once** — its `REF_SHADOWED` check catches "you configured a ref but plaintext is what's actually in effect."
4. **Auth profile ordering encodes a cost strategy**: OAuth subscription → token → API key, round-robin within a type. Subscription allowance gets used first, automatically.
5. **Two failure rules explain observed behavior**: only rate limits rotate the key, and **timeouts are never charged to the account** (or a network blip takes down every model under that provider).
6. **`openclaw auth sync` reuses Claude Code / Codex CLI credentials** already on the machine; short-lived tokens **refresh 5 minutes early** so in-flight requests don't fail.
