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

# Errors, memory and threads

> What each HTTP and gRPC error means, and how much memory and how many threads the server uses.

## What an error means

Every error of the API is `{"error": "message"}`. The message never quotes the request: a prompt or an email in a filter does not come back, also not in logs. It names the place and the kind of problem, and a short hash of what it cut out.

| HTTP | gRPC | meaning | what to do |
| - | - | - | - |
| `400` | `INVALID_ARGUMENT`, `INTERNAL` | a bad request: unknown field, wrong type, a filter deeper than 32 levels, a body that is not OTLP | fix the request; do not retry |
| `401` | `UNAUTHENTICATED` | no API key, or a key that is not in the file | send `authorization: Bearer <key>` |
| `403` | `PERMISSION_DENIED` | the key has no right for this route; another tenant's header; a request from a page of another site (`CROSS_SITE`); a host name that is not allowed (`HOST_NOT_ALLOWED`) | use the right key; see `--allowed-host` |
| `404` | | no such trace, span or session; a tenant with no store yet | |
| `405` | `FAILED_PRECONDITION` | a write to a read-only server (S3 reader); a wrong method on a path of the API (the `Allow` header names the right one) | write to the writer; use the method of the route |
| `408` | | the body did not arrive within `--body-timeout` (60 s); OTLP over HTTP gets `503` with `Retry-After` instead, gRPC `DEADLINE_EXCEEDED` | |
| `413` | `RESOURCE_EXHAUSTED` | too large: the body (64 MiB), the spans of one request (100,000), one list (10,000 items), or the memory of the decode | send smaller batches |
| `415` | | a write without `Content-Type: application/json`, or an unknown OTLP content type | set the header |
| `429` | `UNAVAILABLE` | 16 writes run and 64 wait: the server is busy (`Retry-After: 1`). Over HTTP the server still reads the refused body to its end (at most `--max-body-bytes`, within `--body-timeout`) and drops it, so a client that sends its whole body before it reads gets the `429`, not a broken connection | retry later (OTLP SDKs do) |
| `500` | | a bug | report it ([Security](https://docs.magpiedb.com/security.md) if it is a security problem) |
| `503` | `UNAVAILABLE` | the store cannot write now (disk full, a tenant limit, the thread limit: `THREADS`), a slow OTLP body, or an S3 writer that was fenced. Over HTTP with `Retry-After: 1` | retry later (OTLP SDKs do) |

Each limit is a flag: [Server reference, "Run it" and "Limits"](https://docs.magpiedb.com/reference/server.md). In the library (Rust, Node, Python) an error is an exception with the full message.

## Memory and threads

| | measured | source |
| - | - | - |
| threads of one open store | 13 and one more for each CPU up to 8: 21 with 8 or more CPUs, 17 with 4. Before any write and after a commit, whatever the options | `crates/server/tests/threads.rs` (it fails when the number changes); [`docs/FINDINGS.md`](https://github.com/intojhanurag/magpie/blob/main/docs/FINDINGS.md) F25, F100 |
| memory of one open store, no data | about 7 MB | F25, `results/tenants-*.json` |
| memory of one open store after small writes | 11 to 25 MB | F25, `results/tenants-*.json` |
| the most open stores in one process on a Mac | 292 with no reserve (the 293rd failed: macOS stops a process at 6,144 threads). The server keeps 512 threads in reserve there: about 266 (8 or more CPUs) | F25; F100 |
| the server, 16 OTLP writes at once, each an SDK's full batch (512 spans of 50 KB, 26 MB) | 1.0 to 1.3 GB peak (HTTP JSON, gRPC, HTTP protobuf) | F99, `results/anu87-sdk-batch/` |
| the server, 16 OTLP writes at once at the body limit (64 MiB) | 2.0 GB (JSON) to 2.7 GB (protobuf) peak | F99 |

### Write memory

* The write path of one store: a writer heap of 256 MB and a write queue of 256 MB. The store keeps its write memory to about 2 to 3 times the queue plus the heap. That is a rule of the code, not a measured peak.
* A tenant store gets 32 MB + 32 MB, so about 100 to 130 MB at most while it takes writes (by the same rule; not measured). Keep `--max-open-tenants` under the thread limit of your system (next section).
* Merges in the background use memory that these limits do not cover.
* The memory of the requests in flight has its own budget: `--ingest-memory-mb` (512) for the decodes of writes. The bodies (at most 16 at once, each at most `--max-body-bytes`) and the mapped spans are not in it: 16 full SDK batches of 50 KB model calls at once take about 1.3 GB, and 16 bodies of 64 MiB about 2.7 GB (F99).
* Queries have no memory budget yet ([Limits](https://docs.magpiedb.com/limits.md#server)).
* Memory of an import: `--memory-mb`, with measured peaks in [Server reference, "Import old traces"](https://docs.magpiedb.com/reference/server.md).

> [!NOTE]
>
> On a small machine (or a container with 1 GB of memory), lower the SDK's batch (`OTEL_BSP_MAX_EXPORT_BATCH_SIZE`, for example 128) together with `--max-body-bytes` and `--max-mapped-bytes`. A `--max-body-bytes` below the SDK's full batch refuses that batch with `413`, and the SDK does not send it again.

### Threads, with tenants

One open store holds 13 threads and one more for each CPU up to 8 (21 on a machine with 8 or more CPUs, 17 with 4). Count `--max-open-tenants` x that number, plus the server's own (about 40 when idle; up to 512 more while many requests wait for a store), plus the reserve.

The limit is `kern.num_taskthreads` on macOS (6,144, per process) and on Linux the pids limit of the container (`pids.max` of its own cgroup: Docker `--pids-limit`; it counts the threads of every process in the container), `ulimit -u` and `/proc/sys/kernel/threads-max`. For the default of 100 open stores set the pids limit to 4,096 or more (100 x 21 + 512 + 512 = 3,124), or leave it unlimited.

> [!WARNING]
>
> A pids limit of a Kubernetes POD (`podPidsLimit`) is not seen: on cgroup v2 a container sees only its own cgroup, and the limit of the pod is in the cgroup above it. The server then writes no warning and makes no check before an open; it still answers `503` `THREADS` when an open gets no thread, and opens a failed store again. With a limit of the pod, set `--max-open-tenants` by hand: threads of one store x stores + the server's own threads (about 40, and up to 512 more under load) must stay under the limit.

### What the server does at the limit

At its start it writes a `WARNING` line when `--max-open-tenants` does not fit the limit it reads, with the number of stores that fit. Before it opens a tenant's store it reads the limit again: with no room for the threads of one store and the reserve (an eighth of the limit, 16 to 512), it closes the store used longest ago first; while every open store is in use the request gets `503` with a text that starts with `THREADS:` and `Retry-After: 1`, and no folder is made for a new tenant. The reserve is there so that the commits of the open stores keep their threads.

**If a commit gets no thread all the same** (another process of the container took them): that store stops taking writes (`503`), and the next request for its tenant opens it again, as a restart does. A query waits up to 1 s for the other requests of the tenant to end; a write never waits (it gets `503` at once while another request holds the store). The writes that were answered OK before are in the write log and are shown after that open. Until then, queries of that tenant answer from its last commit. A stop of the server opens such a store once more and closes it: the exit code is 0 when that works; when not, a log line names the tenant, and the next start applies its write log.


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