Skip to content

AgentBuilder

AgentBuilder creates an Agent. Create a builder with a model name, call any of the methods below, then call build(). Every method except build() returns the builder, so calls can be chained.

from ailoy import AgentBuilder
agent = await (
AgentBuilder("anthropic/claude-haiku-5-5")
.instruction("Answer in one sentence.")
.web_search_tool()
.build()
)

In Python and Node.js, the builder is changed in place, and build() empties it. Create a new builder for each agent.

Method Does
Constructor Starts a builder for a model.
instruction Sets the system prompt.
tool, tools Add tools by their description.
web_search_tool Adds web_search.
web_fetch_tool Adds web_fetch.
sandbox Gives the agent a sandbox and the shell and file tools.
browser Gives the agent a browser and the browser_* tools.
skill Adds a skill from a folder in the sandbox.
memory Lets the agent keep notes on the user.
context_manager Keeps the history under the model’s limits.
thinking_effort Sets how much the model reasons.
output_schema Makes answers match a JSON schema.
history Starts from a saved conversation.
build Creates the agent.

Constructor

Creates a builder for the given model.

AgentBuilder(model: str)
Parameter Type Description
model string The model as <org>/<name>, such as "anthropic/claude-haiku-5-5". It runs on the backend set for its org. See Model providers.

instruction

Sets the system prompt. The agent adds its skills and memory notes to it when it builds the system message.

.instruction(instruction: str) -> AgentBuilder

tool

Adds one tool by its description. The tool’s function must be registered under the same name, with register_tool or set_tool_provider.

.tool(desc: ToolDesc) -> AgentBuilder
Parameter Type Description
desc ToolDesc The tool’s name, description and argument schema.

tools

Adds several tools at once, such as the list an MCP registration returns.

.tools(descs: list[ToolDesc]) -> AgentBuilder

web_search_tool

Adds the web_search tool, which runs in your program. Node.js: webSearchTool.

.web_search_tool(engines: list[WebSearchEngine] | None = None) -> AgentBuilder
Parameter Type Default Description
engines list of WebSearchEngine All engines The engines to query. An empty list also means all of them.

web_fetch_tool

Adds the web_fetch tool, which fetches a URL from your program. Node.js: webFetchTool.

.web_fetch_tool() -> AgentBuilder

sandbox

Gives the agent a virtx sandbox and adds the shell and file tools that run in it. The sandbox stays yours: the agent never starts, stops or removes it. See Give an agent a sandbox.

.sandbox(sandbox: virtx.Sandbox) -> AgentBuilder

Example

from virtx import Client
client = Client.spawn()
sandbox = await client.create_sandbox("python:3.12-slim-trixie")
agent = await AgentBuilder("anthropic/claude-haiku-5-5").sandbox(sandbox).build()

browser

Gives the agent browser_* tools that control a Chromium-based browser over CDP. See Use a browser.

.browser(
*,
user_browser: str | Path | None = None,
careful: bool = True,
start_url: str | None = None,
isolated: bool = False,
headless: bool = False,
executable: str | Path | None = None,
profile: str | Path | None = None,
) -> AgentBuilder
Option Type Default Applies to Description
isolated bool false false uses your own browser. true starts a separate browser on first use and closes it with the agent.
user_browser string or path The browser that turned on remote debugging most recently Your own browser "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "chromium", "edge", "brave", or the user data directory of any other Chromium-based browser.
careful bool true Both Tells the agent to use sites as a person would, so bot checks are less likely to block it.
start_url string Both The page the agent’s tab opens on.
headless bool false Separate browser Runs without a window.
executable path The first of Chrome, Chromium, Edge or Brave found Separate browser The browser executable to start.
profile path A fresh profile, deleted afterwards Separate browser A user data directory to keep, so sign-ins last between runs.

Mixing options for your own browser with options for a separate browser fails at build().

Example

agent = await AgentBuilder("anthropic/claude-sonnet-5-5").browser(isolated=True).build()

skill

Adds a skill: a folder with a SKILL.md, at a path inside the sandbox. Requires sandbox. Call once per skill. See Skills.

.skill(dir: str) -> AgentBuilder

Example

agent = await AgentBuilder("anthropic/claude-haiku-5-5").sandbox(sandbox).skill("/skills/laya").build()

memory

Keeps short notes about the user in MEMORY.md in a folder on this machine, adds them to the system message, and gives the agent the memory tool to change them. See Memory.

.memory(dir: str | Path, *, limit: int | None = None) -> AgentBuilder
Parameter Type Default Description
dir path The folder that holds MEMORY.md. Created when the agent is built.
limit int 3000 The most characters the notes can hold.

Example

agent = await AgentBuilder("anthropic/claude-haiku-5-5").memory("./memory/user1", limit=2000).build()

context_manager

Limits how much old tool output and how many old images the history keeps. Node.js: contextManager. See Manage context.

.context_manager(
max_input_tokens: int | None = None,
preserve_recent_turns: int | None = None,
max_images: int | None = None,
preserve_recent_images: int | None = None,
) -> AgentBuilder
Parameter Type Default Description
max_input_tokens int 0 (off) Input size in tokens above which old tool results are replaced.
preserve_recent_turns int 3 How many recent turns are never changed.
max_images int 30 How many tool-result images the history can hold. 0 turns the cap off.
preserve_recent_images int 10 How many tool-result images are kept once the cap is reached.

Without this call, the agent uses these defaults.

Example

agent = await (
AgentBuilder("anthropic/claude-haiku-5-5")
.context_manager(max_input_tokens=50_000, preserve_recent_turns=2)
.build()
)

thinking_effort

Sets how much the model reasons before answering. Node.js: thinkingEffort.

.thinking_effort(effort: Literal["low", "medium", "high"]) -> AgentBuilder

If not set, the model uses its own default.

output_schema

Sets a JSON schema that the model’s answers must match. Node.js: outputSchema.

.output_schema(schema: dict) -> AgentBuilder

Example

agent = await (
AgentBuilder("anthropic/claude-haiku-5-5")
.thinking_effort("high")
.output_schema({"type": "object", "properties": {"answer": {"type": "string"}}, "required": ["answer"]})
.build()
)

history

Starts the agent with an earlier conversation. If the first message is a system message, the agent keeps it as it is and doesn’t add skills or memory notes. See Continue a conversation.

.history(history: list[Message]) -> AgentBuilder

Example

agent = await AgentBuilder("anthropic/claude-haiku-5-5").history(saved_messages).build()

build

Builds the agent. This is when the model is created, API keys are read from the environment, and skills are read from the sandbox.

async def build() -> Agent

Returns the Agent. In Python and Node.js, the builder is emptied.

Raises if the org has no backend, a required API key is missing, a skill’s SKILL.md is missing or invalid, or the browser options conflict. Calling build() a second time on the same builder raises ValueError in Python and an INVALID_ARG error in Node.js. See Errors.