---
title: Claude Code
description: Use Claude Code with a Lovely Router API key: set the base URL and key, pick Claude models, and keep prompt caching working.
---

Claude Code is the coding agent that runs in your terminal. It speaks the Anthropic Messages API, which Lovely Router serves natively, so you change two settings, the base URL and the key, and keep everything else. You need an [API key](/app/keys) first.

## Set up in one click

On the [API keys](/app/keys) page, use **Use in tools** → **Claude Code**. The dialog shows the setup below with your key filled in, and **Import with CC Switch** adds Lovely Router as a provider in the [CC Switch](https://github.com/farion1231/cc-switch) desktop app, with the models already set. CC Switch must be installed.

Right after you create a key, the dialog already contains it. For an existing key, paste the full key into the dialog: Lovely Router stores only a hash, so it can't show the key again.

## Set the base URL and key

Set the base URL and key, then start Claude Code as usual:

```bash title="Shell"
export ANTHROPIC_BASE_URL=https://lovelyrouter.com
export ANTHROPIC_AUTH_TOKEN=$LOVELY_API_KEY
export ANTHROPIC_MODEL=claude-sonnet-5.5
claude
```

To keep the setting across sessions, put it in `~/.claude/settings.json` instead, with your full key:

```json title="~/.claude/settings.json"
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://lovelyrouter.com",
    "ANTHROPIC_AUTH_TOKEN": "lr-...",
    "ANTHROPIC_MODEL": "claude-sonnet-5.5",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-4.5"
  }
}
```

## Choose models

`ANTHROPIC_MODEL` is the main model and `ANTHROPIC_DEFAULT_HAIKU_MODEL` handles quick background tasks. Any Claude model id from `GET /v1/models` works, for example `claude-opus-5.5`. The [model list](/models) has every id.

## Prompt caching

Claude Code's prompt caching works through Lovely Router: each session stays on the channel that holds its cache. See [Prompt caching](/docs/models/caching).

## Add data tools over MCP

The MCP server at `https://lovelyrouter.com/mcp` gives Claude Code the [data tools](/docs/tools) and the [browser agent](/docs/browser-agent), billed from the same balance. It authenticates with the same key:

```bash
claude mcp add --transport http lovely https://lovelyrouter.com/mcp \
  --header "Authorization: Bearer $LOVELY_API_KEY"
```

This adds it to the current project only. Add `--scope user` to use it in every project. Run `claude mcp list` to check that it connects. To share the server with your team through `.mcp.json`, and for the full list of tools, see [MCP server](/docs/tools/mcp).

## Troubleshooting

| Symptom | Fix |
| --- | --- |
| `401` with `Missing API key` | Claude Code isn't sending the key. Check that `ANTHROPIC_AUTH_TOKEN` is exported in the shell that starts `claude`, or set in `~/.claude/settings.json`. |
| `401` with `Invalid or revoked API key` | The key is mistyped, truncated or revoked. Paste it again, or create a new one. |
| `404` with `Model '...' is not available` | The model id doesn't exist. Copy a Claude model id from `GET /v1/models` or the [model list](/models). |
| `404` on a path containing `/v1/v1/` | `ANTHROPIC_BASE_URL` ends in `/v1`, and Claude Code adds `/v1` itself. Set it to `https://lovelyrouter.com`. |
| `402` | Your balance is empty or the key reached its spend limit. See [Credits & billing](/docs/account/billing). |

Every other status is covered in [Errors](/docs/api/errors).
