Skip to main content
A span is one step of an agent run: the run itself, a model call, a tool call. Every span needs traceId, spanId, kind and startedAt. The example uses an open store (open a store).

Write a span when it starts and when it ends

Both return the number of spans. A batch with one invalid span is refused whole.

When a span can be found

A span is in the write log when write returns. Queries find it after the next commit: call commit(), or wait for the timer (commitInterval, 500 ms by default). commit() returns the operation stamp of the commit. What a write survives depends on the option durability (options).

Span fields

Node.js and Python use these camelCase names. Rust uses the same names in snake_case (trace_id), and Span::new(trace_id, span_id, kind, started_at) sets the four that every span needs. Never add a part to its whole: that is the rule of the OpenTelemetry GenAI conventions and of OpenAI’s numbers. Anthropic reports input tokens without the cached ones: add input_tokens, cache_read_input_tokens and cache_creation_input_tokens. Google reports output tokens without the thinking tokens: add them. A span with a part above its whole is refused. An answer leaves out the usage fields that are 0 or not set. Read them with a default: span.cacheReadTokens ?? 0 in Node.js, span.get("cacheReadTokens", 0) in Python.

Compute cost from a price table

With this table, a my-model span with 1,000,000 input tokens and 1,000 output tokens gets cost 3.015 and costComputed: true.
  • Prices are US dollars per one million tokens, by the exact model name, with at most 6 decimals. cacheRead and cacheWrite default to the input price.
  • write and upsert compute the cost of a span that has a model of the table, tokens, and no cost: inputCost, outputCost and cost, their sum.
  • The cost is computed once, when the span is written. A new table changes new spans only.
  • A span that comes with a cost, or a part of it, keeps it. A model that is not in the table gets no cost, and so does a span with no tokens.
  • No price list comes with the package: take the numbers from the price page of your provider. The table is the same as the file of magpie-server --prices.

Mask private data

write, upsert and writeScores replace every match before the write goes into the write log. No file of the store holds the original text, no search finds it, and no read returns it. Masking is off by default. The same policy is the file of magpie-server --mask. What each built-in rule does not find: masking reference.
write does not look for an older version, so a span id written twice is stored twice. Lists of rows (search, traceSpans, the export) give every copy; copies with one start come in no promised order.Where an answer takes one copy, it takes the least by a fixed copy order, not the one that came first. The order: the span’s fields (root or not, kind, name, model, userId, sessionId, service, environment, error, endedAt, the token counts, the costs, the time to first token, whether it has an input and an output; not set comes first), then the first 200 characters of input and of output, then its line of the export. This holds for the summary of a trace, for getSpan (of the copies with the latest start) and for getTrace (copies with one start). A preview of more than 200 characters comes from the copy whose text starts with the 200 characters that the summary keeps.To replace a span, use upsert.

Query from your app

Search text, find traces, add up tokens and cost.

Sessions and scores

Score traces, spans and sessions.

Errors and input rules

Unknown fields, depth, numbers, a full disk.