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

# Aggregate scores

> POST /v1/scores/aggregate: the count, average, percentiles and label counts of the scores with one name, per time bucket, trace field or root attribute.

`POST /v1/scores/aggregate` sums the scores with one name, per group. Right: `read`.

```sh Request theme={null}
curl -s localhost:4318/v1/scores/aggregate -d '{"score": {"name": "tone"}, "groupBy": {"traceField": "model"}}'
```

```json 200 theme={null}
{"rows":[{"avg":null,"count":2,"key":null,"labels":{"polite":1,"rude":1}},{"avg":null,"count":1,"key":"gpt-y","labels":{"polite":1}}]}
```

## Body

**score** `object` *required*

A [score filter](https://docs.magpiedb.com/reference/api/filters.md#score-filter). `name` is required. `from` and `to` are on the score's own time `at`.

**traces** `object`

A [trace filter](https://docs.magpiedb.com/reference/api/filters.md#trace-filter). Only the scores of the traces that meet it count (scores on such a trace and on its spans). A score with no trace in the store does not count then.

**groupBy** `string | object` (default: all)

**values**

| Value | One group per |
| - | - |
| `"all"` | nothing: one group |
| `{"time": {"bucketMs": 86400000}}` | bucket of the score's own time `at` |
| `{"traceField": f}` | value of a field of the trace: `model`, `userId`, `sessionId`, `name`, `tag`, `environment` or `service` |
| `{"rootAttribute": "prompt.version"}` | value of an attribute of the root span of the trace |

**thenBy** `object`

A second key (a trace field or a root attribute), for a series per value. `groupBy` must be a time bucket: else an error. `key` is the bucket, `key2` the value.

**percentiles** `number[]` (default: \[])

Percentiles of the value, for example `[50, 95]`. Each from 0 to 100, at most 4 decimals. Exact, by the nearest-rank rule.

**sort** `object`

`{"by": "count" | "avg", "order": "desc" | "asc"}`. `order` defaults to `"desc"`. Ties by key; rows with no average last. Left out: rows by key.

**limit** `integer`

Keep this many rows, after the sort. Left out: every row.

With `thenBy`, `sort` and `limit` choose whole series, as in [`/v1/aggregate`](https://docs.magpiedb.com/reference/api/aggregate).

## Answer

**rows** `object[]`

**row**

**key** `string | number | boolean | null`

The value of the group, or the bucket start (ms). `null` for `"all"`, or for scores with no value for the key.

**key2** `string | number | boolean | null`

The value of `thenBy`. Only with `thenBy`.

**count** `integer`

Every score of the group, of every type.

**avg** `number | null`

The average of the numbers and booleans. Of a boolean: the share of true. `null` for a group with only categories.

**percentiles** `(number | null)[]`

One value per percentile, of the same scores as `avg`. Only when the query asks for percentiles.

**labels** `object`

The count of scores per label.

## How the trace side works

* The server joins the trace when the query runs. It copies nothing onto a score when the score is written. So a group comes from the trace as it is now.
* A trace with two models is in both groups.
* A score with no trace in the store (on a session, or before its trace came) has the key `null`, and does not count with `traces`.
* The query reads the columns of the matching scores, one trace summary per trace of those scores, and for `rootAttribute` one span per trace.
* To compare two groups (two experiments), run the same query twice: with `"traces": {"root": {"attributes": {"experiment": "A"}}}`, then with `"B"`.

> [card] [Scores](https://docs.magpiedb.com/reference/api/scores.md)
>
> Write and read scores.

> [card] [Sessions and scores](https://docs.magpiedb.com/queries/sessions-scores.md)
>
> A guide with real sums of scores.


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