> For the complete documentation index, see [llms.txt](https://docs.codna.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.codna.ai/concepts/models-and-byok.md). Agents can also search and read this documentation through its [MCP server](https://docs.codna.ai/~gitbook/mcp).

# Models & BYOK

Your model provider key (BYOK) vs. your Codna account key, where keys are stored, and how to pin a model in codna.yaml.

Codna uses two credentials:

| Credential        | Env var                  | Identifies         | Used for                                                                                                                                                                     |
| ----------------- | ------------------------ | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Model key (BYOK)  | e.g. `ANTHROPIC_API_KEY` | Your LLM provider  | The agent's model calls in `codna fix` and `codna review`                                                                                                                    |
| Codna account key | `CODNA_API_KEY`          | Your Codna account | A remote engine, when one is configured. Local runs do not read it. The [GitHub App](https://docs.codna.ai/guides/github-app.md) meters against your linked account instead. |

{% hint style="warning" %}
`CODNA_API_KEY` is not a model key. To run `codna fix` or `codna review` on your machine, add a provider key.
{% endhint %}

## Bring your own key

The provider key stays on your device, in the OS secure store (macOS Keychain, Linux Secret Service) via `keyring`. It is never written to `.env`, passed on the command line, or printed.

```bash
codna key set anthropic     # hidden prompt; the key is not echoed
codna key list              # shows which providers are stored (never the values)
codna key rm anthropic      # remove a stored key
```

For CI, pipe the secret in:

```bash
echo "$ANTHROPIC_API_KEY" | codna key set anthropic --stdin
```

### Supported providers

`codna key set` accepts an alias or the raw `*_API_KEY` name:

| Alias              | Environment variable |
| ------------------ | -------------------- |
| `anthropic`        | `ANTHROPIC_API_KEY`  |
| `openai`           | `OPENAI_API_KEY`     |
| `gemini`           | `GEMINI_API_KEY`     |
| `google`           | `GOOGLE_API_KEY`     |
| `groq`             | `GROQ_API_KEY`       |
| `mistral`          | `MISTRAL_API_KEY`    |
| `openrouter`       | `OPENROUTER_API_KEY` |
| `xai`              | `XAI_API_KEY`        |
| `cursor`           | `CURSOR_API_KEY`     |
| `codna` / `engine` | `CODNA_API_KEY`      |

### Resolution order

Codna resolves a provider key first-match-wins:

1. An environment variable (e.g. `ANTHROPIC_API_KEY`).
2. The OS keychain (anything set with `codna key set`).
3. A legacy `keys.txt` (source checkout only; never read from an installed wheel).

Set `CODNA_DISABLE_KEYCHAIN=1` to skip the keychain and use environment variables only.

## Your Codna account

```bash
codna login
```

`codna login` signs this machine into your Codna account with a device code, creates your Codna key, and stores it in the OS keychain as `CODNA_API_KEY`. It also installs the on-device memory runtime (skipped when already installed) and checks that a provider key is present so `codna fix` can run. Flags: `--token` (or `$CODNA_TOKEN`) for a headless token that skips the browser, and `--no-browser` to print the verification URL and code instead of opening a browser.

Code memory needs no key; its license ships in the wheel. On a fresh machine it requires the one-time free `codna login` above — the device authorization every codna MCP tool checks — which installs the on-device memory runtime, and it runs fully offline thereafter.

## Choosing a model

### Default

By default Codna uses the model id `repository.verified_agentic_v1`. This is not a specific provider model: it is the bundled planner, which takes the provider and model from your configuration. It is the default for `codna fix` and for the Action's `model` input. When no provider is named, the default provider is Anthropic, so `ANTHROPIC_API_KEY` must be set in that case.

### `--model`

Pass a `provider/model_id` pair:

```bash
codna fix . --tests --apply --model openai/$MODEL_ID
codna review . --pr 123 --model anthropic/$MODEL_ID
```

A value with no slash is treated as a bare model id and paired with the default provider.

### Pin a model in `codna.yaml`

For a repo-wide default, add a `model:` block (`codna init` scaffolds one, commented out):

```yaml
model:
  provider: openai            # required when a model block is present
  key: env:OPENAI_API_KEY     # read from the environment; fails closed if unset
```

* `provider` is required and must be a supported alias, or the config is rejected.
* `key: env:NAME` reads `$NAME` at load time and fails closed if it is unset or empty. A plain value is used literally.
* The `model:` block applies to `codna fix`. `codna review` selects its model with the `--model` flag (or the default provider); `triage` and `secure` classification do not call a model.

## Managed vs. self-billed

* **Your key:** your provider key bills to your own provider account, uncapped. This is how the CLI, the MCP server and the GitHub Action run.
* **Managed:** the [GitHub App](https://docs.codna.ai/guides/github-app.md) is keyless per repository. It meters against the Codna account you link the installation to, which includes a managed-model allowance of about $5 a month. Add your own provider key on the account page for uncapped, self-billed usage there too.

## See also

* [Configuration](https://docs.codna.ai/reference/configuration.md) — full `codna.yaml` and environment reference.
* [Privacy and data](https://docs.codna.ai/concepts/privacy.md) — what reaches your provider, and what never leaves your machine.
* [Local runtime](https://docs.codna.ai/reference/operations.md) — `codna status` shows key state.
