# Claude

Run Anthropic's Claude models on the Claude harness with a Claude plan login, an Anthropic API key, a custom base URL, or Vertex AI.

## Overview

The Claude harness runs Anthropic's models with Claude Code's own prompts and tools. Each model family runs on the harness its vendor built for it, so a Claude model receives the instructions and tool shapes it was trained on.

A `ClaudeClient` carries the credentials. `Nanocode::builder().claude(client)` adds the harness to the runtime, and `.backend::<Claude>()` spawns an agent on it.

#### Rust

```rust
use nanocode::{Claude, ClaudeClient, ClaudeModel, Nanocode};

// 1. Sign requests with the login that /login claude stores
let runtime = Nanocode::builder()
    .claude(ClaudeClient::default())
    .build_async()
    .await?;

// 2. Spawn a Claude agent on Sonnet
let (agent, _events) = runtime
    .spawn()
    .backend::<Claude>()
    .model(ClaudeModel::Sonnet55)
    .build()
    .await?;
```

#### TypeScript

```ts
// A Claude session on the Messages API. dispatch adds the key.
await runtime.open(
  {
    endpoint: 'https://api.anthropic.com/v1/messages',
    model: 'claude-sonnet-5-5',
    provider: 'claude',
    scope: 'owner',
    session_id: Contracts.sessionUuid(),
    subscription: false,
  },
  {
    dispatch: (request, signal) =>
      fetch(request.endpoint, {
        body: request.body as Uint8Array<ArrayBuffer>,
        headers: [
          ...request.headers,
          ['x-api-key', process.env.ANTHROPIC_API_KEY ?? ''],
        ],
        method: 'POST',
        redirect: 'error',
        signal,
      }),
  },
)
```

## Walkthrough

The steps below sign in with a Claude plan and run one Claude turn from Rust.

### Sign In

Start `nc` and run `/login claude`. The command opens the Claude sign-in page and saves the login to `claude.json` in the nanocode home. If no nanocode login exists, the harness uses an existing Claude Code login.

```bash
# 1. Open the terminal UI, which defaults to claude-opus-5-5
nc

# 2. Inside nc, sign in with the Claude plan
/login claude
```

### Run the Agent

`ClaudeClient::default()` reads the nanocode login first, then the Claude Code login, when a request needs it.

```rust title="main.rs"
use nanocode::{Claude, ClaudeClient, ClaudeModel, Nanocode};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>>
{
    // 1. Build the runtime with the Claude login
    let runtime = Nanocode::builder()
        .claude(ClaudeClient::default())
        .build_async()
        .await?;

    // 2. Spawn a Claude agent and run one turn
    let (agent, _events) = runtime
        .spawn()
        .backend::<Claude>()
        .model(ClaudeModel::Sonnet55)
        .build()
        .await?;
    let result = agent
        .prompt("Name the harness in one sentence.")
        .await?
        .await?;
    println!("{}", result.final_message());

    runtime.shutdown().await?;
    Ok(())
}
```

## Recipes

### Pick a Model

`ClaudeModel` lists the Claude harness models. `HarnessFamily::Claude.default_model()` is `ClaudeModel::Opus55`. `FromStr` accepts the identifier, or `opus`, `sonnet`, `haiku`, `fable`, and `mythos` for the 5.x models.

| Variant    | Identifier          |
| ---------- | ------------------- |
| `Fable51`  | `claude-fable-5-1`  |
| `Haiku55`  | `claude-haiku-5-5`  |
| `Mythos51` | `claude-mythos-5-1` |
| `Opus55`   | `claude-opus-5-5`   |
| `Sonnet55` | `claude-sonnet-5-5` |

The 4.x models `Opus46`, `Sonnet46`, and `Haiku45` are also available. [Models and Types](/docs/api/rust/types#claudemodel) lists their thinking levels.

```bash
# Pick a Claude model by its short name
nc -m sonnet
```

### Use an API Key

`ClaudeClient::with_api_key` signs requests to a Messages API endpoint with an Anthropic API key. `ClaudeClient::subscription_or_key` uses the Claude login when one exists and the key otherwise.

```rust
use nanocode::ClaudeClient;

// 1. Always use the API key
let claude = ClaudeClient::with_api_key(
    "https://api.anthropic.com/v1/messages",
    std::env::var("ANTHROPIC_API_KEY")?,
);

// 2. Prefer the Claude login, and fall back to the key
let claude =
    ClaudeClient::subscription_or_key(&std::env::var("ANTHROPIC_API_KEY")?)?;
```

In `nc`, `NANOCODE_ANTHROPIC_API_KEY` or `ANTHROPIC_API_KEY` sets the fallback key behind the login.

```bash
# Use the key when no Claude login exists
ANTHROPIC_API_KEY=... nc -m sonnet
```

### Set a Base URL

`NANOCODE_ANTHROPIC_BASE_URL` sends Claude requests to another Messages API, such as a gateway. Requests go to the base URL followed by `/messages`. The credential is the first of these variables that is set:

* **`NANOCODE_ANTHROPIC_API_KEY`:** Sent as an API key.
* **`NANOCODE_ANTHROPIC_AUTH_TOKEN`:** Sent as a bearer token.
* **`ANTHROPIC_API_KEY`:** Sent as an API key.
* **`ANTHROPIC_AUTH_TOKEN`:** Sent as a bearer token.

```bash
# A gateway that serves the Messages API under /v1
NANOCODE_ANTHROPIC_BASE_URL=https://llm.example.com/v1 ANTHROPIC_AUTH_TOKEN=... nc
```

In Rust, pass the full Messages URL to `ClaudeClient::with_api_key`.

```rust
use nanocode::ClaudeClient;

// The endpoint is the base URL followed by /messages
let claude = ClaudeClient::with_api_key(
    "https://llm.example.com/v1/messages",
    std::env::var("ANTHROPIC_API_KEY")?,
);
```

### Use Vertex AI

`CLAUDE_CODE_USE_VERTEX` set to `1`, `true`, or `yes` sends Claude requests to Vertex AI with Google Application Default Credentials. It takes precedence over a base URL and an API key.

* **Endpoint:** `ANTHROPIC_VERTEX_BASE_URL` replaces the regional Vertex AI URL.
* **Project:** `ANTHROPIC_VERTEX_PROJECT_ID`, then `GOOGLE_CLOUD_PROJECT`, then the project of the credentials.
* **Region:** `CLOUD_ML_REGION`, then `GOOGLE_CLOUD_LOCATION`. Defaults to `global`.

```bash
# 1. Create Application Default Credentials
gcloud auth application-default login

# 2. Run Claude on Vertex AI in one project
CLAUDE_CODE_USE_VERTEX=1 ANTHROPIC_VERTEX_PROJECT_ID=my-project nc -m sonnet
```

> [!NOTE]
> Claude on Vertex AI is available in `nc` only. The `nanocode` crate has no Vertex AI constructor for `ClaudeClient`.

## More

- [ClaudeClient](/docs/api/rust/ClaudeClient): Reference for the Claude client.

- [Codex](/docs/models/codex): Run OpenAI's models on the Codex harness.

- [Google](/docs/models/google): Run Gemini models on the Antigravity harness.

- [Terminal UI](/docs/guides/cli): Sign in and run agents from nc.
