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

# Filter spans

> Match spans by field, time, tag and attribute, and join filters with and, or and not.

A filter is a set of fields. Every field that you set must hold on the span. The examples use the [sample data](https://docs.magpiedb.com/queries/overview.md#the-sample-data).

| field | matches |
| - | - |
| `text`, `errorText` | text (see [Find text](https://docs.magpiedb.com/queries/text-search.md)) |
| `traceId`, `kind`, `model`, `userId`, `sessionId`, `name`, `service`, `environment`, `tag` | the exact value |
| `hasError` | `true`: status `error`, or an error message |
| `from`, `to` | the start time in ms: `from <= startedAt < to` |
| `rootOnly` | `true`: spans with no parent |
| `attributes` | exact values at attribute paths |
| `and`, `or`, `not` | filters inside a filter (below) |

Text, kind and time together:

```sh theme={null}
curl -s localhost:4318/v1/count -d '{"filter": {"text": {"words": "refund"}, "kind": "tool_call", "from": 3000}}'
```

```json theme={null}
{"count":1}
```

A time range. `to` is not in the range:

```sh theme={null}
curl -s localhost:4318/v1/count -d '{"filter": {"from": 2000, "to": 3000}}'
```

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

Tag, service and environment:

```sh theme={null}
curl -s localhost:4318/v1/count -d '{"filter": {"tag": "beta", "service": "api", "environment": "prod"}}'
```

```json theme={null}
{"count":1}
```

## Attributes

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

```sh theme={null}
curl -s localhost:4318/v1/count -d '{"filter": {"attributes": {"user.plan": "pro", "retries": 2}}}'
```

```json theme={null}
{"count":1}
```

## AND, OR, NOT

`and: [filter]`: every one holds. `or: [filter]`: at least one holds. `not: filter`: it does not hold. They sit next to the other fields.

Tool calls, or spans of the model `gpt-y`:

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

```json theme={null}
{"count":3}
```

Spans that do not have the model `gpt-x`. A span with no model counts too: it does not have that value.

```sh theme={null}
curl -s localhost:4318/v1/count -d '{"filter": {"not": {"model": "gpt-x"}}}'
```

```json theme={null}
{"count":6}
```

Agent runs (of user `u2`, or with the word "refund") that are not named "chat":

```sh theme={null}
curl -s localhost:4318/v1/count -d '{"filter": {"kind": "agent_run", "or": [{"userId": "u2"}, {"text": {"words": "refund"}}], "not": {"name": "chat"}}}'
```

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

Nested: (a tool call or a model call) and not (failed, or of the model `gpt-y`):

```sh 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 theme={null}
{"count":2}
```

A filter can be at most 32 levels deep. Each `and`, `or` and `not` is one level.

> [card] [Questions about whole traces](https://docs.magpiedb.com/queries/traces.md)
>
> Match a trace by its spans, its root, its scores and its totals.


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