Tools
A model on its own can only talk. It does not know where order A-1001 is, and if you ask, a model without tools may make something up. Tools fix that. A tool is a function the model may ask you to call. Troupekit runs it and hands the result back, and the model carries on with real data in front of it.
A tool is a Rust function
course_tools is a helper for a small online shop. Its first tool looks up an order:
#![allow(unused)]
fn main() {
/// Looks up the status of one order. Order numbers look like A-1001.
#[tool]
fn order_status(order: String) -> Result<String, String> {
match order.trim() {
"A-1001" => Ok("shipped on 2026-11-20, arriving in 2 to 4 business days".to_string()),
"A-1002" => Ok("packed and waiting for the courier".to_string()),
other => Err(format!(
"no order {other}; the number is on the confirmation email"
)),
}
}
}
In your app this would query a database. Here a match stands in for one, so the example runs anywhere.
#[tool] does three things with this function. The doc comment becomes the description the model reads, so write it for the model: say what the tool does and what a good argument looks like. The argument list becomes a JSON Schema, here one string called order. And the function becomes a value you can hand to an agent.
Because of that schema, your function never sees arguments of the wrong shape. If the model sends a number where order should be a string, the call is refused before your code runs.
Errors are answers too
Look at the last arm of the match. An unknown order returns Err. That does not crash the run. The model receives the error text as the tool's output and can react to it, for example by telling the customer where to find the right number. So write error messages the way you would explain the problem to a person.
The second tool takes two arguments and fails for countries the shop does not ship to:
#![allow(unused)]
fn main() {
/// Returns the shipping cost in euros for a parcel of `grams` to `country`,
/// given as a two-letter code such as DE or FR.
#[tool]
fn shipping_cost(country: String, grams: u32) -> Result<String, String> {
}
Hand the tools to the agent
#![allow(unused)]
fn main() {
/// You answer customer questions for a small online shop. Use the tools
/// for every order and shipping question. When a tool returns an error,
/// tell the customer what to check instead of guessing.
#[agent(
model = "anthropic/claude-haiku-5-5",
tools = [order_status, shipping_cost],
max_turns = 4,
limits = Limits { run: Some(30_000), ..Limits::default() },
)]
fn shop_helper() {}
}
tools = [..] lists them. The builder spelling is .tool(order_status).tool(shipping_cost). Two tools with the same name make build fail, because the model could not tell them apart. max_turns goes up to 4 here, since a tool call needs at least two turns: one where the model asks for the tool, and one where it answers with the result.
Try it
cargo run -p troupekit --example course_tools
The program asks three questions. The first should come back with the shipping date from order_status. The second should give a price worked out by shipping_cost. The third asks about order B-77, which does not exist, so the tool fails and the agent should tell the customer to check the confirmation email.
Look at the model calls after each answer. A question answered with one tool takes two calls. That is the loop from Level 0, now visible: ask for a tool, get the result, answer.
When the model asks for several tools in one turn, troupekit runs those calls at the same time.
Next: Spend limits.