Observability
Freyja reports what an agent does through the tracing crate and through numbers on the Run it returns. This guide covers the events and span it emits, how to see them, and how to count tokens and turn them into cost.
Turn it on
Tracing sits behind a feature, so a build that does not need it pays nothing.
freyja = { version = "0.7", features = ["tracing"] }
Freyja installs no subscriber. Events go wherever your application's tracing subscriber sends them.
The Tracing hook
Tracing is a ready-made hook that emits one INFO event with target freyja per model call and per tool call. It never stops a run. Its source, src/agent/tracing.rs in the repository, is a complete hook to read before writing one.
let agent = Agent::new(client).tool(add).hook(freyja::Tracing::new());
A model call event, with message model call, has these fields:
| Field | Value |
|---|---|
model | The model name the response reports |
status | The ResponseStatus, in debug form |
elapsed_ms | How long the call took |
input_tokens, output_tokens, total_tokens | Present only when the response carries usage |
A tool call event, with message tool call, has these:
| Field | Value |
|---|---|
tool | The tool's name |
elapsed_ms | How long the call took |
output_bytes | The length of the output text |
A call the guard refused is still an event, with the output denied: <reason>.
Content stays out by default
Message text, tool arguments and tool output can hold user data and secrets, so the default events carry none of it. with_content adds three fields:
let agent = Agent::new(client).hook(freyja::Tracing::new().with_content());
The model call event gains text, the model's output text. The tool call event gains arguments and output. Turn this on in development, and think before turning it on where logs are retained or shared.
The run span
With the feature on, every run executes inside a span named run, target freyja, level INFO, whether or not the Tracing hook is installed. The hook's events nest under it, so the calls of two runs happening at once stay apart. A streaming run gets a span too, and each message of a conversation is its own run with its own span.
The span starts empty and records its fields when the run ends:
| Field | Recorded when |
|---|---|
stop | The run returned, in debug form, such as Answered |
turns | The run returned |
input_tokens, output_tokens, total_tokens | The run returned |
error | The run failed with an Error instead |
Seeing the events
tracing-subscriber is a separate crate you add yourself. It is not part of Freyja. Its fmt subscriber prints events to the terminal:
tracing-subscriber = "0.3"
tracing_subscriber::fmt::init();
let agent = Agent::new(client).tool(add).hook(freyja::Tracing::new());
let run = agent.ask("What is 20 + 22?").await?;
This example calls a real model, so it needs a key and network. With the crate's env-filter feature, fmt::init() reads RUST_LOG, so RUST_LOG=freyja=info shows only Freyja's events. For a metrics pipeline or an OpenTelemetry exporter, install the matching subscriber layer instead. The events and the span are the same either way.
Tokens and cost
What a run spent, token budgets, a budget shared across a request, and prices in dollars are in Controlling cost.
See also
- Agents for
Run,StopReasonand the builder - Hooks for the trait
Tracingimplements - Multi-agent for how inner runs and team members add to usage
- Tools for
call_metered - Responses for
Usageand statuses