# `Nous.Eval.Trace`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/eval/trace.ex#L1)

The telemetry events one eval sample produced, in emission order.

A trace is everything Nous emitted under the sample's `trace_id`: the root
run, its model and tool calls, guard verdicts, and every sub-agent run
(linked through `parent_agent_run_id` / `parent_tool_call_id`). It
survives failed and killed runs, so a failing sample can be inspected up
to the point it stopped.

Events carry the metadata documented in `Nous.Telemetry`. Content keys
(`messages`, `response`, `arguments`, `result`, `details`, `errors`) hold
user data.

`truncated?` is true when the per-trace event or byte cap was reached; the
tail of the trace is then missing, and a scorer that needs to prove an
absence ("tool X was never called") must not pass on it.

## Queries

Scorers read a trace through the pure functions below rather than
walking events: `tool_calls/2`, `model_calls/1`, `guard_verdicts/2`,
`output_retries/2`, `runs/1`, `usage/1` and `transcript/2`. They are
total: an unknown event or an event with malformed metadata is ignored,
never raised on.

### Scope

Functions taking `:scope` select runs of the run tree:

  * `:root` - the root run only (a `delegate_task` call is a root call; the
    sub-agent's own calls are not)
  * `:tree` - every run
  * `{:run, agent_run_id}` - one run
  * `{:sub_agent, template}` - the runs whose start event carries
    `sub_agent_template: template` (not their descendants)

# `scope`

```elixir
@type scope() :: :root | :tree | {:run, String.t()} | {:sub_agent, String.t()}
```

# `t`

```elixir
@type t() :: %Nous.Eval.Trace{
  dropped: non_neg_integer(),
  events: [Nous.Eval.Trace.Event.t()],
  trace_id: String.t(),
  truncated?: boolean()
}
```

# `guard_verdicts`

```elixir
@spec guard_verdicts(t(), keyword()) :: [Nous.Eval.Trace.GuardVerdict.t()]
```

The guard decisions in the trace, in emission order, as
`Nous.Eval.Trace.GuardVerdict`s.

Options:

  * `:guard` - `:decision_guard | :input_guard | :loop_guard | :hook |
    :all` (default `:all`). `:hook` is a decision hook that is not one of
    DecisionGuard's surfaces: a DecisionGuard hook surface reports both a
    `[:nous, :decision_guard, :verdict]` and a `[:nous, :hook, :decision]`
    whose `hook_name` starts with `"decision_guard:"`; the hook event is
    dropped so each decision counts once.
  * `:scope` - see "Scope" in the moduledoc (default `:tree`)

# `model_calls`

```elixir
@spec model_calls(t()) :: [Nous.Eval.Trace.ModelCall.t()]
```

The model requests in the trace (every run), in request order.

Each `Nous.Eval.Trace.ModelCall` pairs a model start with the next stop
or exception of the same run; a start with neither is `:incomplete`.

# `output_retries`

```elixir
@spec output_retries(t(), keyword()) :: [map()]
```

The structured-output retries (`[:nous, :agent, :structured_output,
:retry]`) in the trace, in order, as maps with `:agent_run_id`,
`:parent_agent_run_id`, `:attempt`, `:retries_left`, `:errors` and
`:seq`.

Options: `:scope` (default `:tree`; see "Scope" in the moduledoc).

# `root_run_id`

```elixir
@spec root_run_id(t()) :: String.t() | nil
```

The `agent_run_id` of the root run, or `nil` when the trace has no root
run start event.

# `runs`

```elixir
@spec runs(t()) :: [Nous.Eval.Trace.AgentRun.t()]
```

The agent runs in the trace as `Nous.Eval.Trace.AgentRun`s in start order,
with `children` linking each run to the runs it started. The root run is
the one without a `parent_agent_run_id`.

# `tool_calls`

```elixir
@spec tool_calls(t(), keyword()) :: [Nous.Eval.Trace.ToolCall.t()]
```

The tool calls in the trace, in request order.

Options:

  * `:scope` - see "Scope" in the moduledoc (default `:root`)
  * `:include` - which outcomes count: a list of `Nous.Eval.Trace.ToolCall`
    outcomes, or `:all` (default `[:executed]`; failed, denied and
    incomplete attempts are excluded)

A start event without a matching stop yields a call with outcome
`:incomplete`. When the stop reports `approval: :edited`, `arguments` are
the edited arguments the tool ran with.

# `transcript`

```elixir
@spec transcript(t(), keyword()) :: [Nous.Message.t()]
```

Rebuilds the message history of a run from its events, for runs that
failed before returning a context.

The result is the request messages of the run's last model request, its
response (when it completed), and a tool message for every tool call
that finished after that response. Returns `[]` when the run made no
model request.

Options: `:run` - the `agent_run_id` (default: the root run).

# `usage`

```elixir
@spec usage(t()) :: Nous.Usage.t()
```

The token usage of every completed model request in the trace (the whole
run tree), summed into one `Nous.Usage`. `requests` counts model stop
events; `tool_calls` counts the tool calls the responses requested.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
