Defining an agent
An agent is a model plus what you give it. AgentBuilder takes the model’s name, then one call for each thing the agent should have, and build() makes the agent.
AgentBuilder at a glance
Every call returns the builder, so they chain. Node.js spells them in camelCase (webSearchTool, contextManager).
| Call | What the agent gets | Where it is covered |
|---|---|---|
instruction(text) |
The system message’s text. | Below |
sandbox(sandbox) |
A virtx sandbox to work in, and a shell and file tools there. | Giving an agent a sandbox |
tool(desc), tools(descs) |
Tools by name: your own functions, or a registered MCP server’s. | Tools, Using MCP |
web_search_tool(engines), web_fetch_tool() |
Searching the web and reading a page, from your program. | Tools |
browser(...) |
A browser it drives with browser_* tools. |
Using a browser |
skill(dir) |
A folder of instructions in its sandbox, for one kind of task. | Skills |
memory(dir) |
Notes on the user that outlast a session, in a folder on this host. | Tools |
context_manager(...) |
Old tool results and images cut once a run’s input grows past a limit. | Context manager |
thinking_effort(effort), output_schema(schema) |
How much the model thinks, and a JSON schema its answers must match. | Model providers |
history(messages) |
An earlier conversation to continue. | Running an agent |
Instruction
instruction(...) is the system message’s text: who the agent is, what it is for, and where in its sandbox to find inputs and leave results. The agent writes the system message from this text, the list of its skills (those of skill(...) and those of its sandbox’s abins) and the notes in its memory, and puts that message at the front of its history, unless the history you give it already has one.
From a spec
The model, the instruction, the tools, the skills, the memory and the model options make up an agent’s spec: plain JSON, which you can store and build an agent from again. The rest (its history and its sandbox) is its state, given beside the spec. Agent.from_spec builds an agent from both:
from ailoy import Agent
spec = { "model": "anthropic/claude-haiku-5-5", "instruction": "Answer in one sentence.",}agent = await Agent.from_spec(spec, history=saved_messages, sandbox=sandbox)const { Agent } = require('@brekkylab/ailoy')
const spec = { model: 'anthropic/claude-haiku-5-5', instruction: 'Answer in one sentence.',}const agent = await Agent.fromSpec(spec, { history: savedMessages, sandbox })use ailoy::agent::{Agent, AgentSpec, AgentState};use serde_json::json;
let spec: AgentSpec = serde_json::from_value(json!({ "model": "anthropic/claude-haiku-5-5", "instruction": "Answer in one sentence.",}))?;let state = AgentState::new() .with_history(saved_messages) .with_sandbox(sandbox);let agent = Agent::try_with_state(spec, state).await?;A spec lists its tools itself. sandbox(...) on the builder adds the shell and file tools to the spec; a sandbox given beside a spec does not, so the spec must list them for the agent to have them. In Rust, calling .sandbox_tools() on the AgentSpec adds them. Python and Node.js have no such call: to give an agent with a sandbox a saved history there, build it with AgentBuilder, history(...) and sandbox(...).
Closing
In Python and Node.js, close() releases what the agent holds: its model, any browser it started itself, and its hold on the sandbox’s client. It waits for a turn being iterated to finish. In Python, async with agent: closes it at the end of the block. A Rust agent releases them when it is dropped.
Closing an agent leaves its sandbox as it is, for you to use again or remove.
Next
- Giving an agent a sandbox: the computer the agent works in.
- Model providers: naming a model and choosing how it runs.