# SpawnBuilder

Select the harness, model, reasoning, and instructions of a new root agent, then build it.

`runtime.spawn()` returns a `SpawnBuilder`. Settings that the builder does not set come from the runtime: its default harness, model, and thinking. A different harness without a model uses that harness's default model.

```rust
use nanocode::{Claude, ClaudeModel, Thinking};

// Spawns a Claude agent with its own effort and instructions
let (agent, mut events) = runtime
    .spawn()
    .backend::<Claude>()
    .model(ClaudeModel::Haiku55)
    .thinking(Thinking::Low)
    .instructions("Reply in one sentence.")
    .build()
    .await?;
```

## Signature

```rust
// B is Unselected until backend selects a backend kind
pub struct SpawnBuilder<'a, B = Unselected>;

impl Nanocode {
    pub fn spawn(&self) -> SpawnBuilder<'_>;
}
```

## Parameters

### backend

* **Type:** `fn backend<B: BackendKind>(self) -> SpawnBuilder<'a, B>`
* **Default:** the runtime harness

Selects the harness by its backend kind, `Codex` or `Claude`. This method is available before a backend is selected, and it unlocks `model`.

```rust
use nanocode::Codex;

// Spawns on the Codex harness with its default model
let (agent, events) = runtime.spawn().backend::<Codex>().build().await?;
```

### model

* **Type:** `B::Model`
* **Default:** the runtime model on the same harness, or the harness default model

Sets the model. The type follows the backend kind: `Model` for `Codex` and `ClaudeModel` for `Claude`. See [Models and Types](/docs/api/rust/types).

```rust
use nanocode::{Codex, Model};

// Spawns on Model::Luna
let (agent, events) = runtime
    .spawn()
    .backend::<Codex>()
    .model(Model::Luna)
    .build()
    .await?;
```

### thinking

* **Type:** `Thinking`
* **Default:** the runtime thinking for the runtime model, or the model's default

Sets the reasoning effort. `build` rejects a level that the model does not support.

```rust
use nanocode::Thinking;

// Uses high effort for this agent only
let (agent, events) =
    runtime.spawn().thinking(Thinking::High).build().await?;
```

### reasoning_mode

* **Type:** `ReasoningMode`
* **Default:** `ReasoningMode::Standard`

Sets the reasoning mode. `ReasoningMode::Pro` is not available on every model.

```rust
use nanocode::ReasoningMode;

// Requests pro reasoning
let (agent, events) = runtime
    .spawn()
    .reasoning_mode(ReasoningMode::Pro)
    .build()
    .await?;
```

### instructions

* **Type:** `impl Into<Arc<str>>`

Sets instructions for this agent, in place of the runtime instructions.

```rust
// Gives this agent a role
let (agent, events) = runtime
    .spawn()
    .instructions("You review Rust code.")
    .build()
    .await?;
```

## Returns

### build

* **Type:** `Result<(Agent, AgentEvents), NanocodeError>`

`build` is async. It returns the [`Agent`](/docs/api/rust/Agent) and the stream of its events. With durability on, the new session is journaled in the runtime store.

```rust
// Reads the session ID of the new agent
let (agent, mut events) = runtime.spawn().build().await?;
println!("{}", agent.session_id());
```

## Errors

### NanocodeError

`build` returns `NanocodeError::InvalidRequest` when the model does not belong to the selected harness, when the thinking level is not supported, or when the harness is not configured.

```rust
use nanocode::{Claude, ClaudeModel, NanocodeError, Thinking};

// Claude Haiku 4.5 supports Thinking::None only
let spawned = runtime
    .spawn()
    .backend::<Claude>()
    .model(ClaudeModel::Haiku45)
    .thinking(Thinking::High)
    .build()
    .await;
if let Err(NanocodeError::InvalidRequest(message)) = spawned {
    eprintln!("{message}");
}
```
