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

# Questions about whole traces

> Find traces by their spans, root, scores and totals, and read long lists in pages with cursors.

`POST /v1/traces/find` returns traces, each with its totals. `count` is every match, not only the ones in the page of results you get back. The examples use the [sample data](https://docs.magpiedb.com/queries/overview.md#the-sample-data).

| field | matches the trace when |
| - | - |
| `anySpan: [filter]` | for each filter in the list, some span of the trace matches it |
| `root: filter` | the root span matches |
| `anyScore: [scoreFilter]` | for each entry, some score of the trace matches |
| `filter: traceFilter` | the trace matches: `traceId`, `userId`, `sessionId`, `name` (of the root), `hasError`, `from`, `to`, `tag`, `service`, `environment`, ranges of totals, and `and`, `or`, `not` |

**Two conditions on two different spans of one trace:** a failed tool call, and a model call of `gpt-x`.

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"anySpan": [{"kind": "tool_call", "hasError": true}, {"kind": "model_generation", "model": "gpt-x"}], "limit": 10}'
```

```json theme={null}
{"count":1,"traces":[{"traceId":"t1","rootSpanId":"r1","startedAt":1000,"endedAt":5000,"name":"chat","userId":"u1","sessionId":"s1","spanCount":3,"errorCount":1,"inputTokens":100,"outputTokens":50,"costNanos":1500000}],"nextCursor":null}
```

**The root span.** In `t2` only a child span has "refund", so `t2` is not found:

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"root": {"text": {"words": "refund"}}}'
```

```json theme={null}
{"count":2,"traces":[{"traceId":"t3","rootSpanId":"r3","startedAt":3000,"endedAt":3200,"name":null,"userId":"u1","sessionId":null,"spanCount":2,"errorCount":0,"inputTokens":0,"outputTokens":0,"costNanos":0},{"traceId":"t1","rootSpanId":"r1","startedAt":1000,"endedAt":5000,"name":"chat","userId":"u1","sessionId":"s1","spanCount":3,"errorCount":1,"inputTokens":100,"outputTokens":50,"costNanos":1500000}],"nextCursor":null}
```

**Ranges of totals.** `spanCount`, `errorCount`, `inputTokens`, `outputTokens`, `totalTokens`, `costNanos` and `durationMs`, each `{"min": a, "max": b}` (both ends are in the range; leave one out for no end). Cost is in nano-dollars: one dollar is `{"costNanos": {"min": 1000000000}}`. Traces that cost more than \$0.0001:

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"filter": {"costNanos": {"min": 100001}}}'
```

```json theme={null}
{"count":1,"traces":[{"traceId":"t1","rootSpanId":"r1","startedAt":1000,"endedAt":5000,"name":"chat","userId":"u1","sessionId":"s1","spanCount":3,"errorCount":1,"inputTokens":100,"outputTokens":50,"costNanos":1500000}],"nextCursor":null}
```

**OR on traces:** failed, or 1 to 15 tokens in total.

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"filter": {"or": [{"hasError": true}, {"totalTokens": {"min": 1, "max": 15}}]}}'
```

```json theme={null}
{"count":2,"traces":[{"traceId":"t2","rootSpanId":"r2","startedAt":2000,"endedAt":3000,"name":null,"userId":"u2","sessionId":null,"spanCount":2,"errorCount":0,"inputTokens":10,"outputTokens":5,"costNanos":100000},{"traceId":"t1","rootSpanId":"r1","startedAt":1000,"endedAt":5000,"name":"chat","userId":"u1","sessionId":"s1","spanCount":3,"errorCount":1,"inputTokens":100,"outputTokens":50,"costNanos":1500000}],"nextCursor":null}
```

**NOT on traces:** traces with no tool call at all.

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"filter": {"not": {"anySpan": [{"kind": "tool_call"}]}}}'
```

```json theme={null}
{"count":1,"traces":[{"traceId":"t2","rootSpanId":"r2","startedAt":2000,"endedAt":3000,"name":null,"userId":"u2","sessionId":null,"spanCount":2,"errorCount":0,"inputTokens":10,"outputTokens":5,"costNanos":100000}],"nextCursor":null}
```

**Previews.** `previewChars` adds the first characters of the trace's input and output to each row. The input is the root's; the output is of the span that ended last and has one. The trace list is oldest first here (`sort: "oldest"`):

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"filter": {"userId": "u1"}, "sort": "oldest", "previewChars": 10}'
```

```json theme={null}
{"count":2,"traces":[{"traceId":"t1","rootSpanId":"r1","startedAt":1000,"endedAt":5000,"name":"chat","userId":"u1","sessionId":"s1","spanCount":3,"errorCount":1,"inputTokens":100,"outputTokens":50,"costNanos":1500000,"inputPreview":"please ref","outputPreview":"refund pol"},{"traceId":"t3","rootSpanId":"r3","startedAt":3000,"endedAt":3200,"name":null,"userId":"u1","sessionId":null,"spanCount":2,"errorCount":0,"inputTokens":0,"outputTokens":0,"costNanos":0,"inputPreview":"refund","outputPreview":""}],"nextCursor":null}
```

**Only the count:** `"limit": 0`.

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"limit": 0}'
```

```json theme={null}
{"count":3,"traces":[],"nextCursor":null}
```

## Pages and cursors

A list returns `limit` rows (default 50, at most 10,000) and a `nextCursor`. Give it back as `cursor` for the next page. On the last page `nextCursor` is `null`.

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"limit": 2}'
```

```json theme={null}
{"count":3,"traces":[{"traceId":"t3","rootSpanId":"r3","startedAt":3000,"endedAt":3200,"name":null,"userId":"u1","sessionId":null,"spanCount":2,"errorCount":0,"inputTokens":0,"outputTokens":0,"costNanos":0},{"traceId":"t2","rootSpanId":"r2","startedAt":2000,"endedAt":3000,"name":null,"userId":"u2","sessionId":null,"spanCount":2,"errorCount":0,"inputTokens":10,"outputTokens":5,"costNanos":100000}],"nextCursor":"eyJsaXN0IjoidHJhY2VzIiwic29ydCI6Im5ld2VzdCIsInRpbWUiOjIwMDAsImlkIjoidDIiLCJpZDIiOiIiLCJuIjoxfQ"}
```

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"limit": 2, "cursor": "eyJsaXN0IjoidHJhY2VzIiwic29ydCI6Im5ld2VzdCIsInRpbWUiOjIwMDAsImlkIjoidDIiLCJpZDIiOiIiLCJuIjoxfQ"}'
```

```json theme={null}
{"count":3,"traces":[{"traceId":"t1","rootSpanId":"r1","startedAt":1000,"endedAt":5000,"name":"chat","userId":"u1","sessionId":"s1","spanCount":3,"errorCount":1,"inputTokens":100,"outputTokens":50,"costNanos":1500000}],"nextCursor":null}
```

* A cursor is a place in the sort order, not a saved result. Each page costs about as much as page 1 (there is no offset).
* While writes go on, a row is never returned twice and never skipped, as long as its sort key does not change. A new row that sorts before the cursor is not returned.
* A cursor works only with the same list and the same `sort`.

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


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