> ## 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).

# Trace stats

> POST /v1/traces/stats: the numbers of the viewer's summary strip for the traces of a trace query: failures, durations, tokens, cost and a time chart.

`POST /v1/traces/stats` gives the numbers of the viewer's summary strip for the traces that match a trace query. It reads at most the newest 100,000 matching traces. Right: `read`.

```sh Request theme={null}
curl -s localhost:4318/v1/traces/stats -d '{"query": {"filter": {"userId": "u1"}}, "bars": 4}'
```

```json 200 theme={null}
{"count":2,"scanned":2,"complete":true,"failed":1,"ended":2,"durationP50Ms":200,"durationP95Ms":4000,"inputTokens":100,"outputTokens":50,"costNanos":1500000,"bucketMs":1000,"from":1000,"to":3001,"buckets":[{"at":1000,"count":1,"failed":1},{"at":2000,"count":0,"failed":0},{"at":3000,"count":1,"failed":0}]}
```

## Body

**query** `object` (default: \{})

The conditions of [`/v1/traces/find`](https://docs.magpiedb.com/reference/api/traces-find): `filter`, `root`, `anySpan`, `anyScore`. Its `limit`, `cursor`, `sort` and `previewChars` are not used.

**bars** `integer` (default: 48)

The most buckets of the time chart, from 2 to 200 (else `400`).

## Answer

**count** `integer | null`

The number of matching traces when `complete` is `true`. Else `null`: counting the rest would be the work that the cap avoids.

**scanned** `integer`

The traces that the numbers below come from: the newest matching traces, at most 100,000.

**complete** `boolean`

`true` when the server read every matching trace.

**failed** `integer`

Read traces with a failed span.

**ended** `integer`

Read traces that have an end.

**durationP50Ms** `integer | null`

The median duration (end minus start of the trace), nearest rank over the ended traces. `durationP95Ms` is the 95th percentile. `null` when no trace has ended.

**inputTokens** `integer`

Sum over the read traces. So are `outputTokens` and `costNanos`.

**bucketMs** `integer | null`

The width of a bucket: the smallest of 1 s, 5 s, 15 s, 30 s, 1, 2, 5, 10, 15, 30 min, 1, 2, 3, 6, 12 h, 1, 2, 7, 14, 30, 90 days, or whole years, that gives at most `bars` buckets. Buckets are aligned to the Unix epoch.

**from** `integer | null`

The start of the window: `filter.from` of the query, else the oldest read trace.

**to** `integer | null`

The end of the window: `filter.to` of the query. With a `from` and no `to`, it is now (the server's clock). With neither, it is just after the newest read trace.

**buckets** `object[]`

Trace starts per bucket: every bucket from the one of `from` to the one of `to - 1`, the empty ones too, oldest first. No window (no trace and no `from`): no buckets.

**bucket**

**at** `integer`

The start of the bucket, ms.

**count** `integer`

Traces that start in the bucket.

**failed** `integer`

Of those, traces with a failed span.

> [accordion] How the server keeps the work small
>
> The server reads the traces newest first, in time windows that double, from one minute back from the newest trace of the store (or from the query's `to` when that is earlier). In each window, it gives every span condition the window's start as its lowest start: no answer changes. The engine's joins and counts cover only the windows read.
>
> With more matches than the cap, the server reads the time from the newest trace back to at most twice the time that holds the newest 100,000 matches, whatever the server's clock. Older matches cost nothing.

> [card] [Find traces](https://docs.magpiedb.com/reference/api/traces-find.md)
>
> The query that this route sums.

> [card] [Viewer](https://docs.magpiedb.com/reference/viewer.md)
>
> The web viewer that shows these numbers.


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