Skip to content

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, or from a saved spec with 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.

Member Does
run Runs one turn and returns each complete message.
run_stream Runs one turn and returns the reply in pieces.
history The conversation so far.
start_browser Connects the browser now.
close Releases the model, browser and sandbox connection.
from_spec Creates an agent from a saved spec.
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.

agent.run(query: str | Message) -> AgentRun[MessageOutput]
async for output in agent.run("What is 2 + 2?"):
print(output["message"])
Parameter Type Description
query string or Message A string becomes a user message with one text part. Rust takes a Message only.

Returns an async iterator of 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.

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.

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

Returns an async iterator of MessageDeltaOutput. A non-null finish_reason marks the end of a message. See Stream the reply.

Raises while iterating, under the same conditions as run.

Example

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)

history

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

agent.history -> list[Message] # property

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

for message in agent.history:
print(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.

async def start_browser() -> None

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.

async def close() -> None
# Or close automatically at the end of a block:
async with agent:
...

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

Example

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

from_spec

Builds an agent from a saved AgentSpec, with an optional history and sandbox. Node.js: Agent.fromSpec. Rust: Agent::try_new or Agent::try_with_state.

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

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.

Returns the agent.

Raises under the same conditions as AgentBuilder.build.

Example

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)

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.