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

# Find traces

> POST /v1/traces/find: whole traces that match conditions on the trace, its root, its spans and its scores, with totals and previews.

`POST /v1/traces/find` returns traces, each with its totals, in pages. Right: `read`.

```sh Request 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 200 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}
```

## Body

All set conditions must hold.

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

A [trace filter](https://docs.magpiedb.com/reference/api/filters.md#trace-filter): fields of the whole trace, ranges of its totals, and `and`, `or`, `not`.

**root** `object`

A [span filter](https://docs.magpiedb.com/reference/api/filters.md#span-filter) that the root span meets.

**anySpan** `object[]`

[Span filters](https://docs.magpiedb.com/reference/api/filters.md#span-filter). For each entry, some span of the trace meets it. Different spans may meet different entries.

**anyScore** `object[]`

[Score filters](https://docs.magpiedb.com/reference/api/filters.md#score-filter). For each entry, some score of the trace meets it.

**sort** `string` (default: newest)

`"newest"` or `"oldest"`, by the start of the trace. Ties: `traceId`.

**limit** `integer` (default: 50)

At most 10,000. `0` returns only the count.

**cursor** `string`

The `nextCursor` of the page before ([Lists and cursors](https://docs.magpiedb.com/reference/api/pagination.md)).

**previewChars** `integer`

Adds `inputPreview` and `outputPreview` to each trace: the first `previewChars` characters of the input and output of the root span.

**where the preview comes from**

* A root with no input gives the input of the earliest span that has one (by start, then span id).
* A root with no output gives the output of the span that ended last among those that have one (ties: earlier start, then smaller span id).
* The summary of a trace keeps the first 200 characters of both texts. Up to 200, the list reads no stored span. Above 200, it reads the stored span of each text that is longer.

`root`, `anySpan` and `anyScore` mean the same as the fields of the trace filter with these names. Use them here for a query with no `or` and no `not`.

## Answer

**count** `integer`

Every matching trace, not only the ones of this page.

**traces** `object[]`

The traces of this page.

**trace**

**traceId** `string`

**rootSpanId** `string`

The earliest span with no parent, or the earliest span when every span has a parent.

**startedAt** `integer`

Start of the earliest span, Unix ms.

**endedAt** `integer | null`

End of the latest span. `null` while no span has ended.

**name** `string | null`

The name of the root span.

**userId** `string | null`

The user of the root span, or else of the earliest span that has one.

**sessionId** `string | null`

The session of the root span, or else of the earliest span that has one.

**spanCount** `integer`

**errorCount** `integer`

Failed spans.

**inputTokens** `integer`

**outputTokens** `integer`

**costNanos** `integer`

Cost in nano-dollars.

**cacheReadTokens** `integer`

Summed over the spans, as are `cacheWriteTokens`, `reasoningTokens`, `inputCostNanos` and `outputCostNanos`. Each is a part of `inputTokens`, `outputTokens` or `costNanos`. Left out when 0.

**timeToFirstTokenMs** `integer`

Ms from the start of the trace to the earliest first token of any of its spans. Left out when no span has one.

**inputPreview** `string`

Only with `previewChars`. So is `outputPreview`.

**nextCursor** `string | null`

The `cursor` of the next page. `null` on the last page.

> [card] [Questions about whole traces](https://docs.magpiedb.com/queries/traces.md)
>
> A guide with real trace queries and answers.

> [card] [Read a trace](https://docs.magpiedb.com/reference/api/read-trace.md)
>
> Every span of one trace.


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