AI SDK calls in Tracewell

The AI SDK gives you token usage and a finish reason on every result, whichever provider you call. That makes it a convenient single place to log traces for Tracewell, even when you use several model providers.

Field mapping

How AI SDK's data maps to the kit's Trace type. Your backend does this mapping and serves the result to the kit. The kit itself doesn't call AI SDK. Check field names against AI SDK's documentation for the version you use.

AI SDK fieldKit fieldNote
result.usage.inputTokensTrace.inputTokensTotal across steps for multi-step calls
result.usage.outputTokensTrace.outputTokens
model id you passed inTrace.model
result.finishReasonTrace.statuserror → error; length or content-filter → warning; stop or tool-calls → success
your timer, or the onFinish callbackTrace.latencyMs
tokens × your price per tokenTrace.costPrices differ per provider; keep a table on the server

Two ways to collect

  1. Log from callbacks. After generateText or in the finish callback of streamText, write a trace record with usage, finish reason, model, latency, feature and user.
  2. Use OpenTelemetry. Turn on the SDK's telemetry so each call becomes spans, collect them with your OpenTelemetry pipeline, and map them as described in the OpenTelemetry guide.

The first is simpler for one app. The second scales better when you want every service traced the same way.

Questions

Can I use the SDK's built-in telemetry instead?

Yes. The AI SDK can emit OpenTelemetry spans. Send them to your collector and map them as in the OpenTelemetry guide.

Does this work with streaming?

Yes. Log the trace from the streaming result's finish callback, where usage is available.

Does Tracewell need the AI SDK?

No. Tracewell is a front end that reads your API; the AI SDK is one convenient way to collect the data.