OpenAI API calls as Tracewell traces

Every OpenAI API response includes a usage object with token counts. Log it with the model, timing and outcome and you have everything Tracewell's traces, cost and models screens show.

Field mapping

How OpenAI API'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 OpenAI API. Check field names against OpenAI API's documentation for the version you use.

OpenAI API fieldKit fieldNote
response.idTrace.idOr your own request id
response.modelTrace.modelThe exact model snapshot that served the call
usage.input_tokens (Responses) / usage.prompt_tokens (Chat Completions)Trace.inputTokens
usage.output_tokens / usage.completion_tokensTrace.outputTokensIncludes reasoning tokens for reasoning models
your timer around the callTrace.latencyMsThe API doesn't return total latency
HTTP status or error objectTrace.statusErrors and timeouts → error; refusals or truncation → warning
tokens × your price per tokenTrace.costCalculate on your server
your app contextTrace.feature, Trace.userPass these through from the request

A minimal logging wrapper

Wrap your OpenAI calls in one function on your server that starts a timer, makes the call, reads usage, and writes a trace record with the model, tokens, latency, status, cost, feature and user. Tracewell's screens then read those records from your API.

Detailed token fields

The usage object can also break tokens down, such as cached input tokens and reasoning output tokens. If your pricing depends on them, store them too and add columns to the traces table.

Multi-step calls

When one user request makes several model and tool calls, log each as a span under one trace, so the trace explorer shows the whole chain. See the OpenTelemetry guide.

Questions

Does the API return cost?

No. It returns token counts. Multiply by the model's current price, and keep prices in your backend so you can update them.

What about streaming?

Request usage with the final streamed event, or read it from the completed response, then log the trace once the stream ends.

Should I log prompts and outputs?

Only with your users' consent and your retention rules in place. Tracewell can show them in the trace explorer if you do.