# Agent

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

Reference for Agent and AgentRun: run turns, stream replies, read the history and close the agent.

An `Agent` runs turns: it sends your query to the model, runs the tools the model asks for, and returns every message. Create one with [`AgentBuilder`](https://ailoy.co/docs/sdk/agent-builder/), or from a saved spec with [`from_spec`](#from_spec).

When a call fails, Python raises `ailoy.AiloyError`, Node.js throws an `Error` with `code` set to `"AILOY_ERROR"`, and Rust returns an `Err`. Each entry's **Raises** line says when. See [Errors](https://ailoy.co/docs/sdk/errors/).

| Member | Does |
| --- | --- |
| [`run`](#run) | Runs one turn and returns each complete message. |
| [`run_stream`](#run_stream) | Runs one turn and returns the reply in pieces. |
| [`history`](#history) | The conversation so far. |
| [`start_browser`](#start_browser) | Connects the browser now. |
| [`close`](#close) | Releases the model, browser and sandbox connection. |
| [`from_spec`](#from_spec) | Creates an agent from a saved spec. |
| [`AgentRun`](#agentrun) | The turn that `run` and `run_stream` return. |

## run

Runs one turn and returns each message as it completes: the model's tool calls, each tool result and the final answer. Nothing runs until you iterate the result.

**Python**

```python
agent.run(query: str | Message) -> AgentRun[MessageOutput]

async for output in agent.run("What is 2 + 2?"):
    print(output["message"])
```

**Node.js**

```js
agent.run(query: string | Message): AsyncIterableIterator<MessageOutput>

for await (const output of agent.run('What is 2 + 2?')) {
  console.log(output.message)
}
```

**Rust**

```rust
fn run(&mut self, query: Message) -> BoxStream<'_, anyhow::Result<MessageOutput>>

let mut stream = agent.run(Message::new(Role::User).with_contents([Part::text("What is 2 + 2?")]));
while let Some(output) = stream.next().await {
    println!("{:?}", output?.message);
}
```

| Parameter | Type | Description |
| --- | --- | --- |
| `query` | string or [`Message`](https://ailoy.co/docs/sdk/types/#message) | A string becomes a user message with one text part. Rust takes a `Message` only. |

**Returns** an async iterator of [`MessageOutput`](https://ailoy.co/docs/sdk/types/#messageoutput).

**Raises** while iterating, if the model fails before its first message (your query is removed from the history so you can retry), or if the model's reply is cut off at its output limit (the cut reply is returned first). A failing tool doesn't end the turn. See [Handle errors](https://ailoy.co/docs/guides/running/#handle-errors).

One agent runs one turn at a time. A second `run` or `run_stream` on the same agent waits until the first turn ends. To run turns in parallel, build one agent for each. In Rust, the compiler enforces this, since `run` and `run_stream` borrow the agent mutably.

## run_stream

Runs one turn like `run`, but returns the model's output in pieces as it's written. Each tool result arrives as one piece. Node.js: `runStream`.

**Python**

```python
agent.run_stream(query: str | Message) -> AgentRun[MessageDeltaOutput]
```

**Node.js**

```js
agent.runStream(query: string | Message): AsyncIterableIterator<MessageDeltaOutput>
```

**Rust**

```rust
fn run_stream(&mut self, query: Message) -> BoxStream<'_, anyhow::Result<MessageDeltaOutput>>
```

**Returns** an async iterator of [`MessageDeltaOutput`](https://ailoy.co/docs/sdk/types/#messagedeltaoutput). A non-null `finish_reason` marks the end of a message. See [Stream the reply](https://ailoy.co/docs/guides/running/#stream-the-reply).

**Raises** while iterating, under the same conditions as `run`.

**Example**

**Python**

```python
async for output in agent.run_stream("Write a haiku about rain."):
    for part in output["delta"]["contents"]:
        if part["type"] == "text":
            print(part["text"], end="", flush=True)
```

**Node.js**

```js
for await (const { delta } of agent.runStream('Write a haiku about rain.')) {
  for (const part of delta.contents) {
    if (part.type === 'text') process.stdout.write(part.text)
  }
}
```

**Rust**

```rust
let mut stream = agent.run_stream(Message::new(Role::User).with_contents([Part::text("Write a haiku about rain.")]));
while let Some(output) = stream.next().await {
    let output = output?;
    // output.delta.contents holds the new pieces
}
```

## history

Every message of the conversation so far: the system message, your queries, the model's messages and tool results.

**Python**

```python
agent.history -> list[Message]   # property
```

**Node.js**

```js
agent.history: Array<Message>   // getter
```

**Rust**

```rust
fn history(&self) -> &[Message]
```

The agent's whole state is the public field `agent.state`, an `AgentState` with `history`, `sandbox` and `last_input_tokens`. `context_manager()` and `set_context_manager(Option<ContextManager>)` read and change the context manager between turns.

**Returns** the messages, oldest first.

**Raises** in Python and Node.js if a turn is running: `this agent is in the middle of a turn`. Read it between turns.

**Example**

**Python**

```python
for message in agent.history:
    print(message["role"], message["contents"])
```

**Node.js**

```js
for (const message of agent.history) {
  console.log(message.role, message.contents)
}
```

**Rust**

```rust
for message in agent.history() {
    println!("{:?}: {:?}", message.role, message.contents);
}
```

## start_browser

Connects to the browser now, instead of on the agent's first browser tool call. With your own browser, this is when the browser asks for permission, so call it while the user is at their screen. Does nothing if already connected. Node.js: `startBrowser`.

**Python**

```python
async def start_browser() -> None
```

**Node.js**

```js
startBrowser(): Promise<void>
```

**Rust**

```rust
async fn start_browser(&mut self) -> anyhow::Result<()>
```

**Raises** an error if the user declines, if remote debugging is off, or if called during a turn.

## close

Releases the model, any browser the agent started, and its connection to the sandbox. Waits for a running turn to finish. Leaves the sandbox itself running. Calling it twice is the same as calling it once. Python and Node.js only; a Rust agent releases everything when it's dropped.

**Python**

```python
async def close() -> None

# Or close automatically at the end of a block:
async with agent:
    ...
```

**Node.js**

```js
close(): Promise<void>
```

After `close`, any other call on the agent raises `this agent has been closed`.

**Example**

**Python**

```python
agent = await AgentBuilder("anthropic/claude-haiku-5-5").build()
try:
    async for output in agent.run("Hello"):
        ...
finally:
    await agent.close()
```

**Node.js**

```js
const agent = await new AgentBuilder('anthropic/claude-haiku-5-5').build()
try {
  for await (const output of agent.run('Hello')) {
    // ...
  }
} finally {
  await agent.close()
}
```

## from_spec

Builds an agent from a saved [`AgentSpec`](https://ailoy.co/docs/sdk/types/#agentspec), with an optional history and sandbox. Node.js: `Agent.fromSpec`. Rust: `Agent::try_new` or `Agent::try_with_state`.

**Python**

```python
@staticmethod
async def from_spec(
    spec: AgentSpec,
    *,
    history: list[Message] | None = None,
    sandbox: virtx.Sandbox | None = None,
) -> Agent
```

**Node.js**

```js
Agent.fromSpec(spec: AgentSpec, options?: { history?: Array<Message>; sandbox?: Sandbox }): Promise<Agent>
```

**Rust**

```rust
use ailoy::agent::{Agent, AgentSpec, AgentState};

async fn try_new(spec: AgentSpec) -> anyhow::Result<Agent>
async fn try_with_state(spec: AgentSpec, state: AgentState) -> anyhow::Result<Agent>

// AgentState::new().with_history(messages).with_sandbox(sandbox)
```

A sandbox passed here doesn't add the shell and file tools: the spec must list them. In Rust, call `.sandbox_tools()` on the `AgentSpec`. See [Build an agent from a spec](https://ailoy.co/docs/guides/agents/#build-an-agent-from-a-spec).

**Returns** the agent.

**Raises** under the same conditions as [`AgentBuilder.build`](https://ailoy.co/docs/sdk/agent-builder/#build).

**Example**

**Python**

```python
import json
from ailoy import Agent

with open("spec.json") as f:
    spec = json.load(f)

agent = await Agent.from_spec(spec, history=saved_history)
```

**Node.js**

```js
const { Agent } = require('@brekkylab/ailoy')
const fs = require('node:fs')

const spec = JSON.parse(fs.readFileSync('spec.json', 'utf8'))
const agent = await Agent.fromSpec(spec, { history: savedHistory })
```

**Rust**

```rust
use ailoy::agent::{Agent, AgentSpec, AgentState};

// AgentSpec implements serde's Deserialize; this reads it with the serde_json crate.
let spec: AgentSpec = serde_json::from_str(&std::fs::read_to_string("spec.json")?)?;
let agent = Agent::try_with_state(spec, AgentState::new().with_history(saved_history)).await?;
```

## AgentRun

What `run` and `run_stream` return in Python and Node.js: one turn, iterated with `async for` or `for await`.

| Method | Description |
| --- | --- |
| `__anext__()` / `next()` | Moves the turn forward and returns the next output. |
| `aclose()` (Python) / `return()` (Node.js) | Stops the turn early. Breaking out of the loop does the same. |

In Rust, `run` and `run_stream` return a `Stream`. Drop it to stop the turn.

### What stopping a turn keeps

When a turn stops early, by `aclose()`, `return()`, breaking out of the loop or dropping the Rust stream:

- Messages that completed stay in the history.
- A message the model was still writing isn't kept.
- A tool call left without a result gets the error `the call was interrupted before it returned` as its result when the next turn starts, so the conversation stays valid for the model.

If the model fails before its first message of a turn, the query is taken back out of the history, so you can run it again as it is.
