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

# Filters

> Every field of the span filter, the text search, the trace filter and the score filter of the magpie-server query API.

A filter is JSON. Every field that you set must hold. A filter with no field matches everything. There is no query language as text: a saved view or an alert keeps this JSON.

| Filter | Matches | Used by |
| - | - | - |
| [Span filter](#span-filter) | one span | `/v1/search`, `/v1/count`, `/v1/aggregate`, `/v1/export`, `root` and `anySpan` of a trace query |
| [Trace filter](#trace-filter) | one whole trace | `/v1/traces/find`, `/v1/traces/stats`, `traces` of `/v1/scores/aggregate`, `/v1/traces/delete` |
| [Score filter](#score-filter) | one score | `anyScore` of a trace query, `score` of `/v1/scores/aggregate` |

```sh Request theme={null}
curl -s localhost:4318/v1/count -d '{"filter": {"and": [{"or": [{"kind": "tool_call"}, {"kind": "model_generation"}]}, {"not": {"or": [{"hasError": true}, {"model": "gpt-y"}]}}]}}'
```

```json 200 theme={null}
{"count":2}
```

## Span filter

All set fields must hold on one span.

**text** `object`

Text in the span's `input` or `output`. See [Text](#text).

**errorText** `object`

Text in the span's `error` message, by the rules of [Text](#text). `text` does not look there.

**traceId** `string`

Exact value. The same holds for `kind`, `model`, `userId`, `sessionId`, `name`, `service` and `environment`.

**tag** `string`

The span has this tag.

**hasError** `boolean`

`true`: the span failed (`status` is `error`, or `error` is set). `false`: it did not fail.

**from** `integer`

Start time in ms: `from <= startedAt`.

**to** `integer`

Start time in ms: `startedAt < to`.

**rootOnly** `boolean` (default: false)

Only spans with no parent.

**attributes** `object | array`

Exact values at attribute paths: `{"path": value}` or `[["path", value]]`. A value is a string, a number or a boolean.

* A dot is a step of the path. So `{"user.plan": "pro"}` matches `{"user": {"plan": "pro"}}` and also the flat OpenTelemetry key `"user.plan"`.
* Resource attributes are under `resource`: `"resource.host.name"`.

**and** `object[]`

Every filter in the list holds. Two conditions on one field: `{"and": [{"tag": "a"}, {"tag": "b"}]}`.

**or** `object[]`

At least one filter in the list holds. An empty list matches nothing.

**not** `object`

This filter does not hold. A span with no value for a field does not have that value: `{"not": {"model": "x"}}` also matches the spans with no model.

`and`, `or` and `not` hold filters of the same shape, next to the other fields. `{"kind": "tool_call", "or": [{"name": "a"}, {"name": "b"}], "not": {"hasError": true}}` is a tool call named a or b that did not fail. Depth limit: [32 levels](https://docs.magpiedb.com/reference/api/errors.md#how-deep-can-a-filter-be).

## Text

`text` and `errorText` take an object with exactly one of these fields. Upper and lower case are the same.

**words** `string`

Every part between spaces, each in input or in output, in any order. Characters with no space between them are found together, in their order: `e-mail` is `e` next to `mail`, and `user@example.com` is that address. A run of Thai, Korean or Chinese characters is that run.

**phrase** `string`

The words next to each other, in this order.

**contains** `string`

A piece inside a word, as you type it for SQL `LIKE '%...%'`. One piece is found anywhere in a word: `und` finds `refund`, `funding` and `REFUNDED`.

* With a space or a sign, the first piece is the end of a word, the last piece is the start of a word, and the pieces between are whole words, all next to each other. `fund pol` finds `refund policy`.
* One character of Thai or Korean finds the runs that have it.

**prefix** `string`

Whole words, and the last one may go on. `refund` finds `refunded`, and `refund pol` finds `refund policy`.

> [accordion] What text search does not find
>
> * It is not a byte-for-byte `LIKE`. Signs and spaces are not in the index, so `a-b`, `a b` and `a,b` are the same.
> * A word of 256 bytes or more stays in the stored text, but no text search finds it.
> * `text` does not search the `error` message: use `errorText`.

> [accordion] Limits and cost of text search
>
> * The cost of `contains` and `prefix` follows the number of different words in the store, not the amount of text. A piece of one or two letters matches most words, and is slow.
> * A text of several pieces whose pieces match more than 16,384 different words in one segment gets `400`, with a message that says so. Make the first or the last piece longer.
> * A phrase of 4,096 positions or more gets `400`. A position is a word, or a word too long to index (256 bytes or more) between two words.
> * A phrase with 64 or more words too long to index in a row, between two of its words, gets `400`. 63 are searched.
> * `phrase`, `contains` and `prefix` are one phrase each. In `words`, each part between spaces is a phrase of its own, so `words` takes any number of words.

## Trace filter

All set fields must hold on one trace.

**traceId** `string`

Exact value.

**userId** `string`

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

**sessionId** `string`

The session of the trace.

**name** `string`

The name of the root span.

**hasError** `boolean`

`true`: the trace has a failed span. `false`: it has none.

**from** `integer`

The trace started at or after this time (ms). A trace starts when its earliest span starts.

**to** `integer`

The trace started before this time (ms).

**tag** `string`

A span of the trace has this tag. The same holds for `service` and `environment`.

**costNanos** `object`

A range of a total of the trace: `{"min": a, "max": b}`, so `a <= value <= b`. Leave out an end for no end. `min` above `max` matches no trace. Cost is in nano-dollars: one dollar is `{"costNanos": {"min": 1000000000}}`.

**every total that takes a range**

| Field | Total |
| - | - |
| `spanCount` | spans |
| `errorCount` | failed spans |
| `inputTokens` | input tokens |
| `outputTokens` | output tokens |
| `totalTokens` | input plus output tokens |
| `costNanos` | cost, nano-dollars |
| `durationMs` | end of the latest span minus start of the earliest, ms |

A trace with no ended span has no duration, and is in no `durationMs` range. So `{"durationMs": {}}` matches the traces that have an ended span.

**root** `object`

A [span filter](#span-filter) that the root span meets. The root (`rootSpanId`) is the earliest span with no parent, or the earliest span when every span has a parent.

**anySpan** `object[]`

[Span filters](#span-filter). For each entry, some span of the trace meets it. Different spans may meet different entries. `rootOnly` in an entry asks for some span with no parent; `root` asks for the one root.

**anyScore** `object[]`

[Score filters](#score-filter). For each entry, some score of the trace (on the trace or on one of its spans) meets it.

**and** `object[]`

Trace filters. `and`, `or` and `not` work as in the span filter. Traces that failed or cost a dollar, and have no tool call: `{"or": [{"hasError": true}, {"costNanos": {"min": 1000000000}}], "not": {"anySpan": [{"kind": "tool_call"}]}}`.

> [accordion] What a trace filter costs
>
> * One index lookup each: `traceId`, `userId`, `sessionId`, `name`, `hasError`, `from`, `to`, `tag`, `service`, `environment`, `root` with only a `name`, and an `anySpan` entry with only `kind`, `model`, `name`, `tag`, `service` or `environment`. The same holds for `kind` with `model`, `name` or `hasError: true`, and for an `or` of such entries.
> * A range of a total and a `not` read one column of the trace summaries.
> * Any other `anySpan` or `root`, and every `anyScore`, first finds the matching spans or scores, then their traces.
> * `and`, `or` and `not` cost what their parts cost.

## Score filter

All set fields must hold on one score.

**name** `string` *required*

The name of the score.

**min** `number`

`value >= min`.

**max** `number`

`value <= max`.

**label** `string`

Exact value. The same holds for `source`, `authorId` and `evalId`.

**from** `integer`

The time of the score (`at`), not of its trace: `from <= at`, in ms.

**to** `integer`

`at < to`, in ms.

> [card] [Filter spans](https://docs.magpiedb.com/queries/filters.md)
>
> A guide with real filters and answers.

> [card] [Find traces](https://docs.magpiedb.com/reference/api/traces-find.md)
>
> The route that takes a trace filter.


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