Telemetry

Forward spans into OSLog, OpenTelemetry, or your analytics, and control what each call records.

AITelemetry is a process-wide hook, disabled until you set a collector. Conform to AITelemetryCollector and forward events into OSLog signposts, an OpenTelemetry exporter, or your analytics:

struct LogCollector: AITelemetryCollector {
  func record(_ event: AITelemetryEvent) {
    logger.info("\(event.name) \(event.phase)", metadata: [
      "model": "\(event.attributes["ai.model.id"] ?? "")",
      "duration": "\(event.duration)"
    ])
  }
}
AITelemetry.collector = LogCollector()

Each call is bracketed by start and end (or error) phases with a measured duration. Spans cover ai.generateText, ai.streamText, ai.generateObject, and the embedding calls, with model, usage, and finish-reason attributes.

Per-call settings

telemetry: sets a function id and metadata per call, turns collection off for one call, and allow-lists which context keys reach spans. Nothing from runtimeContext or toolsContext is recorded unless you name it:

telemetry: TelemetrySettings(
  functionID: "summarize",
  metadata: ["team": "ios"],
  includeRuntimeContext: ["tenant"],     // "secret" stays out of spans
  includeToolsContext: ["weather"]
)

On this page