# Chalk Compute Reference: Trace
source: https://docs.chalk.ai/compute-docs#section-trace

Session-scoped telemetry for a unit of work, readable from a deployed function.

A [`Trace`](https://docs.chalk.ai/compute-docs#Trace) holds an agent session id and every query it makes is session-scoped, so it covers each turn of a multi-turn agent and the spans nested under them.

## `Trace`
source: https://docs.chalk.ai/compute-docs#Trace

Class

A session's worth of telemetry: every trace and span under one call.

Declaring a `@chalkcompute.function` parameter as [`Trace`](https://docs.chalk.ai/compute-docs#Trace) installs its Arrow contract, so the value arrives as the `large_utf8` session id Chalk bound to that argument.

.. _trace-waiting:

**Every read waits for ingest to settle.** Spans reach Chalk asynchronously — through a batching span processor, an OTLP collector, and finally ClickHouse — so a scorer running immediately after the task it scores can observe an incomplete session. That is worse than an error: a partial read produces a plausible wrong number. So `get_spans`, `summary`, and `traces` all poll until the number of spans they can see stops growing, and all take the same five arguments:

`wait` Poll until the span count holds steady across two consecutive reads. Pass [`False`](https://docs.python.org/3/library/constants.html#False) to read once, for a session that finished long ago. `timeout` Seconds to wait for that. Exceeding it raises `TimeoutError` rather than returning a knowingly incomplete result. `poll_interval` Seconds between polls. `expect_min` Minimum span count to wait for. Defaults to 1, so an empty result is treated as "not ingested yet" rather than "nothing to see". Pass 0 to accept an empty session. `quiet_for` Seconds the count must hold steady before the read returns, for a session that may still be running: an agent pauses between turns for longer than one poll. [`None`](https://docs.python.org/3/library/constants.html#None), the default, honors `CHALK_TRACE_COMPLETE_AFTER_SECONDS` when the function environment sets it -- that is how an environment's "trace is complete" setting reaches a scorer -- and otherwise returns as soon as the count holds across two polls. It cannot exceed `timeout`.

Waited reads also check completion counts for sampled local spans started with the call’s context before teardown, excluding the active scorer call. Threads without copied context and explicitly reparented spans are excluded. Remote spans need their own markers. Unusable markers fall back to quiet-count checks; valid count shortfalls raise at timeout.

For producers without markers, a steady count cannot tell "complete" from "quiet", so a long pause between agent turns can settle early. Pass `quiet_for` to require a longer silence, or `expect_min` when the caller knows better.

**Attributes**

- `id`: `str`
  The session id this handle queries.

### `Trace.from_id(session_id, chalk_client)`
source: https://docs.chalk.ai/compute-docs#Trace.from_id

Attach to a session by id, for use outside a scorer.

### `Trace.get_spans(operation_name, attribute_filters, limit, wait, timeout, poll_interval, expect_min, quiet_for)`
source: https://docs.chalk.ai/compute-docs#Trace.get_spans

Return this session's spans, waiting for span ingest to settle.

**Parameters**

- `operation_name`: `str | None` = `None`
  Only return spans with this operation name.
- `attribute_filters`: `Mapping[str, str | None] | None` = `None`
  Span attributes that must match. A [`None`](https://docs.python.org/3/library/constants.html#None) value matches any span carrying the key.
- `limit`: `int | None` = `None`
  Stop after this many spans. [`None`](https://docs.python.org/3/library/constants.html#None) returns all of them.
- `wait`: `bool` = `True`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `timeout`: `float` = `60.0`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `poll_interval`: `float` = `2.0`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `expect_min`: `int | None` = `None`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `quiet_for`: `float | None` = `None`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).

**Returns**

`list[Span]`

The session's spans.

**Raises**

- `TimeoutError`: If the span count does not reach `expect_min` and hold steady within `timeout`.

### `Trace.summary(wait, timeout, poll_interval, expect_min, quiet_for)`
source: https://docs.chalk.ai/compute-docs#Trace.summary

Return this session's aggregate: turn count, span count, wall clock.

Settles on the same signal as `get_spans`, so `trace_count` and `span_count` do not undercount a session whose spans are still arriving.

**Parameters**

- `wait`: `bool` = `True`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `timeout`: `float` = `60.0`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `poll_interval`: `float` = `2.0`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `expect_min`: `int | None` = `None`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `quiet_for`: `float | None` = `None`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).

**Returns**

`SessionSummary`

The session's aggregate.

**Raises**

- `TraceNotFoundError`: If the session never appears — it received no spans at all.
- `TimeoutError`: If it appears but its span count never holds steady.

### `Trace.traces(limit, wait, timeout, poll_interval, expect_min, quiet_for)`
source: https://docs.chalk.ai/compute-docs#Trace.traces

Return a summary per trace in this session — typically one per turn.

Settles on the same signal as `get_spans`: a turn that is still recording grows this session's span count without adding a trace, so waiting on the traces alone would return stale per-trace counts.

**Parameters**

- `limit`: `int | None` = `None`
  Stop after this many traces. [`None`](https://docs.python.org/3/library/constants.html#None) returns all of them.
- `wait`: `bool` = `True`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `timeout`: `float` = `60.0`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `poll_interval`: `float` = `2.0`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `expect_min`: `int | None` = `None`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).
- `quiet_for`: `float | None` = `None`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace).

**Returns**

`list[TraceSummary]`

One summary per trace, newest first.

**Raises**

- `TimeoutError`: If the session's span count does not reach `expect_min` and hold steady within `timeout`.

### `Trace.transcript(max_chars_per_turn, require_content, wait, timeout, poll_interval, expect_min, quiet_for)`
source: https://docs.chalk.ai/compute-docs#Trace.transcript

Render this session's LLM turns as text, oldest first.

A turn is a span carrying the OpenInference `input.value` attribute (the prompt) or Chalk's input-content metadata; `output.value` is its completion, when the span has one. Externally stored or omitted content renders as an explicit note: it is not an absent turn. Content URIs are neither fetched nor included in the text. Each turn is headed by the model and its token total. Prompts keep their _last_ `max_chars_per_turn` characters and completions their _first_, so truncation preserves the newest context the model saw and the start of what it said. A session with no turns renders as a one-line note rather than an empty string.

This is what `llm_judge` shows the model when a judge names `trace` among its inputs.

**Parameters**

- `max_chars_per_turn`: `int` = `4000`
  Characters kept of each prompt and of each completion.
- `require_content`: `bool` = `False`
  Raise [`TraceError`](https://docs.chalk.ai/compute-docs#TraceError) if a turn's raw prompt or completion is known to be unavailable or truncated, instead of rendering its omission note. The default [`llm_judge`](https://docs.chalk.ai/compute-docs#llm_judge) prompt enables this to avoid grading incomplete content. This does not disable the deliberate `max_chars_per_turn` display limit.
- `wait`: `bool` = `True`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace). The wait settles on the whole session but needs a turn: it holds until at least one LLM turn is there and the session's span count -- every span, not only the turns -- stays steady, so a turn that is still being ingested is never mistaken for none, the root span that lands before any turn cannot end the wait early, and a tool call between turns keeps it open. A session that shows no turn within `timeout` renders as the no-turns note rather than raising, once an unwaited read confirms it, so a judge still grades the row; pass `expect_min=0` to accept an empty transcript without waiting.
- `timeout`: `float` = `60.0`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace). The wait settles on the whole session but needs a turn: it holds until at least one LLM turn is there and the session's span count -- every span, not only the turns -- stays steady, so a turn that is still being ingested is never mistaken for none, the root span that lands before any turn cannot end the wait early, and a tool call between turns keeps it open. A session that shows no turn within `timeout` renders as the no-turns note rather than raising, once an unwaited read confirms it, so a judge still grades the row; pass `expect_min=0` to accept an empty transcript without waiting.
- `poll_interval`: `float` = `2.0`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace). The wait settles on the whole session but needs a turn: it holds until at least one LLM turn is there and the session's span count -- every span, not only the turns -- stays steady, so a turn that is still being ingested is never mistaken for none, the root span that lands before any turn cannot end the wait early, and a tool call between turns keeps it open. A session that shows no turn within `timeout` renders as the no-turns note rather than raising, once an unwaited read confirms it, so a judge still grades the row; pass `expect_min=0` to accept an empty transcript without waiting.
- `expect_min`: `int | None` = `None`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace). The wait settles on the whole session but needs a turn: it holds until at least one LLM turn is there and the session's span count -- every span, not only the turns -- stays steady, so a turn that is still being ingested is never mistaken for none, the root span that lands before any turn cannot end the wait early, and a tool call between turns keeps it open. A session that shows no turn within `timeout` renders as the no-turns note rather than raising, once an unwaited read confirms it, so a judge still grades the row; pass `expect_min=0` to accept an empty transcript without waiting.
- `quiet_for`: `float | None` = `None`
  See :ref:`the settling rules <trace-waiting>` on [`Trace`](https://docs.chalk.ai/compute-docs#Trace). The wait settles on the whole session but needs a turn: it holds until at least one LLM turn is there and the session's span count -- every span, not only the turns -- stays steady, so a turn that is still being ingested is never mistaken for none, the root span that lands before any turn cannot end the wait early, and a tool call between turns keeps it open. A session that shows no turn within `timeout` renders as the no-turns note rather than raising, once an unwaited read confirms it, so a judge still grades the row; pass `expect_min=0` to accept an empty transcript without waiting.

## `TraceClient`
source: https://docs.chalk.ai/compute-docs#TraceClient

Class

Query spans, traces, and session summaries in a Chalk environment.

### `TraceClient.__enter__()`
source: https://docs.chalk.ai/compute-docs#TraceClient.__enter__

### `TraceClient.__exit__(_args)`
source: https://docs.chalk.ai/compute-docs#TraceClient.__exit__

### `TraceClient.close()`
source: https://docs.chalk.ai/compute-docs#TraceClient.close

Close the connection created by this client.

### `TraceClient.list_spans(session_id, operation_name, attribute_filters, limit)`
source: https://docs.chalk.ai/compute-docs#TraceClient.list_spans

List a session's spans, paging transparently.

### `TraceClient.list_sandbox_spans(sandbox_id, start_time, limit)`
source: https://docs.chalk.ai/compute-docs#TraceClient.list_sandbox_spans

List spans recorded for a sandbox, paging transparently.

`start_time` sets the earliest span start to include. `limit` caps the total number of spans returned across all pages.

### `TraceClient.get_session(session_id)`
source: https://docs.chalk.ai/compute-docs#TraceClient.get_session

Fetch one session's aggregate summary.

### `TraceClient.list_traces(session_id, limit)`
source: https://docs.chalk.ai/compute-docs#TraceClient.list_traces

Iterate a session's traces, newest first, paging transparently.

## `TraceSummary`
source: https://docs.chalk.ai/compute-docs#TraceSummary

Class

Aggregate over one OpenTelemetry trace — typically one agent turn.

**Attributes**

- `trace_id`: `str`
- `start_time`: `datetime | None`
- `end_time`: `datetime | None`
- `duration`: `timedelta | None`
- `span_count`: `int`
- `root_span_names`: `tuple[str, ...]`
- `session_id`: `str | None`

## `SessionSummary`
source: https://docs.chalk.ai/compute-docs#SessionSummary

Class

Aggregate over every trace sharing one session id.

**Attributes**

- `session_id`: `str`
- `start_time`: `datetime | None`
- `end_time`: `datetime | None`
- `duration`: `timedelta | None`
- `trace_count`: `int`
- `span_count`: `int`
- `first_root_span_name`: `str | None`

## `Span`
source: https://docs.chalk.ai/compute-docs#Span

Class

One span of a session's telemetry.

Token, cost, model and provider properties normalize common telemetry attributes without making additional requests. Numeric properties return [`None`](https://docs.python.org/3/library/constants.html#None) when unavailable or invalid; a recorded zero remains zero. Metrics describe this span only, never an aggregate over its children.

**Attributes**

- `span_id`: `str`
- `trace_id`: `str`
- `operation_name`: `str`
- `parent_span_id`: `str | None`
- `start_time`: `datetime | None`
- `end_time`: `datetime | None`
- `duration`: `timedelta | None`
- `status_code`: `SpanStatusCode`
- `status_description`: `str | None`
- `kind`: `SpanKind`
- `attributes`: `Mapping[str, str]`
- `resource_attributes`: `Mapping[str, str]`
- `session_id`: `str | None`
  Session this span was grouped into, if it carries one.
- `model`: `str | None`
  Model used, preferring Chalk's normalized value and the response model.
- `provider`: `str | None`
  LLM provider, from OpenInference or GenAI attributes.
- `input_tokens`: `int | None`
  Prompt tokens, including any cache tokens already included by the emitter.
- `output_tokens`: `int | None`
  Completion tokens, including any reasoning tokens reported in that total.
- `total_tokens`: `int | None`
  Recorded total, or input plus output when both counts are available.

  Cache and reasoning counts are breakdowns and are not added again. A partial count does not imply the missing count is zero.
- `cache_read_tokens`: `int | None`
  Prompt tokens read from the provider's cache.
- `cache_write_tokens`: `int | None`
  Prompt tokens written to the provider's cache.
- `reasoning_tokens`: `int | None`
  Reasoning tokens within the completion, when reported.
- `cost`: `float | None`
  Recorded total cost in USD; no model pricing is inferred.

  Tries Chalk's normalized cost, OpenInference total/legacy cost, then GenAI cost, skipping malformed, negative and non-finite values.

### `Span.attribute_float(key)`
source: https://docs.chalk.ai/compute-docs#Span.attribute_float

Return a finite float, or [`None`](https://docs.python.org/3/library/constants.html#None) if absent, unparseable or non-finite.

Span attributes arrive as strings — `ChalkSpan.attributes` is a `map<string, string>` — so summing `span.attributes["llm.cost"]` directly concatenates text instead of adding numbers. Use this instead.

### `Span.attribute_int(key)`
source: https://docs.chalk.ai/compute-docs#Span.attribute_int

Return an attribute parsed as an int, or [`None`](https://docs.python.org/3/library/constants.html#None) if absent or unparseable.

## `SpanKind`
source: https://docs.chalk.ai/compute-docs#SpanKind

Enum (bases: `str`, `Enum`)

OpenTelemetry span kind.

## `SpanStatusCode`
source: https://docs.chalk.ai/compute-docs#SpanStatusCode

Enum (bases: `str`, `Enum`)

OpenTelemetry span status.

## `TraceError`
source: https://docs.chalk.ai/compute-docs#TraceError

Exception (bases: `Exception`)

Base exception for trace and span queries.

## `TraceNotFoundError`
source: https://docs.chalk.ai/compute-docs#TraceNotFoundError

Class (bases: `TraceError`)

Raised when a session or span does not exist.
