# @demlik/tea/otel

> an agent run as OpenTelemetry spans.

Tier: `experimental`

```ts
import { … } from "@demlik/tea/otel";
```

## Start here

The exports below are alphabetical, which says nothing about where to begin.
These are the ones to read first:

| Symbol | Reach for it when |
| --- | --- |
| [`traceAgent`](#traceAgent) | You hold an agent runtime and want each run in Langfuse, or any OpenTelemetry backend, as one span tree. |
| [`agentSpans`](#agentSpans) | The same span writer as an `onEvent` listener, for a `defineAgent` run. |

## Exports (8)

| Symbol | Kind | Tier | Summary |
| --- | --- | --- | --- |
| [`AgentEventSource`](#AgentEventSource) | Interface | experimental | Anything that delivers an agent's events by type — a runtime built with `events: agentEvents()`. |
| [`agentSpans`](#agentSpans) | Function | experimental | The span writer behind traceAgent, as a listener. |
| [`AgentSpans`](#AgentSpans) | Interface | experimental | The span writer traceAgent subscribes, for wiring to an `onEvent` listener. |
| [`maskBase64DataUris`](#maskBase64DataUris) | Variable | experimental | The default SpanMask: every base64 `data:` URI in a string becomes `[base64 data URI stripped before export]`. |
| [`runTraceId`](#runTraceId) | Function | experimental | The trace id a run's spans are written into: the SHA-256 of `runId` as lowercase hex, cut to 32 characters. |
| [`SpanMask`](#SpanMask) | Type | experimental | A mask over one exported input or output — the same shape as `LangfuseSpanProcessor`'s `mask`, so one function serves both. |
| [`traceAgent`](#traceAgent) | Function | experimental | Trace an agent runtime into OpenTelemetry. |
| [`TraceAgentOptions`](#TraceAgentOptions) | Interface | experimental | How traceAgent writes spans. |

## Declarations

<a id="AgentEventSource"></a>

### `AgentEventSource`

```ts
interface AgentEventSource<R> {
  on<
    K extends "BrainStarted" | "RunDone" | "ToolFailed" | "ToolSettled" | "ToolStarted" | "TurnSettled",
  >(
    type: K,
    handler: (
      event: Extract<AgentEventHead & {
        readonly _tag: "BrainStarted";
        readonly model: string | null;
        readonly payload: unknown;
        readonly purpose: string;
        readonly turn: number;
      }, { _tag: K }> | Extract<AgentEventHead & {
        readonly _tag: "TurnSettled";
        /** Feedback issued for this empty turn, when the caller refused its finish. */
        readonly nudge?: string;
        readonly turn: AgentTurn;
        /** The usage the provider reported for this turn. Absent → none reported. */
        readonly usage?: TurnUsage;
      }, { _tag: K }> | Extract<AgentEventHead & {
        readonly _tag: "ToolStarted";
        readonly args: Readonly<Record<string, unknown>>;
        readonly callId: string;
        readonly name: string;
      }, { _tag: K }> | Extract<AgentEventHead & {
        readonly _tag: "ToolFailed";
        readonly callId: string;
        readonly failure: ToolFailure;
        readonly name: string;
      }, { _tag: K }> | Extract<AgentEventHead & {
        readonly _tag: "RunDone";
        readonly status: AgentEndedStatus;
      }, { _tag: K }> | Extract<AgentEventHead & {
        readonly _tag: "ToolSettled";
        readonly callId: string;
        readonly result: R;
      }, { _tag: K }>,
    ) => void,
  ): () => void;
}
```

<a id="agentSpans"></a>

### `agentSpans`

```ts
function agentSpans<R>(opts: TraceAgentOptions): AgentSpans<R>
```

<a id="AgentSpans"></a>

### `AgentSpans`

```ts
interface AgentSpans<R> {
  /**
   * End every span still open — the leg this process ran, when it stops
   * before its run does. Each carries `tea.run.detached: true`.
   *
   * With `at`, every open span ends at `at`, used as given. Without it, each
   * run's spans end at the `at` of the last event that run folded — its own
   * clock, never wall time.
   */
  readonly end: (at?: number) => void;
  /** Fold one event into the span tree. */
  readonly onEvent: (event: AgentEvent<R>) => void;
}
```

<a id="maskBase64DataUris"></a>

### `maskBase64DataUris`

```ts
const maskBase64DataUris: SpanMask
```

<a id="runTraceId"></a>

### `runTraceId`

```ts
function runTraceId(runId: string): string
```

<a id="SpanMask"></a>

### `SpanMask`

```ts
type SpanMask = (params: { readonly data: unknown }) => unknown
```

<a id="traceAgent"></a>

### `traceAgent`

```ts
function traceAgent<R>(
  runtime: AgentEventSource<R>,
  opts: TraceAgentOptions,
): () => void
```

<a id="TraceAgentOptions"></a>

### `TraceAgentOptions`

```ts
interface TraceAgentOptions {
  /**
   * Applied to every exported input and output. Omit → maskBase64DataUris,
   * which keeps a screenshot a tool returned from being exported as megabytes
   * of base64.
   */
  readonly mask?: SpanMask;
  /**
   * The agent's name, on `gen_ai.agent.name`, the run span's name and
   * Langfuse's trace name. Omit → `"agent"`.
   */
  readonly name?: string;
  /**
   * The context every run span starts under — a span the host already has
   * open, as `trace.setSpan(context.active(), span)`. The run then lands in the
   * host's trace as that span's child, and its brain and tool spans follow.
   *
   * `parent` wins over `traceIdOf`: when both are given, the run is in the
   * parent's trace and `traceIdOf` is never called. With a `parent`, the run's
   * trace is the host's, so keeping one trace across a kill is the host's job
   * too — a resumed run's spans go wherever the resuming host's `parent`
   * points. Omit → each run hangs off a detached parent in the trace
   * `traceIdOf(runId)` names, which a resumed process derives again.
   */
  readonly parent?: Context;
  /**
   * The trace id a run writes into, from its `runId`. Omit → runTraceId.
   * Must return 32 lowercase hex characters, not all zero. Not called when
   * parent is given.
   */
  readonly traceIdOf?: (runId: string) => string;
  /** The tracer the spans are started on — `provider.getTracer("…")`. */
  readonly tracer: Tracer;
}
```
