Tracing
Sooner or later an agent gives an answer you did not expect, and the answer alone does not tell you why. Did it call the tool? With which arguments? How many times did it ask the model? You want a record of the run, not a guess.
That record is a trace: one line per span, for every model call, tool call, search and graph step, written to a JSONL file.
One setting
course_tracing gives an agent a stock tool and turns tracing on:
#![allow(unused)]
fn main() {
let agent = Agent::new("stock")
.model(MODEL)
.instructions("Answer stock questions with the stock_level tool, in one sentence.")
.tool(stock_level)
.max_turns(4)
.limits(Limits {
run: Some(20_000),
..Limits::default()
})
.tracing(true)
.trace_file(&path)
.build()
.await?;
}
.tracing(true) records every category. Without .trace_file(..), spans go to ./traces/<name>-<trace id>.jsonl; here we pick the file, in the temp folder, so the example leaves nothing in your working directory.
By default a span records sizes, not text. The trace knows how many bytes a prompt had, not what it said, so turning tracing on does not write your users' messages to disk. Add .trace_content(true) when you need the text itself while debugging. The model's API key is scrubbed either way when .api_key(..) or the vendor's environment variable supplies it. A key that only a custom .model_client(..) holds is not scrubbed, and neither is any other secret in the text: with .trace_content(true), a password a user types into a message is written to the trace as is. Treat a content trace as sensitive data.
Reading the file
The example asks about two products, so the model should call stock_level twice. Then it reads the trace back and counts the spans by name:
#![allow(unused)]
fn main() {
fn span_counts(text: &str) -> BTreeMap<String, usize> {
let mut counts = BTreeMap::new();
for line in text.lines() {
let Ok(record) = serde_json::from_str::<serde_json::Value>(line) else {
continue;
};
if record["t"] == "start" {
if let Some(name) = record["name"].as_str() {
*counts.entry(name.to_string()).or_insert(0) += 1;
}
}
}
counts
}
}
Each line of the file is one JSON record. A record with "t": "start" opens a span, and its name says what kind: model.call, tool.call, budget.check and so on. Counting them is a quick way to see the shape of a run.
Try it
cargo run -p troupekit --example course_tracing
After the answer you get the trace path and a list of span names with counts. Look for tool.call: two of them means the model asked for both products. Look for model.call and budget.check next to each other. Because this agent has a spend limit, every model call is preceded by a budget check: the check from the beginner lessons, now visible in the record.
For more than counts, the command line tool draws the whole span tree, or writes it as one HTML page:
cargo install troupekit-cli
troupekit trace /path/printed/by/the/example.jsonl
troupekit trace /path/printed/by/the/example.jsonl --html
When to trace
Tracing is off by default, and it costs a little time for every span it writes. Turn it on while you build and debug, and in production for the agents you need to explain later. .trace(..) takes a list of categories when the full trace is more than you need; Tracing and budgets lists all eleven.
That is the end of the intermediate level. You can now build an agent that uses tools, remembers people, searches your documents, runs inside a graph and leaves a record. The advanced lessons are about what happens when things go wrong: a process that dies mid-run, and spend that has to be capped across many runs.
Next: Runs that survive a kill.