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

# Sessions and scores

> Find sessions (conversations) and write, read and add up scores for evals and feedback.

## Sessions

A session is a conversation: the traces with one `sessionId`. Newest first, with sums and the first input and last output:

```sh theme={null}
curl -s localhost:4318/v1/sessions/find -d '{}'
```

```json theme={null}
{"count":1,"sessions":[{"sessionId":"s1","userId":"u1","startedAt":1000,"endedAt":5000,"traceCount":1,"spanCount":3,"errorCount":1,"inputTokens":100,"outputTokens":50,"costNanos":1500000,"firstTraceId":"t1","lastTraceId":"t1","firstInput":"please refund my order","lastOutput":"refund policy applies"}],"nextCursor":null}
```

The filter of `/v1/sessions/find` takes `userId`, `from` and `to` (the start of the session). One session with its traces, oldest first:

```sh theme={null}
curl -s localhost:4318/v1/sessions/s1
```

```json theme={null}
{"session":{"sessionId":"s1","userId":"u1","startedAt":1000,"endedAt":5000,"traceCount":1,"spanCount":3,"errorCount":1,"inputTokens":100,"outputTokens":50,"costNanos":1500000,"firstTraceId":"t1","lastTraceId":"t1","firstInput":"please refund my order","lastOutput":"refund policy applies"},"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}]}
```

## Scores (evals and feedback)

A score is a number, a yes/no or a label on a trace, a span or a session. It can come long after the trace: from a person, an LLM judge or a test.

Write one (`at`, the time of the score, is the time the server gets it when you leave it out):

```sh theme={null}
curl -s localhost:4318/v1/scores -H 'content-type: application/json' -d '{"scores": [{"traceId": "t3", "name": "quality", "value": 0.9, "source": "llm-judge", "at": 4000}]}'
```

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

The scores of a trace, by time:

```sh theme={null}
curl -s localhost:4318/v1/traces/t1/scores
```

```json theme={null}
{"scores":[{"traceId":"t1","spanId":"m1","name":"hallucination","value":1.0,"label":null,"source":null,"comment":null,"at":2},{"traceId":"t1","spanId":null,"name":"quality","value":0.4,"label":null,"source":null,"comment":null,"at":3}]}
```

Traces by their scores (`anyScore` takes `name`, `min`, `max`, `label`, `source`):

```sh theme={null}
curl -s localhost:4318/v1/traces/find -d '{"anyScore": [{"name": "quality", "label": "fail", "source": "human"}]}'
```

```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}
```

**Sums of scores.** The average and the count per label of the scores with one name, per group. Per model of the trace:

```sh theme={null}
curl -s localhost:4318/v1/scores/aggregate -d '{"score": {"name": "tone"}, "groupBy": {"traceField": "model"}}'
```

```json theme={null}
{"rows":[{"avg":null,"count":2,"key":null,"labels":{"polite":1,"rude":1}},{"avg":null,"count":1,"key":"gpt-y","labels":{"polite":1}}]}
```

The average of a yes/no score is the share of yes (here 3 of 5):

```sh theme={null}
curl -s localhost:4318/v1/scores/aggregate -d '{"score": {"name": "helpful"}}'
```

```json theme={null}
{"rows":[{"key":null,"count":5,"avg":0.6,"labels":{}}]}
```

The average per day (of the score's own time):

```sh theme={null}
curl -s localhost:4318/v1/scores/aggregate -d '{"score": {"name": "depth"}, "groupBy": {"time": {"bucketMs": 86400000}}}'
```

```json theme={null}
{"rows":[{"key":0,"count":1,"avg":0.5,"labels":{}},{"key":86400000,"count":2,"avg":0.5,"labels":{}}]}
```

To compare two experiments, run the same query twice with a `traces` filter, for example `"traces": {"root": {"attributes": {"experiment": "A"}}}` and then `"B"`. All fields: [the server reference, "Query API"](https://docs.magpiedb.com/reference/http-api.md#query-api) (the item "Scores for evals").


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