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

# Sessions

> POST /v1/sessions/find and GET /v1/sessions/:id: conversations (the traces of one session id) with their sums, first input and last output.

A session is a conversation: the traces with one `sessionId`. The session of a trace is the `sessionId` of its root span, or else of its earliest span that has one. Right: `read`.

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

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

## Find sessions

`POST /v1/sessions/find` returns sessions in pages, newest first.

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

**properties**

**userId** `string`

The user of the session: the user of its earliest trace that has one.

**from** `integer`

The session started at or after this time (ms).

**to** `integer`

The session started before this time (ms).

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

`"newest"` or `"oldest"`, by the start of the session. Ties: the session id.

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

**count** `integer`

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

**sessions** `object[]`

**session**

**sessionId** `string`

**userId** `string | null`

The user of the earliest trace that has one.

**startedAt** `integer`

The start of the first trace, Unix ms.

**endedAt** `integer | null`

The latest end of a trace. `null` while no span has ended.

**traceCount** `integer`

**spanCount** `integer`

Sum over the traces of the session, as are `errorCount`, `inputTokens`, `outputTokens` and `costNanos`.

**cacheReadTokens** `integer`

Summed over the traces, as are `cacheWriteTokens`, `reasoningTokens`, `inputCostNanos` and `outputCostNanos`. Left out when 0.

**firstTraceId** `string`

The first trace: the earliest start, ties by trace id.

**lastTraceId** `string`

The last trace: the latest start, ties by trace id (the smaller one).

**firstInput** `string`

The first 200 characters of the input of the first trace.

**lastOutput** `string`

The first 200 characters of the output of the last trace.

**nextCursor** `string | null`

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

`firstInput` and `lastOutput` follow the preview rule of [trace lists](https://docs.magpiedb.com/reference/api/traces-find.md): the text of the root span, or else of another span of the trace.

## Get one session

`GET /v1/sessions/:id` returns `{"session": session, "traces": [trace]}`: the session row, and every trace of the session, oldest first (by start, then trace id). No trace has this session id: `404`.

**id** `string` *required*

The session id.

The traces have the fields of a [trace](https://docs.magpiedb.com/reference/api/traces-find.md#answer). For a very large session, page [`/v1/traces/find`](https://docs.magpiedb.com/reference/api/traces-find) with `filter.sessionId` instead.

The scores on a session itself: [`GET /v1/sessions/:id/scores`](https://docs.magpiedb.com/reference/api/scores#get-the-scores-on-a-session).

> [card] [Sessions and scores](https://docs.magpiedb.com/queries/sessions-scores.md)
>
> A guide with real sessions and scores.

> [card] [Scores](https://docs.magpiedb.com/reference/api/scores.md)
>
> Write and read scores on a session.


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