# Observability

> What zelk exports as spans and counters, what it keeps out, and why the session log stays the record while telemetry is only a view of it.

zelk keeps two accounts of a session, for two different readers. The
**session log** is the record. It is durable and ordered, and it is the only
thing zelk reads back to continue a session, replay it or recover it after a
crash. **Telemetry** is a view for the people who run the system around zelk:
spans that show how long each step took and where a run waited, and counters
that add up tokens and tool calls across sessions. zelk only sends it. It
never reads it back. Observer callbacks run inline and must return promptly.

The diagram plays one run as a trace waterfall. Each bar is a span, indented
under its parent and drawn from the moment it starts to the moment it ends.
The row of ticks beneath is the session log on the same time axis. The
scenes play a normal run, a wait for an approval, a crash followed by a
replay, and a tool that fails.

The spans zelk emits, as a tree:

- `zelk.execution`: one driver execution after log replay. Suspension, caller stop or recorded interruption leaves status unset and sets zelk.outcome.
- `zelk.turn`, under `zelk.execution`: one model turn: its model call and what the turn’s tool calls did.
- `zelk.model_call`, under `zelk.turn`: one call to the model provider.
- `zelk.compaction`, under `zelk.turn`: a provider attempt to compact history. It sits under the execution if no turn is open.
- `zelk.tool`, under `zelk.turn`: one tool call from its first observed step.
- `zelk.hook`, under `zelk.tool`: PreToolUse and PermissionRequest hooks. Other hooks sit under the active turn or the execution if no turn is open.
- `zelk.classifier`, under `zelk.tool`: the auto-mode reviewer judging the call.
- `zelk.tool.awaiting_approval`, under `zelk.tool`: the wait for a person to answer an approval.
- `zelk.tool.run`, under `zelk.tool`: the call running; a subagent’s execution nests here.
- `zelk.search`, under `zelk.tool.run`: a native web search, a model call of its own.

The counters, which are added as spans end:

- `zelk.tokens.input`: input tokens of every model call.
- `zelk.tokens.output`: output tokens of every model call.
- `zelk.tokens.cache_read`: cached input tokens read.
- `zelk.tokens.cache_write`: cached input tokens written.
- `zelk.tool.calls`: tool attempts that return (by `zelk.tool` and `zelk.outcome`).
- `zelk.permission.decisions`: PermissionDecided records for asks, denials and reviews that fall back to approval (by `zelk.verdict` and `zelk.step`).
- `zelk.compactions`: successful compactions.
- `zelk.retries`: retries of model and compaction provider calls.

Four runs as a waterfall on a time axis in seconds, with the session log beneath on the same axis:

- **A normal run**. A run is a tree of spans, and a wave of tool calls is a wave of bars. The execution holds a turn for each model turn. This wave’s tool calls overlap, and each tool span holds the attempt itself. The log beneath shows selected records for those effects.
   - 0 s · zelk.execution starts, with zelk.session and zelk.model. The log records execution_traced, the IDs the next execution will link to. zelk.turn starts, and zelk.model_call under it.
   - 10 s · zelk.model_call ends: zelk.outcome=done, 3200 tokens in and 140 out, which the counters add. The model asked for 3 calls. Each gets a zelk.tool with its zelk.tool.run, and they run at once.
   - 14 s · zelk.tool.run ends, zelk.outcome=done, and zelk.tool.calls counts the call.
   - 16 s · zelk.tool.run ends, zelk.outcome=done, and zelk.tool.calls counts the call.
   - 20 s · zelk.turn starts, and zelk.model_call under it. zelk.tool.run ends, zelk.outcome=done, and zelk.tool.calls counts the call.
   - 36 s · zelk.model_call ends: zelk.outcome=done, 4100 tokens in and 380 out, which the counters add. The run finishes: the log records run_finished, and zelk.execution ends. Every span has ended.
- **Waiting on a person**. An approval wait is a span of its own, and the long one. The call asked a person, who took sixteen seconds. The wait is a child of the tool call, so the time spent waiting shows apart from the time spent running, and its outcome says how it was answered. The command itself took five seconds.
   - 0 s · zelk.execution starts, with zelk.session and zelk.model. The log records execution_traced, the IDs the next execution will link to. zelk.turn starts, and zelk.model_call under it.
   - 9 s · zelk.model_call ends: zelk.outcome=done, 2800 tokens in and 120 out, which the counters add. zelk.hook starts under the call (zelk.hook, zelk.hook_event=PreToolUse). This hook leaves the decision to policy.
   - 11 s · zelk.tool.awaiting_approval starts: the call needs a person. The log records permission_decided; zelk.permission.decisions counts it.
   - 27 s · The wait ends after 16 seconds, zelk.outcome=allow_once. It is a span of its own, apart from the run. zelk.tool.run starts (zelk.tool, zelk.attempt): the approved call runs.
   - 32 s · zelk.turn starts, and zelk.model_call under it. zelk.tool.run ends, zelk.outcome=done, and zelk.tool.calls counts the call.
   - 39 s · zelk.model_call ends: zelk.outcome=done, 3300 tokens in and 210 out, which the counters add. The run finishes: the log records run_finished, and zelk.execution ends. Every span has ended.
- **A crash and a replay**. A crash loses the spans in flight. The log keeps the recorded intents. Only ended spans can be exported. The model call had ended, so the collector can receive it without its parent spans. The next process starts a new execution that links to the old one and runs the lost tool call again as attempt 2.
   - 0 s · zelk.execution starts, with zelk.session and zelk.model. The log records execution_traced, the IDs the next execution will link to. zelk.turn starts, and zelk.model_call under it.
   - 8 s · zelk.model_call ends: zelk.outcome=done, 3000 tokens in and 110 out, which the counters add. The model asked for Grep ErrEmpty: a zelk.tool starts, and a zelk.tool.run beneath it.
   - 12 s · The process is killed with 4 spans open. A span reaches the collector only when it ends, so these never do.
   - 20 s · A new process starts a new zelk.execution, in a trace of its own, linked to the old one by the IDs the log kept. The log closes the lost call (attempt_ended) and it runs again as attempt 2.
   - 28 s · zelk.turn starts, and zelk.model_call under it. zelk.tool.run ends, zelk.outcome=done, and zelk.tool.calls counts the call.
   - 38 s · zelk.model_call ends: zelk.outcome=done, 3600 tokens in and 260 out, which the counters add. The run finishes: the log records run_finished, and zelk.execution ends. Every span has ended.
- **A tool that fails**. A failed tool result leaves the span’s status unset. The first Read sets zelk.outcome=failed on its span and counter. The model reads the failed result and asks again.
   - 0 s · zelk.execution starts, with zelk.session and zelk.model. The log records execution_traced, the IDs the next execution will link to. zelk.turn starts, and zelk.model_call under it.
   - 9 s · zelk.model_call ends: zelk.outcome=done, 2600 tokens in and 100 out, which the counters add. The model asked for Read parse.cgo: a zelk.tool starts, and a zelk.tool.run beneath it.
   - 12 s · zelk.turn starts, and zelk.model_call under it. zelk.tool.run ends with zelk.outcome=failed, and zelk.tool.calls counts it so. No error status: a failed result is for the model, not a fault in zelk.
   - 21 s · zelk.model_call ends: zelk.outcome=done, 2900 tokens in and 130 out, which the counters add. The model asked for Read parse.go: a zelk.tool starts, and a zelk.tool.run beneath it.
   - 26 s · zelk.turn starts, and zelk.model_call under it. zelk.tool.run ends, zelk.outcome=done, and zelk.tool.calls counts the call.
   - 38 s · zelk.model_call ends: zelk.outcome=done, 3500 tokens in and 300 out, which the counters add. The run finishes: the log records run_finished, and zelk.execution ends. Every span has ended.

## The log is the record, and the trace is a view

The diagram aligns spans with selected records from the session log. An effect
such as a model call, tool run or hook has a durable intent and a terminal
record. A span measures the live work around those records. The execution,
turn and tool spans group work across multiple records.

- The log's acknowledged records survive a process crash. They hold the
  conversation, the recorded decisions and tool results.
- Telemetry is best effort. It leaves the process through your exporter,
  where it may be batched, sampled or dropped, and a process that dies takes
  with it whatever it hadn't sent.

So use telemetry to ask how long, how often and how much, across many
sessions, and use the log to ask what happened in one. `zelk sessions report`
and `Session.Events` read the log. Nothing in zelk reads a trace.

## What zelk emits

Each observed execution starts a span after loading the session log. It
starts a trace if the caller's context has no parent span.

```text
zelk.execution
├─ zelk.hook
├─ zelk.compaction
└─ zelk.turn
   ├─ zelk.model_call
   ├─ zelk.compaction
   ├─ zelk.hook
   └─ zelk.tool
      ├─ zelk.hook
      ├─ zelk.classifier
      ├─ zelk.tool.awaiting_approval
      └─ zelk.tool.run
         └─ zelk.search
```

| Span | Covers | Attributes |
|---|---|---|
| `zelk.execution` | one driver execution after replay | `zelk.session`, `zelk.model`, `zelk.agent_type` in a subagent, `zelk.outcome` for suspension, stop or interruption |
| `zelk.turn` | one model turn: its model call and what its tool calls did | `zelk.run`, `zelk.turn`, `zelk.resumed` |
| `zelk.model_call`, `zelk.compaction` | one provider attempt for a model reply or compaction. A compaction with no active turn sits under the execution | `zelk.model`, `zelk.effect_id`, `zelk.attempt`, then `zelk.outcome`, `zelk.stop_reason`, `zelk.tokens.input`, `zelk.tokens.output` when available |
| `zelk.tool` | a tool call from its first observed step, whether a hook, review, approval wait or run | `zelk.tool`, `zelk.call_id` |
| `zelk.hook` | `PreToolUse` and `PermissionRequest` sit under their tool call. Other hooks sit under the active turn or the execution if no turn is open | `zelk.hook`, `zelk.hook_event` |
| `zelk.classifier` | [the reviewer](/concepts/reviewer/) judging a call in `auto` mode | `zelk.verdict`, token counts |
| `zelk.tool.awaiting_approval` | the wait for a person to answer | `zelk.request`, `zelk.outcome` |
| `zelk.tool.run` | the tool attempt running | `zelk.tool`, `zelk.effect_id`, `zelk.attempt`, `zelk.outcome` |
| `zelk.search` | a native web search under a `WebSearch` run, a model call of its own | `zelk.results`, `zelk.model` and token counts when available |

A few of these are worth reading closely.

Calls dispatched in the same wave can overlap in the
waterfall, as the first scene shows. A call that needs a person waits alone,
and its wait is a span of its own, so the time spent waiting shows apart from
the time spent running. Its `zelk.outcome` says how it was answered:
`allow_once`, `allow_edited`, `allow_session`, `deny` or `cancel`, or
`expired` or `no_answer` where nobody could be asked.

A tool that fails is not an error. Its span and counter have
`zelk.outcome=failed`, and the span's status stays unset: the failure is a
result for the model, which reads it and tries again.
A provider or hook that returns an error sets error status on its span.
When an execution suspends, the caller cancels its context or its deadline
expires, or the execution records an interrupted ending, the execution span
has unset status and `zelk.outcome` of `suspended`, `stopped` or `interrupted`,
respectively. Failures known when the span ends, including persistence errors
and lease loss, set error status. If authority expires during synchronous
span export, the caller still receives a lease-loss error.

A subagent's execution nests under the `zelk.tool.run` of the call that
started it, so a delegation tree is one trace. See
[Subagents](/concepts/subagents/).

Durations come from the spans. The counters below contain no duration or
cost measurements: price the token counters with your own rates.

## Counters

Token usage and tool-call counts are reported when attempts return. Permission
counts come from appends to the session log, and retries count when scheduled.

| Counter | Counts |
|---|---|
| `zelk.tokens.input`, `zelk.tokens.output` | reported token usage for model calls, including the reviewer and native web search |
| `zelk.tokens.cache_read`, `zelk.tokens.cache_write` | cached input tokens read and written |
| `zelk.tool.calls` | tool attempts that return, by `zelk.tool` and `zelk.outcome` |
| `zelk.permission.decisions` | `PermissionDecided` records by `zelk.verdict` and `zelk.step`. An ask, a denial or an inconclusive review that falls back to approval can add one. Allows add nothing to this counter |
| `zelk.compactions` | successful compactions |
| `zelk.retries` | retries of model and compaction provider calls |

Counters carry few attributes on purpose. The identity attributes you add
to spans (below) never go on a counter, since a tenant or a user for each
series would multiply them without end.

## What is not exported

By default, spans carry metadata such as identifiers, tool and model names,
outcomes and token counts. The OpenTelemetry adapter records exception events
and error status messages independently of capture. Those messages can contain
sensitive text from a provider or a hook.

Each kind of content is a separate opt-in, because content can hold anything
a user, a file or a model said, secrets included:

| Capture | Adds |
|---|---|
| prompts | a `zelk.prompt` event on the driver's model and compaction calls, with the system prompt and portable request history |
| tool inputs | `zelk.tool.input` on the `zelk.tool` span |
| tool outputs | output text in `zelk.tool.output` on the `zelk.tool.run` span |
| provider bodies | a `zelk.provider.response` event per response item, carrying the inline raw payload when available. Items that carry reasoning are redacted in this event |

Prompt capture serializes the portable request history, which can include
reasoning summaries. The session log retains the full record under your own
retention rules.

## Wiring an observer

zelk never starts or stops your telemetry. A Go host builds its own
OpenTelemetry providers and hands them to the adapter, which is a module of
its own so that core carries no OpenTelemetry dependency:

```go
agent, err := zelk.New(ctx, provider,
	zelk.WithModel(model),
	zelk.WithObserver(zelkotel.New(tracerProvider, meterProvider)),
	zelk.WithCapture(observe.Capture{ToolInputs: true}),
)
```

`WithObserver` takes anything that implements `observe.Observer`, two methods
that start a span and add to a counter, so a host with other telemetry
writes its own. Without an observer zelk records nothing outside the log.
Observing never fails a session, and an observer must not block: it runs on
zelk's own goroutines.

Spans start from the context you pass, so a span your program started
becomes the execution's parent. To mark whose work it is, pass
`zelk.WithTraceAttrs(observe.String("tenant", "acme"))` when you start a
run; every span of that execution and of its subagents carries it.

The zelk command builds the same pipeline for you. `-otel` turns it on, the
standard `OTEL_*` variables say where it goes, and `-otel-capture` opts in to
content, kind by kind:

```sh
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 zelk exec -otel "fix the build"
```

It exports in batches and flushes what is left when it exits. A failure to
export never interrupts a run or writes over the terminal UI: zelk keeps the
errors and reports them when it exits. Set `TRACEPARENT` and zelk's
executions become children of whatever started it, such as a CI job. The
[OpenTelemetry guide](/cli/otel/) has every variable.

## Across a crash and a replay

zelk calls the observer for live work. Folding the stored history produces
no spans or counters. A driver execution that loads the session starts its
own span for the work it does after replay.

A crash shows the difference between the two accounts. Only ended spans
are eligible for export. In the third scene, the execution, the turn,
the tool call and its run are still open when the process dies. An ended
span can also be lost if the exporter has yet to send it. The log's
recorded intents show what was open.

The next process settles what the crash left, as
[Durability and recovery](/concepts/durability/) describes, and the trace
shows only that:

- An execution records its span's valid IDs in the log. The next execution
  links to that span. The new span joins the
  caller's trace if it has a parent, or starts a trace of its own.
- A lost model attempt or an eligible tool attempt is closed in the log
  and can run again with a higher `zelk.attempt`. Eligible tools are trusted,
  read-only and idempotent, or enforce idempotency. A turn resumed to finish
  tool calls after its model call already completed has `zelk.resumed=true`.
  The new attempt gets its own span.
- If an open tool attempt is ineligible to run again, recovery records its
  outcome as unknown and creates no new span for running that attempt.
- In the scene, the process dies while the tool is still running, so only
  the retry adds to the tool-call counter. Counts are independent of log
  acknowledgments. A crash after counting and before recording the result
  can cause both attempts to be counted.

When a session suspends to wait for a person, its execution span and any open
approval or tool spans end with `zelk.outcome=suspended`. When the answer
comes, perhaps hours later in another process, a new execution links back to
the first.

## Where to go deeper

- The [OpenTelemetry guide](/cli/otel/) gives the flags, the `OTEL_*`
  variables, `TRACEPARENT` and the content you can capture.
- [Observability in Go](/go/observability/) shows the adapter, your own
  observer and routing zelk's logs; the
  [`observe`](/reference/go/observe/) and
  [`adapters/otel`](/reference/go/adapters/otel/) references list every name.
- [Durability and recovery](/concepts/durability/) explains why the log is
  written before zelk acts, and what the next process does with an open
  attempt.
- [Hooks](/concepts/hooks/) and [Permissions](/concepts/permissions/) show
  where `zelk.hook` and the approval wait come from.
- [Sessions](/cli/sessions/) shows `zelk sessions report`, which reads the
  record.
