> ## Documentation Index
> Fetch the complete documentation index at: https://docs.magpiedb.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Magpie is a trace store for AI agents, used as a library (crate `magpiedb`, npm `magpiedb`, PyPI `magpiedb`) or as `magpie-server`, which takes OpenTelemetry (OTLP over HTTP on 127.0.0.1:4318 and gRPC on 127.0.0.1:4317 by default).
> Use only the commands, flags, routes and JSON shapes that these pages show. Do not guess a flag or a query field: if a page does not show it, say that the docs do not show it.
> Install the library with `cargo add magpiedb`, `npm install magpiedb` or `pip install magpiedb`. Get the server from the GitHub Releases download or the Docker image `ghcr.io/intojhanurag/magpie-server`, as the quick start shows.
> Only one process may write to a store folder at a time. For many apps, send traces to one `magpie-server`.
> `magpie-server doctor` checks a running server. An AI agent can query the traces over MCP at `/mcp` on the HTTP port (page guides/mcp).

# Read a trace

> GET /v1/traces/:id, /v1/traces/:id/spans and /v1/traces/:id/spans/:spanId: every span of a trace, the spans without text in pages, or one span.

Three routes read one trace. Right: `read`. A trace of 100,000 spans is hundreds of MB with its text: for such a trace, list its spans in pages without text, then get the text of one span.

```sh Request theme={null}
curl -s 'localhost:4318/v1/traces/t1/spans?limit=2'
```

```json 200 theme={null}
{"count":3,"spans":[{"spanId":"r1","parentSpanId":null,"name":"chat","kind":"agent_run","model":null,"startedAt":1000,"endedAt":5000,"hasError":false,"inputTokens":0,"outputTokens":0,"cost":null},{"spanId":"m1","parentSpanId":"r1","name":"","kind":"model_generation","model":"gpt-x","startedAt":1100,"endedAt":1600,"hasError":false,"inputTokens":100,"outputTokens":50,"cost":0.0015}],"nextCursor":"eyJsaXN0IjoidHJhY2Ugc3BhbnMiLCJzb3J0Ijoib2xkZXN0IiwidGltZSI6MTEwMCwiaWQiOiJtMSIsImlkMiI6IiIsIm4iOjF9"}
```

## Get every span of a trace

`GET /v1/traces/:id` returns `{"spans": [span]}`: every span with its text, by `startedAt`, then `spanId`. A trace with no spans: `404`.

**id** `string` *required*

The trace id.

**spans** `object[]`

**span**

**traceId** `string`

**spanId** `string`

**parentSpanId** `string | null`

`null`: the root span of the trace.

**name** `string`

**kind** `string`

`agent_run`, `model_generation`, `tool_call`, ... Free text.

**model** `string | null`

**userId** `string | null`

**sessionId** `string | null`

**service** `string | null`

**environment** `string | null`

**tags** `string[]`

**startedAt** `integer`

Unix ms.

**endedAt** `integer | null`

`null`: the span is still running.

**inputTokens** `integer`

**outputTokens** `integer`

**cost** `number | null`

US dollars. Stored as whole nano-dollars, so values round to 9 decimals.

**status** `string`

`"unset"`, `"ok"` or `"error"`.

**error** `string | null`

The error message. A span failed when `status` is `"error"` or `error` is set.

**input** `string`

**output** `string`

**attributes** `object | null`

A JSON object. You can filter each leaf value by exact match.

**events** `array | null`

Span events (exceptions, logs). Stored, not indexed.

**cacheReadTokens** `integer`

Input tokens read from the provider's cache: a part of `inputTokens`. Left out when 0, as are `cacheWriteTokens` and `reasoningTokens` (a part of `outputTokens`).

**timeToFirstTokenMs** `integer`

Ms from `startedAt` to the first piece of the answer. Left out when not known.

**inputCost** `number`

The part of `cost` for input tokens, US dollars. `outputCost` is the part for output tokens. Left out when not set.

**costComputed** `boolean`

`true`: the server computed the cost from a price table. Left out when `false`.

## List the spans of a trace in pages

`GET /v1/traces/:id/spans?limit=n&cursor=c` returns `{"count", "spans", "nextCursor"}`: the spans of the trace without their text, oldest first. `count` is every span of the trace. A trace that is not there gives an empty list with `count` 0.

**id** `string` *required*

The trace id.

**limit** `integer` (default: 50)

At most 10,000. `0` returns only the count. Another query parameter than `limit` or `cursor` gets `400`.

**cursor** `string`

The `nextCursor` of the page before, as it is: it needs no escaping.

**spans** `object[]`

**light span**

`spanId`, `parentSpanId`, `name`, `kind`, `model`, `startedAt`, `endedAt`, `inputTokens`, `outputTokens`, `cost`, and `hasError` (`status` is `error`, or `error` is set). `cacheReadTokens`, `cacheWriteTokens`, `reasoningTokens`, `timeToFirstTokenMs`, `inputCost` and `outputCost` come when set. The names and values are the same as in the span. There is no `input`, `output`, `attributes` or `events`.

Each page reads the time of every span of the trace once, as every list by cursor does: for a whole trace, use a large `limit`. All fields come from columns except `parentSpanId`, which the server reads from the stored span of each row that has a parent.

## Get one span

`GET /v1/traces/:id/spans/:spanId` returns one [span](#get-every-span-of-a-trace) with every field. No such span: `404`.

**spanId** `string` *required*

The span id.

A client that does not use `upsert` (OTLP always does) can write a span twice with the same ids. This route then returns the copy that started last.

> [card] [Read and export](https://docs.magpiedb.com/queries/read-export.md)
>
> A guide with real answers.

> [card] [Scores of a trace](https://docs.magpiedb.com/reference/api/scores.md#get-the-scores-of-a-trace)
>
> The evals and feedback on a trace.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.