Quick start
Install
pip install ailoy-pynpm install @brekkylab/ailoycargo add ailoy virtx futures anyhow tokio --features tokio/macros,tokio/rt-multi-threadThe model runs on a CLI you are logged in to (Claude Code, for the Claude model on this page) or on an API key. Installation covers the machine the sandbox needs, API keys, and fixes for common errors.
Run an agent
An agent needs only a model. This one has no tools and no sandbox, so it answers from what the model knows:
import asyncio
from ailoy import AgentBuilder
async def main() -> None: agent = await ( # For openai, use "openai/gpt-6-luna" AgentBuilder("anthropic/claude-haiku-5-5") .build() )
async for output in agent.run("What is the meaning of hello world?"): for part in output["message"]["contents"]: if part["type"] == "text": print(part["text"])
asyncio.run(main())const { AgentBuilder } = require('@brekkylab/ailoy')
async function main() { // For openai, use "openai/gpt-6-luna" const agent = await new AgentBuilder('anthropic/claude-haiku-5-5') .build()
for await (const { message } of agent.run('What is the meaning of hello world?')) { for (const part of message.contents) { if (part.type === 'text') console.log(part.text) } }
await agent.close()}
main()use ailoy::{ agent::AgentBuilder, message::{Message, Part, Role},};use futures::StreamExt as _;
#[tokio::main]async fn main() -> anyhow::Result<()> { // For openai, use "openai/gpt-6-luna" let mut agent = AgentBuilder::new("anthropic/claude-haiku-5-5") .build() .await?;
let query = Message::new(Role::User).with_contents([Part::text("What is the meaning of hello world?")]); let mut stream = agent.run(query); while let Some(output) = stream.next().await { let message = output?.message; if message.role == Role::Assistant { for text in message.contents.iter().filter_map(Part::as_text) { println!("{text}"); } } }
Ok(())}agent.run yields each message of the turn as it completes; Running an agent covers what it yields and how to stream it instead.
Give it a sandbox
The same agent with a sandbox can install software, write and run code, and keep files, in a Linux microVM of its own rather than on your machine. The added lines start virtx’s server, make a sandbox from a Python image, hand it to the agent, and copy the chart the agent draws out to population.png:
import asynciofrom pathlib import Path
from ailoy import AgentBuilderfrom virtx import Client
async def main() -> None: # Starts virtx's server, fetched into its cache the first time. client = Client.spawn() await client.pull_image("python:3.12-slim-trixie").complete() sandbox = await client.create_sandbox("python:3.12-slim-trixie")
agent = await ( # For openai, use "openai/gpt-6-luna" AgentBuilder("anthropic/claude-haiku-5-5") .instruction("Write what you are asked for into /artifacts.") .sandbox(sandbox) .build() )
async for output in agent.run("Create a bar chart comparing the populations of European countries and save it to /artifacts/population.png."): for part in output["message"]["contents"]: if part["type"] == "text": print(part["text"])
Path("population.png").write_bytes(await sandbox.read_file("artifacts/population.png"))
asyncio.run(main())const { writeFileSync } = require('node:fs')const { AgentBuilder } = require('@brekkylab/ailoy')const { Client } = require('@brekkylab/virtx')
async function main() { // Starts virtx's server, fetched into its cache the first time. const client = Client.spawn() await client.pullImage('python:3.12-slim-trixie').complete() const sandbox = await client.createSandbox('python:3.12-slim-trixie')
// For openai, use "openai/gpt-6-luna" const agent = await new AgentBuilder('anthropic/claude-haiku-5-5') .instruction('Write what you are asked for into /artifacts.') .sandbox(sandbox) .build()
for await (const { message } of agent.run('Create a bar chart comparing the populations of European countries and save it to /artifacts/population.png.')) { for (const part of message.contents) { if (part.type === 'text') console.log(part.text) } }
writeFileSync('population.png', await sandbox.readFile('artifacts/population.png')) await agent.close() await client.close()}
main()use ailoy::{ agent::AgentBuilder, message::{Message, Part, Role},};use futures::StreamExt as _;use virtx::{Client, Files as _, SandboxCreateArgs};
#[tokio::main]async fn main() -> anyhow::Result<()> { // Starts virtx's server, fetched into its cache the first time. let client = Client::spawn_virtx([] as [&str; 0]).await?; client.pull_image("python:3.12-slim-trixie", None).complete(false).await?; let sandbox = client .create_sandbox(&SandboxCreateArgs::new("python:3.12-slim-trixie")) .await?;
// For openai, use "openai/gpt-6-luna" let mut agent = AgentBuilder::new("anthropic/claude-haiku-5-5") .instruction("Write what you are asked for into /artifacts.") .sandbox(sandbox.clone()) .build() .await?;
let query = Message::new(Role::User).with_contents([Part::text("Create a bar chart comparing the populations of European countries and save it to /artifacts/population.png.")]); let mut stream = agent.run(query); while let Some(output) = stream.next().await { let message = output?.message; if message.role == Role::Assistant { for text in message.contents.iter().filter_map(Part::as_text) { println!("{text}"); } } }
let png = sandbox.read_file("artifacts/population.png", 0, None).await?; std::fs::write("population.png", png)?; Ok(())}The image has no matplotlib, so the agent installs it with the shell that sandbox(...) gives it, along with file tools (Tools).
What happened
- How an agent works walks through the turn: the model asks for tools, they run, and their results go back to it until it answers.
- Giving an agent a sandbox covers images, volumes for inputs and results, and picking up later.
- Defining an agent lists everything else an agent can be given.
- If a step fails, Troubleshooting lists the usual errors and their fixes.