> ## 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.
> Packages and server binaries are not published yet: build from source, as the quick start shows. Check the status note on the introduction page before you suggest `cargo add`, `npm install` or `pip install`.
> Only one process may write to a store folder at a time. For many apps, send traces to one `magpie-server`.

# Add up numbers

> Exact sums per group, top N, time series and percentiles with POST /v1/aggregate.

`POST /v1/aggregate`: the count of spans, and exact sums, per group.

* `metrics`: `inputTokens`, `outputTokens`, `totalTokens`, `costNanos`, `durationMs`, `errorCount`, `cacheReadTokens`, `cacheWriteTokens`, `reasoningTokens`, `inputCostNanos`, `outputCostNanos`, `timeToFirstTokenMs`. `sums` come in the order of `metrics`.
* `groupBy`: `"all"` (the default), `{"field": "model"}` (also `traceId`, `kind`, `userId`, `sessionId`, `name`, `tag`), `{"attribute": "user.plan"}`, or `{"time": {"bucketMs": 3600000}}`.
* `filter`: a span filter, as in [Filter spans](https://docs.magpiedb.com/queries/filters.md).

One group, every metric:

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"groupBy": "all", "metrics": ["inputTokens", "outputTokens", "totalTokens", "costNanos", "durationMs"]}'
```

```json theme={null}
{"rows":[{"key":null,"count":7,"sums":[110,55,165,1600000,6300]}]}
```

Tokens per model. Spans with no model have the key `null`:

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"groupBy": {"field": "model"}, "metrics": ["totalTokens"]}'
```

```json theme={null}
{"rows":[{"key":null,"count":5,"sums":[0]},{"key":"gpt-x","count":1,"sums":[150]},{"key":"gpt-y","count":1,"sums":[15]}]}
```

**Top N.** The user with the highest cost. `sort` by a sum (`{"sum": 0}` is the first metric), by `count`, or by a percentile; `"order": "asc"` turns it around:

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"groupBy": {"field": "userId"}, "metrics": ["costNanos"], "sort": {"by": {"sum": 0}}, "limit": 1}'
```

```json theme={null}
{"rows":[{"key":"u1","count":5,"sums":[1500000]}]}
```

**By an attribute.** The key keeps its type (`"1"`, `1` and `true` are three groups):

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"groupBy": {"attribute": "user.plan"}, "metrics": ["durationMs"]}'
```

```json theme={null}
{"rows":[{"key":null,"count":6,"sums":[6100]},{"key":"pro","count":1,"sums":[200]}]}
```

## Time series

`{"time": {"bucketMs": ...}}` groups by time. Buckets start at the Unix epoch, so a bucket of a day is a UTC day. Only buckets with a span come back: a chart fills the others with zero. Root spans per second:

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"filter": {"rootOnly": true}, "groupBy": {"time": {"bucketMs": 1000}}}'
```

```json theme={null}
{"rows":[{"key":1000,"count":1,"sums":[]},{"key":2000,"count":1,"sums":[]},{"key":3000,"count":1,"sums":[]}]}
```

**A series per value:** add `thenBy` (a field or an attribute). `key` is the bucket and `key2` the value. Cost per model per second:

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"groupBy": {"time": {"bucketMs": 1000}}, "thenBy": {"field": "model"}, "metrics": ["costNanos"]}'
```

```json theme={null}
{"rows":[{"key":1000,"key2":null,"count":2,"sums":[0]},{"key":1000,"key2":"gpt-x","count":1,"sums":[1500000]},{"key":2000,"key2":null,"count":1,"sums":[0]},{"key":2000,"key2":"gpt-y","count":1,"sums":[100000]},{"key":3000,"key2":null,"count":2,"sums":[0]}]}
```

With `thenBy`, `sort` and `limit` choose whole series: `"sort": {"by": {"sum": 0}}, "limit": 10` gives the 10 values with the highest sum over all buckets, each with every bucket.

## Percentiles

`percentiles: [{"p": 95}]` is the 95th percentile of the duration. `of` picks another value (`costNanos`, `totalTokens`, `inputTokens`, `outputTokens`, `timeToFirstTokenMs`, ...). Exact by default (the nearest-rank rule). Spans with no value do not count: a span that has not ended has no duration.

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"percentiles": [{"p": 0}, {"p": 33.3333}, {"p": 50}, {"p": 95}, {"p": 100}]}'
```

```json theme={null}
{"rows":[{"key":null,"count":7,"sums":[],"percentiles":[100,200,500,4000,4000]}]}
```

`"approx": true` uses a sketch: at most 1% off, and small memory for any number of spans.

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"percentiles": [{"p": 50, "approx": true}, {"p": 99.9, "approx": true}]}'
```

```json theme={null}
{"rows":[{"key":null,"count":7,"sums":[],"percentiles":[500,4000]}]}
```

Percentiles of cost and of tokens, per kind. `null`: no span of the group has a cost.

```sh theme={null}
curl -s localhost:4318/v1/aggregate -d '{"groupBy": {"field": "kind"}, "metrics": ["costNanos"], "percentiles": [{"p": 50, "of": "costNanos"}, {"p": 100, "of": "totalTokens"}, {"p": 50}]}'
```

```json theme={null}
{"rows":[{"key":"agent_run","count":3,"sums":[0],"percentiles":[null,0,1000]},{"key":"model_generation","count":2,"sums":[1600000],"percentiles":[100000,150,500]},{"key":"tool_call","count":2,"sums":[0],"percentiles":[null,0,100]}]}
```

How fast a sum is: a sum with a filter on `kind`, `model` and time only, grouped by kind, model or whole hours, reads saved sums per hour. Every other sum visits each matching span ([limits](https://docs.magpiedb.com/limits.md#queries)).


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