Language setup
To install Ailoy and run a first agent, follow Installation and the Quick start. This page covers what differs by language once you use Ailoy in a project of your own. Pick your language in any tab; every tab on these pages switches with it.
Packages
All three packages are built from one Rust crate and share its version number.
| Language | Package | Install | Requires | Source |
|---|---|---|---|---|
| Python | ailoy-py on PyPI |
pip install ailoy-py |
Python 3.11 or later | bindings/python |
| Node.js | @brekkylab/ailoy on npm |
npm install @brekkylab/ailoy |
Node.js 22 or later | bindings/node |
| Rust | ailoy on crates.io |
cargo add ailoy |
Rust 1.95 or later | src |
To run a sandbox, the machine also needs a hypervisor. See Check the requirements.
Create a project
Make a virtual environment, so Ailoy’s packages stay out of your system Python:
mkdir my-agent && cd my-agentpython -m venv .venvsource .venv/bin/activate # on Windows: .venv\Scripts\activatepip install ailoy-pyThe package is installed as ailoy-py and imported as ailoy. It also installs virtx, the package you create sandboxes with.
mkdir my-agent && cd my-agentnpm init -ynpm install @brekkylab/ailoyThis also installs @brekkylab/virtx, the package you create sandboxes with. With pnpm, add @brekkylab/virtx yourself.
cargo new my-agent && cd my-agentcargo add ailoy virtx futures anyhow tokio --features tokio/macros,tokio/rt-multi-thread| Crate | Why |
|---|---|
ailoy |
The SDK. |
virtx |
Creates sandboxes. Cargo only lets your code use crates you list, so add it next to ailoy. |
tokio |
The async runtime Ailoy runs on. |
futures |
StreamExt, to read a turn’s stream. |
anyhow |
Ailoy returns anyhow::Error. |
Then copy the first agent from the Quick start to check that everything works.
Credentials
Ailoy reads model credentials from environment variables, in every language. It reads them when build() builds the model, so set them before that.
- Claude, GPT and Gemini run through their CLIs by default (Claude Code, Codex and Gemini CLI), on the CLI’s own login. They need no key.
- An API key goes in the provider’s variable, such as
ANTHROPIC_API_KEY. For Claude, GPT and Gemini, also switch the org toapiwithset_lang_model_provider.
export ANTHROPIC_API_KEY=...Keep keys out of your code. Configuration lists every variable, and Model providers covers Bedrock, OpenRouter and your own server.
Async code
Every call that waits on a model or a sandbox is async.
Call Ailoy from an async function and start it with asyncio.run. agent.run returns an async iterator, so read it with async for.
import asyncio
from ailoy import AgentBuilder
async def main() -> None: async with await AgentBuilder("anthropic/claude-haiku-5-5").build() as agent: async for output in agent.run("Hello"): print(output["message"]["contents"])
asyncio.run(main())Jupyter already runs an event loop, so asyncio.run raises RuntimeError: asyncio.run() cannot be called from a running event loop. In a notebook, await directly in a cell instead:
from ailoy import AgentBuilder
agent = await AgentBuilder("anthropic/claude-haiku-5-5").build()async for output in agent.run("Hello"): print(output["message"]["contents"])Methods that wait return a promise. agent.run returns an async iterator, so read it with for await.
const { AgentBuilder } = require('@brekkylab/ailoy')
async function main() { const agent = await new AgentBuilder('anthropic/claude-haiku-5-5').build() try { for await (const { message } of agent.run('Hello')) { console.log(message.contents) } } finally { await agent.close() }}
main()A CommonJS file can’t use await at the top level, so put it in an async function, as above. An ES module can.
Ailoy’s async functions run on tokio, which #[tokio::main] starts. agent.run returns a Stream; read it with StreamExt::next.
use ailoy::{ agent::AgentBuilder, message::{Message, Part, Role},};use futures::StreamExt as _;
#[tokio::main]async fn main() -> anyhow::Result<()> { let mut agent = AgentBuilder::new("anthropic/claude-haiku-5-5").build().await?; let mut stream = agent.run(Message::new(Role::User).with_contents([Part::text("Hello")])); while let Some(output) = stream.next().await { println!("{:?}", output?.message.contents); } Ok(())}Imports
from ailoy import AgentBuilder, register_tool, set_lang_model_providerfrom ailoy.types import Message, MessageOutput, ToolDesc@brekkylab/ailoy is a CommonJS package. With require, take the names directly:
const { AgentBuilder } = require('@brekkylab/ailoy')In an ES module (a .mjs file, or "type": "module" in package.json), import the package as a whole and take the names from it. A named import such as import { AgentBuilder } from '@brekkylab/ailoy' fails there with SyntaxError: Named export 'AgentBuilder' not found.
import ailoy from '@brekkylab/ailoy'
const { AgentBuilder } = ailoyIn TypeScript, the package includes its type definitions, so there’s no @types package to install. Import types with import type. When TypeScript compiles to CommonJS, named imports of classes work too.
import ailoy from '@brekkylab/ailoy'import type { Message } from '@brekkylab/ailoy'
const { AgentBuilder } = ailoyEach part of the API is a module of the crate:
use ailoy::{ agent::AgentBuilder, message::{Message, Part, Role}, model::{Backend, set_lang_model_provider}, tool::{ToolDescBuilder, set_tool_provider}, datatype::Value, to_value, tool_func,};Messages and types
Messages, outputs and tool descriptions are plain dicts, typed as TypedDicts in ailoy.types. The package ships type hints, so mypy, Pyright and your editor check your calls.
from ailoy.types import Message
query: Message = {"role": "user", "contents": [{"type": "text", "text": "Hello"}]}Messages, outputs and tool descriptions are plain objects. Their keys are snake_case, as in the JSON they stand for (finish_reason, mime_type), while methods are camelCase (runStream).
const query: Message = { role: 'user', contents: [{ type: 'text', text: 'Hello' }] }Messages are typed structs, built with methods:
let query = Message::new(Role::User).with_contents([Part::text("Hello")]);JSON values, such as tool arguments and schemas, are ailoy::datatype::Value, written with the to_value! macro.
Types lists every field, with code for each language.
Close an agent
Call await agent.close() when you’re done, or use async with as above. It releases the model, any browser the agent started and its connection to the sandbox.
Call await agent.close() when you’re done, in a finally block as above. It releases the model, any browser the agent started and its connection to the sandbox.
An agent releases its model, browser and sandbox connection when it’s dropped. There’s no close method.
See close.
Errors
Ailoy raises ailoy.AiloyError. Errors from the sandbox are virtx.VirtxError.
Ailoy throws an Error with code set to "AILOY_ERROR". Errors from the sandbox have code set to "VIRTX_ERROR".
Ailoy’s functions return anyhow::Result. Print an error with its causes using {e:#}.
When a model’s API answers with a rate-limit or server error, Ailoy retries the request up to 3 times before the error reaches your code. The cli backend doesn’t retry. See Automatic retries. Ailoy’s log messages, such as a warning for each retry, are printed only in Rust, once your program installs a logger; see Logging. Errors has the rest.
Next steps
- AgentBuilder: every way to configure an agent.
- Configuration: the environment variables Ailoy reads.