# AgentBuilder

https://ailoy.co/docs/sdk/agent-builder/

Reference for AgentBuilder: every method for configuring and building an agent.

`AgentBuilder` creates an [`Agent`](https://ailoy.co/docs/sdk/agent/). Create a builder with a model name, call any of the methods below, then call `build()`. Every method except `build()` returns the builder, so calls can be chained.

**Python**

```python
from ailoy import AgentBuilder

agent = await (
    AgentBuilder("anthropic/claude-haiku-5-5")
    .instruction("Answer in one sentence.")
    .web_search_tool()
    .build()
)
```

**Node.js**

```js
const { AgentBuilder } = require('@brekkylab/ailoy')

const agent = await new AgentBuilder('anthropic/claude-haiku-5-5')
  .instruction('Answer in one sentence.')
  .webSearchTool()
  .build()
```

**Rust**

```rust
use ailoy::agent::AgentBuilder;

let agent = AgentBuilder::new("anthropic/claude-haiku-5-5")
    .instruction("Answer in one sentence.")
    .web_search_tool(vec![])
    .build()
    .await?;
```

> **Note:**
> In Python and Node.js, the builder is changed in place, and `build()` empties it. Create a new builder for each agent.

| Method | Does |
| --- | --- |
| [Constructor](#constructor) | Starts a builder for a model. |
| [`instruction`](#instruction) | Sets the system prompt. |
| [`tool`](#tool), [`tools`](#tools) | Add tools by their description. |
| [`web_search_tool`](#web_search_tool) | Adds `web_search`. |
| [`web_fetch_tool`](#web_fetch_tool) | Adds `web_fetch`. |
| [`sandbox`](#sandbox) | Gives the agent a sandbox and the shell and file tools. |
| [`browser`](#browser) | Gives the agent a browser and the `browser_*` tools. |
| [`skill`](#skill) | Adds a skill from a folder in the sandbox. |
| [`memory`](#memory) | Lets the agent keep notes on the user. |
| [`context_manager`](#context_manager) | Keeps the history under the model's limits. |
| [`thinking_effort`](#thinking_effort) | Sets how much the model reasons. |
| [`output_schema`](#output_schema) | Makes answers match a JSON schema. |
| [`history`](#history) | Starts from a saved conversation. |
| [`build`](#build) | Creates the agent. |

## Constructor

Creates a builder for the given model.

**Python**

```python
AgentBuilder(model: str)
```

**Node.js**

```js
new AgentBuilder(model: string)
```

**Rust**

```rust
AgentBuilder::new(model: impl Into<String>) -> AgentBuilder
```

| Parameter | Type | Description |
| --- | --- | --- |
| `model` | string | The model as `<org>/<name>`, such as `"anthropic/claude-haiku-5-5"`. It runs on the backend set for its org. See [Model providers](https://ailoy.co/docs/guides/model-providers/). |

## instruction

Sets the system prompt. The agent adds its skills and memory notes to it when it builds the system message.

**Python**

```python
.instruction(instruction: str) -> AgentBuilder
```

**Node.js**

```js
.instruction(instruction: string): AgentBuilder
```

**Rust**

```rust
.instruction(inst: impl Into<String>) -> AgentBuilder
```

## tool

Adds one tool by its description. The tool's function must be registered under the same name, with [`register_tool`](https://ailoy.co/docs/sdk/tools/#register_tool) or [`set_tool_provider`](https://ailoy.co/docs/sdk/tools/#set_tool_provider).

**Python**

```python
.tool(desc: ToolDesc) -> AgentBuilder
```

**Node.js**

```js
.tool(desc: ToolDesc): AgentBuilder
```

**Rust**

```rust
.tool(desc: ToolDesc) -> AgentBuilder
```

| Parameter | Type | Description |
| --- | --- | --- |
| `desc` | [`ToolDesc`](https://ailoy.co/docs/sdk/types/#tooldesc) | The tool's name, description and argument schema. |

## tools

Adds several tools at once, such as the list an [MCP registration](https://ailoy.co/docs/sdk/tools/#register_mcp_stdio) returns.

**Python**

```python
.tools(descs: list[ToolDesc]) -> AgentBuilder
```

**Node.js**

```js
.tools(descs: Array<ToolDesc>): AgentBuilder
```

**Rust**

```rust
.tools(descs: impl IntoIterator<Item = ToolDesc>) -> AgentBuilder
```

## web_search_tool

Adds the `web_search` tool, which runs in your program. Node.js: `webSearchTool`.

**Python**

```python
.web_search_tool(engines: list[WebSearchEngine] | None = None) -> AgentBuilder
```

**Node.js**

```js
.webSearchTool(engines?: Array<WebSearchEngine>): AgentBuilder
```

**Rust**

```rust
.web_search_tool(engines: Vec<WebSearchEngineKind>) -> AgentBuilder
```

| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
| `engines` | list of [`WebSearchEngine`](https://ailoy.co/docs/sdk/types/#websearchengine) | All engines | The engines to query. An empty list also means all of them. |

## web_fetch_tool

Adds the `web_fetch` tool, which fetches a URL from your program. Node.js: `webFetchTool`.

**Python**

```python
.web_fetch_tool() -> AgentBuilder
```

**Node.js**

```js
.webFetchTool(): AgentBuilder
```

**Rust**

```rust
.web_fetch_tool() -> AgentBuilder
```

## sandbox

Gives the agent a virtx sandbox and adds the shell and file tools that run in it. The sandbox stays yours: the agent never starts, stops or removes it. See [Give an agent a sandbox](https://ailoy.co/docs/guides/sandbox/).

**Python**

```python
.sandbox(sandbox: virtx.Sandbox) -> AgentBuilder
```

**Node.js**

```js
.sandbox(sandbox: Sandbox): AgentBuilder   // Sandbox from @brekkylab/virtx
```

**Rust**

```rust
.sandbox(sandbox: virtx::Sandbox) -> AgentBuilder
```

**Example**

**Python**

```python
from virtx import Client

client = Client.spawn()
sandbox = await client.create_sandbox("python:3.12-slim-trixie")
agent = await AgentBuilder("anthropic/claude-haiku-5-5").sandbox(sandbox).build()
```

**Node.js**

```js
const { Client } = require('@brekkylab/virtx')

const client = Client.spawn()
const sandbox = await client.createSandbox('python:3.12-slim-trixie')
const agent = await new AgentBuilder('anthropic/claude-haiku-5-5').sandbox(sandbox).build()
```

**Rust**

```rust
use virtx::{Client, SandboxCreateArgs};

let client = Client::spawn_virtx([] as [&str; 0]).await?;
let sandbox = client.create_sandbox(&SandboxCreateArgs::new("python:3.12-slim-trixie")).await?;
let agent = AgentBuilder::new("anthropic/claude-haiku-5-5").sandbox(sandbox).build().await?;
```

## browser

Gives the agent `browser_*` tools that control a Chromium-based browser over CDP. See [Use a browser](https://ailoy.co/docs/guides/using-browser/).

**Python**

```python
.browser(
    *,
    user_browser: str | Path | None = None,
    careful: bool = True,
    start_url: str | None = None,
    isolated: bool = False,
    headless: bool = False,
    executable: str | Path | None = None,
    profile: str | Path | None = None,
) -> AgentBuilder
```

**Node.js**

```js
.browser(options?: {
  userBrowser?: 'chrome' | 'chrome-beta' | 'chrome-dev' | 'chrome-canary' | 'chromium' | 'edge' | 'brave' | { userDataDir: string },
  careful?: boolean,
  startUrl?: string,
  isolated?: boolean,
  headless?: boolean,
  executable?: string,
  profile?: string,
}): AgentBuilder
```

**Rust**

```rust
use ailoy::browser::{Browser, UserBrowser};

.browser(browser: Browser) -> AgentBuilder

// Your own browser, or a separate one:
Browser::user()
Browser::isolated()
// Options, each returning the Browser:
.user_browser(UserBrowser)   // Chrome, ChromeBeta, ChromeDev, ChromeCanary, Chromium, Edge, Brave, Custom(PathBuf)
.careful(bool)
.start_url(impl Into<String>)
.headless(bool)
.executable(impl Into<PathBuf>)
.profile(impl Into<PathBuf>)
```

| Option | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `isolated` | bool | `false` | | `false` uses your own browser. `true` starts a separate browser on first use and closes it with the agent. |
| `user_browser` | string or path | The browser that turned on remote debugging most recently | Your own browser | `"chrome"`, `"chrome-beta"`, `"chrome-dev"`, `"chrome-canary"`, `"chromium"`, `"edge"`, `"brave"`, or the user data directory of any other Chromium-based browser. |
| `careful` | bool | `true` | Both | Tells the agent to use sites as a person would, so bot checks are less likely to block it. |
| `start_url` | string | | Both | The page the agent's tab opens on. |
| `headless` | bool | `false` | Separate browser | Runs without a window. |
| `executable` | path | The first of Chrome, Chromium, Edge or Brave found | Separate browser | The browser executable to start. |
| `profile` | path | A fresh profile, deleted afterwards | Separate browser | A user data directory to keep, so sign-ins last between runs. |

Mixing options for your own browser with options for a separate browser fails at `build()`.

**Example**

**Python**

```python
agent = await AgentBuilder("anthropic/claude-sonnet-5-5").browser(isolated=True).build()
```

**Node.js**

```js
const agent = await new AgentBuilder('anthropic/claude-sonnet-5-5').browser({ isolated: true }).build()
```

**Rust**

```rust
use ailoy::browser::Browser;

let agent = AgentBuilder::new("anthropic/claude-sonnet-5-5").browser(Browser::isolated()).build().await?;
```

## skill

Adds a skill: a folder with a `SKILL.md`, at a path **inside the sandbox**. Requires [`sandbox`](#sandbox). Call once per skill. See [Skills](https://ailoy.co/docs/guides/skills/).

**Python**

```python
.skill(dir: str) -> AgentBuilder
```

**Node.js**

```js
.skill(dir: string): AgentBuilder
```

**Rust**

```rust
.skill(dir: impl Into<String>) -> AgentBuilder
```

**Example**

**Python**

```python
agent = await AgentBuilder("anthropic/claude-haiku-5-5").sandbox(sandbox).skill("/skills/laya").build()
```

**Node.js**

```js
const agent = await new AgentBuilder('anthropic/claude-haiku-5-5').sandbox(sandbox).skill('/skills/laya').build()
```

**Rust**

```rust
let agent = AgentBuilder::new("anthropic/claude-haiku-5-5").sandbox(sandbox).skill("/skills/laya").build().await?;
```

## memory

Keeps short notes about the user in `MEMORY.md` in a folder **on this machine**, adds them to the system message, and gives the agent the `memory` tool to change them. See [Memory](https://ailoy.co/docs/guides/memory/).

**Python**

```python
.memory(dir: str | Path, *, limit: int | None = None) -> AgentBuilder
```

**Node.js**

```js
.memory(dir: string, options?: { limit?: number }): AgentBuilder
```

**Rust**

```rust
use ailoy::agent::Memory;

.memory(memory: impl Into<Memory>) -> AgentBuilder
// Memory::new("./memory/user1").with_limit(2000)
```

| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
| `dir` | path | | The folder that holds `MEMORY.md`. Created when the agent is built. |
| `limit` | int | `3000` | The most characters the notes can hold. |

**Example**

**Python**

```python
agent = await AgentBuilder("anthropic/claude-haiku-5-5").memory("./memory/user1", limit=2000).build()
```

**Node.js**

```js
const agent = await new AgentBuilder('anthropic/claude-haiku-5-5').memory('./memory/user1', { limit: 2000 }).build()
```

**Rust**

```rust
use ailoy::agent::Memory;

let agent = AgentBuilder::new("anthropic/claude-haiku-5-5")
    .memory(Memory::new("./memory/user1").with_limit(2000))
    .build()
    .await?;
```

## context_manager

Limits how much old tool output and how many old images the history keeps. Node.js: `contextManager`. See [Manage context](https://ailoy.co/docs/guides/context-manager/).

**Python**

```python
.context_manager(
    max_input_tokens: int | None = None,
    preserve_recent_turns: int | None = None,
    max_images: int | None = None,
    preserve_recent_images: int | None = None,
) -> AgentBuilder
```

**Node.js**

```js
.contextManager(options?: {
  maxInputTokens?: number,
  preserveRecentTurns?: number,
  maxImages?: number,
  preserveRecentImages?: number,
}): AgentBuilder
```

**Rust**

```rust
use ailoy::agent::ContextManager;

.context_manager(spec: ContextManager) -> AgentBuilder
// ContextManager { max_input_tokens: 50_000, ..Default::default() }
```

| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
| `max_input_tokens` | int | `0` (off) | Input size in tokens above which old tool results are replaced. |
| `preserve_recent_turns` | int | `3` | How many recent turns are never changed. |
| `max_images` | int | `30` | How many tool-result images the history can hold. `0` turns the cap off. |
| `preserve_recent_images` | int | `10` | How many tool-result images are kept once the cap is reached. |

Without this call, the agent uses these defaults.

**Example**

**Python**

```python
agent = await (
    AgentBuilder("anthropic/claude-haiku-5-5")
    .context_manager(max_input_tokens=50_000, preserve_recent_turns=2)
    .build()
)
```

**Node.js**

```js
const agent = await new AgentBuilder('anthropic/claude-haiku-5-5')
  .contextManager({ maxInputTokens: 50000, preserveRecentTurns: 2 })
  .build()
```

**Rust**

```rust
use ailoy::agent::ContextManager;

let agent = AgentBuilder::new("anthropic/claude-haiku-5-5")
    .context_manager(ContextManager { max_input_tokens: 50_000, preserve_recent_turns: 2, ..Default::default() })
    .build()
    .await?;
```

## thinking_effort

Sets how much the model reasons before answering. Node.js: `thinkingEffort`.

**Python**

```python
.thinking_effort(effort: Literal["low", "medium", "high"]) -> AgentBuilder
```

**Node.js**

```js
.thinkingEffort(effort: 'low' | 'medium' | 'high'): AgentBuilder
```

**Rust**

```rust
use ailoy::model::ThinkingEffort;

.thinking_effort(effort: ThinkingEffort) -> AgentBuilder   // ThinkingEffort::Low | Medium | High
```

If not set, the model uses its own default.

## output_schema

Sets a JSON schema that the model's answers must match. Node.js: `outputSchema`.

**Python**

```python
.output_schema(schema: dict) -> AgentBuilder
```

**Node.js**

```js
.outputSchema(schema: object): AgentBuilder
```

**Rust**

```rust
.output_schema(schema: Value) -> AgentBuilder   // ailoy::to_value!({...})
```

**Example**

**Python**

```python
agent = await (
    AgentBuilder("anthropic/claude-haiku-5-5")
    .thinking_effort("high")
    .output_schema({"type": "object", "properties": {"answer": {"type": "string"}}, "required": ["answer"]})
    .build()
)
```

**Node.js**

```js
const agent = await new AgentBuilder('anthropic/claude-haiku-5-5')
  .thinkingEffort('high')
  .outputSchema({ type: 'object', properties: { answer: { type: 'string' } }, required: ['answer'] })
  .build()
```

**Rust**

```rust
use ailoy::{model::ThinkingEffort, to_value};

let agent = AgentBuilder::new("anthropic/claude-haiku-5-5")
    .thinking_effort(ThinkingEffort::High)
    .output_schema(to_value!({"type": "object", "properties": {"answer": {"type": "string"}}, "required": ["answer"]}))
    .build()
    .await?;
```

## history

Starts the agent with an earlier conversation. If the first message is a system message, the agent keeps it as it is and doesn't add skills or memory notes. See [Continue a conversation](https://ailoy.co/docs/guides/running/#continue-a-conversation).

**Python**

```python
.history(history: list[Message]) -> AgentBuilder
```

**Node.js**

```js
.history(history: Array<Message>): AgentBuilder
```

**Rust**

```rust
.history(history: impl IntoIterator<Item = Message>) -> AgentBuilder
```

**Example**

**Python**

```python
agent = await AgentBuilder("anthropic/claude-haiku-5-5").history(saved_messages).build()
```

**Node.js**

```js
const agent = await new AgentBuilder('anthropic/claude-haiku-5-5').history(savedMessages).build()
```

**Rust**

```rust
let agent = AgentBuilder::new("anthropic/claude-haiku-5-5").history(saved_messages).build().await?;
```

## build

Builds the agent. This is when the model is created, API keys are read from the environment, and skills are read from the sandbox.

**Python**

```python
async def build() -> Agent
```

**Node.js**

```js
build(): Promise<Agent>
```

**Rust**

```rust
async fn build(self) -> anyhow::Result<Agent>
```

**Returns** the [`Agent`](https://ailoy.co/docs/sdk/agent/). In Python and Node.js, the builder is emptied.

**Raises** if the org has no backend, a required API key is missing, a skill's `SKILL.md` is missing or invalid, or the browser options conflict. Calling `build()` a second time on the same builder raises `ValueError` in Python and an `INVALID_ARG` error in Node.js. See [Errors](https://ailoy.co/docs/sdk/errors/).
