Skip to main content

Tools and Agents

Tools are functions​

Add #[tool] to a function, and its signature becomes the tool's schema: the parameters, their types, and which are required (the ones without a default). The attribute says what the signature can't: a description, and each parameter's.

desk: {
#[tool({'description': 'Look up an order', 'params': {'id': 'the order number'}})]
fn order(id: int) -> map {
{'id': id, 'status': 'shipped'}
}

#[tool({'description': 'Refund an order', 'params': {'id': 'the order number', 'amount': 'dollars'}})]
async fn refund(id: int, amount: float, reason: str = 'requested') -> str {
'refunded'
}
}

Pass the object as handlers, and its tools are offered on every call: chat(request, self.desk) or agent(request, self.desk). A list of objects works too. <Ai>.tools(obj) gives the tool maps if you want to look at them, and <Ai>.tool(func, config) makes one from any function, for a request's tools.

What the package does with a call:

  • The model can call only the tools the call offered (after the before hooks), never anything else in the document.
  • Arguments are cast to the parameter types. A missing required one, a thrown error, an unknown tool, or arguments that aren't JSON become an error result the model sees, so it can try again. They never end a run.
  • The result goes back as text: a string as it is, anything else as JSON, cut at the Ai's max_result characters.
  • A response's tool calls run at the same time, unless the request's parallel is false.

Running tool calls yourself​

After chat, run_tools runs a response's tool calls and gives the tool message to add:

const response = await self.ai.chat(request, self.desk);
if (response.tool_calls.len() > 0) {
request.messages.push_back(response.message);
request.messages.push_back(await self.ai.run_tools(response));
const next = await self.ai.chat(request, self.desk);
}

Agents​

agent does that loop for you: it calls the model, runs the tools it asks for, sends the results back, and repeats until the model is done or a limit is reached.

using request = new {
model: 'anthropic/claude-sonnet-5-5',
system: 'You handle refunds. Ask before refunding over $100.',
messages: [new { role: 'user', content: 'Refund order 1042' }],
limits: {'turns': 8, 'tool_calls': 20, 'time': 2min},
};
const run = await self.ai.agent(request, self.desk);
pln(run.stop); // 'end' when the model is done
pln(run.text);

The run has the whole conversation (messages), the usage over every call, and how many turns and tool_calls it made. stop says why it stopped:

StopWhy
'end'the model is done
'max_tokens', 'filter'the last answer was cut
'turns', 'tool_calls', 'time'a limit was reached (the Ai's turns and tool_calls, 10 and 50, by default)
'denied'a hook refused a call (run.error.reason says why)
'cancelled'run.cancel was set (Ex. by a hook, or a UI's stop button through one)
'error'the provider or the network failed (run.error)

A run that stopped still has everything so far, with a result for every tool call. To go on, call agent again with messages: run.messages.

Skills​

A skill is instructions for a kind of task, with the files it points to, as in Claude's skills. Give a request skills: the model sees each one's name and description, and reads the ones a task needs with a read_skill tool, so a skill costs tokens only when it's used.

using request = new {
model: 'anthropic/claude-sonnet-5-5',
messages: [new { role: 'user', content: 'Write a Stof config for a rate limiter.' }],
skills: [<Ai>.skill(fs::read_string('skills/stof/SKILL.md'), {
'references/libs/Std.md': fs::read_string('skills/stof/references/libs/Std.md'),
})],
};
const run = await self.ai.agent(request);

<Ai>.skill(text, files) reads the name and description from a SKILL.md's front matter. A skill can also be a map (name, description, content, files), Ex. from a host, and one with inline: true goes in the system prompt whole (a short house style every answer needs).

Tools from MCP servers​

An McpServer's tools go in a request's tools and work like #[tool] functions, hooks included. See MCP servers.