Skip to content

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-agent
python -m venv .venv
source .venv/bin/activate # on Windows: .venv\Scripts\activate
pip install ailoy-py

The package is installed as ailoy-py and imported as ailoy. It also installs virtx, the package you create sandboxes with.

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 to api with set_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"])

Imports

from ailoy import AgentBuilder, register_tool, set_lang_model_provider
from ailoy.types import Message, MessageOutput, ToolDesc

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"}]}

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.

See close.

Errors

Ailoy raises ailoy.AiloyError. Errors from the sandbox are virtx.VirtxError.

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