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

# Queries

> Every kind of question you can ask Magpie, with a real answer for each.

This section shows every kind of question you can ask Magpie, with a real answer for each.

* Every query is JSON. There is no query language as text: a saved view or an alert keeps the JSON.
* The server, the Node package and the Python package take the same JSON. The examples here use the server and `curl`. The [library calls](https://docs.magpiedb.com/queries/library.md) have their own page.
* The examples come from [`testdata/query-examples.json`](https://github.com/intojhanurag/magpie/blob/main/testdata/query-examples.json). The server, Node and Python tests run every example in that file and check each answer. The answers in these pages were made again on 2026-12-04 by the commands on the pages.

## The sample data

The sample has 3 traces with 7 spans, and 15 scores. Times are small numbers (milliseconds after 1970), so the answers are easy to read.

| trace | spans | what is in it |
| - | - | - |
| `t1` | `r1` agent run, `m1` model call (`gpt-x`, 150 tokens, \$0.0015), `tl1` tool call that failed (`timeout`) | user `u1`, session `s1`, tag `beta`, service `api`, environment `prod`; `tl1` has the attributes `{"user": {"plan": "pro"}, "retries": 2}` |
| `t2` | `r2` agent run, `m2` model call (`gpt-y`, 15 tokens, \$0.0001) | user `u2` |
| `t3` | `r3` agent run (not ended), `tl3` tool call | user `u1` |

The texts: `r1` "please refund my order" / "refund policy applies"; `m1` "what is the refund policy" / "the refund policy is thirty days"; `m2` "policy refund"; `r3` "refund"; `tl3` "refund lookup"; `r2` "hello world"; `tl1` "lookup order" and a JSON text with Thai in it.

Load it into a new store and start a server on it. You need `jq` here, and `magpie-server` on your `PATH` (see the [quickstart](https://docs.magpiedb.com/quickstart.md)):

```sh theme={null}
jq -c '.spans[], .scores[]' "$REPO/testdata/query-examples.json" > sample.ndjson
magpie-server import --dir ./sample --format magpie sample.ndjson > /dev/null 2>&1
magpie-server --dir ./sample
```

Keep the server open. Run the queries in the pages of this section in a second terminal.

## Routes

| route | body | answer |
| - | - | - |
| `POST /v1/search` | `{filter, sort, limit, cursor, previewChars}` | spans: `{"spans": [...], "nextCursor": c}` |
| `POST /v1/count` | `{filter}` | `{"count": n}` |
| `POST /v1/traces/find` | `{filter, root, anySpan, anyScore, sort, limit, cursor, previewChars}` | traces: `{"count": n, "traces": [...], "nextCursor": c}` |
| `POST /v1/aggregate` | `{filter, groupBy, thenBy, metrics, percentiles, sort, limit}` | `{"rows": [...]}` |
| `GET /v1/traces/:id` | | every span of a trace, with its text |
| `GET /v1/traces/:id/spans?limit=&cursor=` | | the spans of a trace without their text, in pages |
| `GET /v1/traces/:id/spans/:spanId` | | one span |
| `POST /v1/sessions/find`, `GET /v1/sessions/:id` | | sessions |
| `POST /v1/scores`, `GET /v1/traces/:id/scores`, `POST /v1/scores/aggregate` | | scores |
| `POST /v1/export`, `POST /v1/export/scores` | `{filter}`, `{from, to}` | JSON Lines |

The full reference of every route and field: [the server reference, "Query API"](https://docs.magpiedb.com/reference/http-api.md#query-api).

## Where to go next

> [card] [Find text](https://docs.magpiedb.com/queries/text-search.md)
>
> Words, phrases, pieces of words, prefixes and error text.

> [card] [Filter spans](https://docs.magpiedb.com/queries/filters.md)
>
> Fields, attributes, time ranges, and AND, OR, NOT.

> [card] [Whole traces and pages](https://docs.magpiedb.com/queries/traces.md)
>
> Questions about whole traces, previews, and cursors.

> [card] [Add up numbers](https://docs.magpiedb.com/queries/sums.md)
>
> Exact sums, top N, time series and percentiles.

> [card] [Sessions and scores](https://docs.magpiedb.com/queries/sessions-scores.md)
>
> Conversations, evals and feedback.

> [card] [Read and export](https://docs.magpiedb.com/queries/read-export.md)
>
> Read one trace, one span, or export spans as JSON Lines.

> [card] [The same queries in the library](https://docs.magpiedb.com/queries/library.md)
>
> The Node and Python calls for each route.

## When a query is wrong

A query with an unknown field, or a value of the wrong type, gets `400`. It is never read as "match everything". The message gives the place and what was expected. It never quotes the request (a prompt can be private), so the server cuts the value out and gives a short hash and a length:

```sh theme={null}
curl -s -w ' %{http_code}\n' localhost:4318/v1/count -d '{"filter": {"text": "refund"}}'
```

```text theme={null}
{"error":"bad request body: an unknown name, expected one of `words`, `phrase`, `contains`, `prefix` at line 1 column 28 (detail sha256=..., 101 bytes)"} 400
```

The library (Rust, Node, Python) gives the full message, with the value: there the caller has the data.


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