Your first agent
In Level 0 the macro built and ran the agent in one go. That is fine for a script. In an application you usually want something else: check the agent once when the program starts, then run it as often as requests come in. This lesson shows that shape with the Agent builder, and teaches you to read what every run did.
Build once
course_first_agent builds its agent in its own function:
#![allow(unused)]
fn main() {
async fn explainer() -> Result<BuiltAgent, KitError> {
Agent::new("explainer")
.model(MODEL)
.instructions("Explain the user's topic to a new programmer in three short sentences.")
.max_turns(2)
.limits(Limits {
run: Some(20_000),
..Limits::default()
})
.build()
.await
}
}
Read it top to bottom. Agent::new takes the name. .model takes a vendor/model selector from the built-in catalog; here MODEL is "anthropic/claude-haiku-5-5". .instructions is the job. .max_turns and .limits keep the loop short and cheap.
None of those setters can fail. Every check waits for build(): the name, the model, the key, the tools, the storage. If something is wrong, build returns the first problem, and it does so before any model call, so a typo never costs you money. What comes back is a BuiltAgent. You can keep it, share it between tasks, and run it as often as you like without checking again.
Run it, and look at the run
#![allow(unused)]
fn main() {
let agent = explainer().await?;
for topic in ["What is a checkpoint?", "What is a tool call?"] {
let outcome = agent.run_with(topic).await?;
let usage = outcome.usage();
println!("{}", outcome.text());
}
run would give us just the answer as a String. run_with gives us a RunOutcome, and that is where the interesting numbers are. text() is the answer. usage() holds the tokens in, the tokens out and the cost. model_calls() says how many times the loop asked the model, and run_id() names the run, which matters later when we resume runs.
The example prints all of them:
#![allow(unused)]
fn main() {
println!(
"run {}: {} model calls, {} tokens in, {} tokens out, {} millionths of a dollar\n",
outcome.run_id(),
outcome.model_calls(),
usage.input_tokens,
usage.output_tokens,
usage.cost_usd_micros,
);
}
Costs in troupekit are whole numbers of millionths of a US dollar. Integers do not drift the way floats do when you add thousands of them, which is exactly what a budget does.
Try it
cargo run -p troupekit --example course_first_agent
You get two short explanations, each followed by a line of usage. Look at the model calls: with no tools, each run needs one call, because the model can answer straight away. In the next lesson we add tools, and that number goes up for a reason you can see.
If you change the model to a selector that is not in the catalog, the program stops at build with an error that names the agent and the bad selector. Try it; it costs nothing.
Next: Tools.