Claude API calls as Tracewell traces

The Anthropic Messages API returns a usage object with input and output tokens, plus cache read and write counts. Here's how those fields map onto Tracewell's traces.

Field mapping

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

Anthropic API fieldKit fieldNote
message.idTrace.idOr your own request id
message.modelTrace.model
usage.input_tokensTrace.inputTokensUncached input tokens
usage.cache_read_input_tokens, usage.cache_creation_input_tokensTrace.inputTokens (or new fields)Priced differently from regular input; store separately for accurate cost
usage.output_tokensTrace.outputTokens
message.stop_reasonTrace.statusmax_tokens or refusal → warning; end_turn or tool_use → success
your timer around the callTrace.latencyMs
tokens × your price per tokenTrace.costCalculate on your server

What to record per call

Model, the four token counts, stop reason, latency, error if any, and the feature and user from your own request context. With prompt caching, cache reads are much cheaper than regular input, so cost per trace is only accurate if you store them separately.

Agents and tool use

An agent loop makes several Messages calls with tool calls in between. Log the whole loop as one trace, each model call as an llm span and each tool call as a tool span, and the trace explorer shows the loop step by step.

Questions

Does the API return cost?

No. Calculate it from the token counts and the model's price, including the separate rates for cache reads and writes.

How do I log streamed responses?

The final message_delta and message_stop events carry the usage. Log the trace when the stream ends.

Can tool use be shown?

Yes. Log each tool call as a tool span under the trace so the waterfall shows model and tool steps in order.