LlmCall.Telemetry (llm_call v0.4.0)

Copy Markdown View Source

The telemetry events LlmCall emits.

The library emits and attaches no handler: there is no reporter, no metrics backend and no default Logger output, so the cost is zero until a host calls :telemetry.attach_many/4.

Events cover the calls that decode a provider response — LlmCall.complete/3, stream/3, generate/3 and poll/3. A call that fails before the request is built (a :validation error from validate_request/2 or encode_request/2) emits nothing: no request was made, and the caller already holds the error. LlmCall.execute/2 and fetch/2 emit nothing here either: they are Finch requests with a JSON body, already covered by [:finch, :request, :start | :stop | :exception].

Every event carries the same four metadata keys: operation (:complete, :stream, :generate, :poll), provider, codec, and model (nil for a call that has no %LlmCall.Request{}, such as poll/3).

[:llm_call, :request, ...]

  • :start — metadata as above, measurements %{system_time: integer}.
  • :stop — measurements %{duration: :native} plus the LlmCall.Usage token counts the provider reported. A count that is nil is omitted, never zero: "not reported" and "reported zero" are different facts. Metadata adds outcome: :ok and stop_reason, or outcome: :error and error_kind, status, retryable, retry_after_ms.
  • :exception — measurements %{duration: :native}; metadata adds kind, reason and stacktrace. Emitted instead of :stop when the call raises, and the raise still reaches the caller.

[:llm_call, :stream, ...]

  • :start — emitted when the stream is reduced, not when it is built: building a stream issues no request.
  • :first_event — measurements %{duration: :native}, the time to the first event. Emitted once, on the first event yielded; a stream that yields none does not emit it.
  • :first_text — the same measurements, on the first :text_delta instead. This is the time to the first token a caller can see: a :message_start or a :thinking_delta does not count, so a stream that only emits those has no :first_text. first_text - first_event is the provider's skeleton-frame overhead, which is what separates "the provider is slow" from "the network is slow".
  • :stop — measurements %{duration: :native}; metadata adds outcome: :done | :halted, status and events (the count yielded).
  • :exception — as for a request.

A stream that is built and never reduced emits nothing, and one whose consumer dies mid-read emits :start with no terminal event.

What metadata never carries

No header, no request or response body, no message and no delta. See docs/design.md §26.2: an Inspect exception can stay in a log, and telemetry metadata ends up in a third-party tracing backend.

Summary

Types

metadata()

@type metadata() :: %{
  operation: operation(),
  provider: module(),
  codec: module() | nil,
  model: String.t() | nil
}

operation()

@type operation() :: :complete | :stream | :generate | :poll

result()

@type result() :: {:ok, term()} | {:error, LlmCall.Error.t()}