Metadata-Version: 2.5
Name: agentflight-sdk
Version: 0.3.0
Summary: Black box recorder for AI agents: record, replay and debug agent runs
Project-URL: Homepage, https://github.com/shupandee/agentflight
Project-URL: Repository, https://github.com/shupandee/agentflight
Project-URL: Issues, https://github.com/shupandee/agentflight/issues
Project-URL: Changelog, https://github.com/shupandee/agentflight/blob/main/CHANGELOG.md
Author: Deepanshu Gautam
License-Expression: MIT
License-File: LICENSE
Keywords: agents,llm,observability,opentelemetry,replay,tracing
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Debuggers
Classifier: Topic :: System :: Logging
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: httpx>=0.27
Requires-Dist: opentelemetry-api>=1.24
Requires-Dist: opentelemetry-sdk>=1.24
Provides-Extra: dev
Requires-Dist: alembic>=1.13; extra == 'dev'
Requires-Dist: fastapi>=0.110; extra == 'dev'
Requires-Dist: jinja2>=3.1; extra == 'dev'
Requires-Dist: langgraph>=1.0; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: openai>=1.0; extra == 'dev'
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.24; extra == 'dev'
Requires-Dist: opentelemetry-proto>=1.24; extra == 'dev'
Requires-Dist: psycopg[binary]>=3.1; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Requires-Dist: sqlalchemy>=2.0; extra == 'dev'
Requires-Dist: uvicorn>=0.29; extra == 'dev'
Provides-Extra: langgraph
Requires-Dist: langgraph>=1.0; extra == 'langgraph'
Provides-Extra: openai
Requires-Dist: openai>=1.0; extra == 'openai'
Provides-Extra: server
Requires-Dist: alembic>=1.13; extra == 'server'
Requires-Dist: fastapi>=0.110; extra == 'server'
Requires-Dist: jinja2>=3.1; extra == 'server'
Requires-Dist: opentelemetry-proto>=1.24; extra == 'server'
Requires-Dist: psycopg[binary]>=3.1; extra == 'server'
Requires-Dist: sqlalchemy>=2.0; extra == 'server'
Requires-Dist: uvicorn>=0.29; extra == 'server'
Description-Content-Type: text/markdown

# AgentFlight

A black box recorder for AI agents. AgentFlight records what an agent does during a run:
model calls, tool executions, decisions, state changes, token spend and failures. Every run
is stored as an ordered timeline you can query, replay and fork.

> Status: early development (week 5, viewer). APIs may change; see [CHANGELOG.md](https://github.com/shupandee/agentflight/blob/main/CHANGELOG.md).

## AgentFlight, Langfuse, LangSmith or Phoenix?

[Langfuse](https://langfuse.com/docs), [LangSmith](https://docs.langchain.com/langsmith/home)
and [Arize Phoenix](https://arize.com/docs/phoenix) are full LLM engineering platforms.
Besides tracing they offer evaluations, datasets and experiments, prompt management and
dashboards, with many framework and provider integrations. Langfuse and Phoenix can be
self-hosted and accept OpenTelemetry traces. LangSmith runs as a LangChain cloud service,
with self-hosted and bring-your-own-cloud options on its Enterprise plan.

AgentFlight is much smaller: a Python recorder, one server on Postgres (or SQLite) and a
read-only viewer, with no evaluations, prompt management or dashboards. Its focus is
re-running your own agent code from a recording. [Replay](#replay-and-fork) serves the
recorded model and tool outputs back to the same code in your process, offline, and fork
swaps one step and runs live from there. That's different from re-running a single LLM
call in a playground (Phoenix's span replay, for example, replays LLM calls with different
inputs). Choose one of the platforms if you need evaluations, prompt management or team
features; AgentFlight if you want a small self-hosted recorder with step-by-step replay
of agent runs.

## Architecture

```mermaid
flowchart LR
    subgraph Agent process
        A[Your agent code] -->|run / llm_call / @tool / decision / state_change| SDK[agentflight SDK]
        SDK --> OTel[OpenTelemetry spans<br/>private TracerProvider]
        OTel --> EXP[AgentFlightExporter<br/>batched HTTP JSON]
    end
    EXP -->|POST /v1/spans| API[FastAPI ingest server]
    O[Any OpenTelemetry agent<br/>or Collector] -->|POST /v1/traces<br/>OTLP/HTTP| API
    API --> DB[(PostgreSQL<br/>runs + events)]
    C[curl / scripts] -->|GET /v1/runs, /v1/runs/:id| API
    V[Browser] -->|/ui viewer| API
```

- **One run = one OpenTelemetry trace.** `agentflight.run(...)` opens the root span; its
  trace id is the run id. Everything recorded inside it is a child span.
- **Each span carries `agentflight.*` attributes** (`kind`, model, messages, tokens, tool
  args/result, error type...). Structured values are stored as JSON.
- The SDK uses its **own TracerProvider**, so it doesn't change any global OTel setup your
  app already has.
- The server **upserts by span id** (re-sent batches are idempotent) and accepts child spans
  before their root span arrives (the run shows as `running` until then; see
  [Run status](#run-status)).
- **Run totals are stored on the run row** (calls, errors, tokens, cost) and recomputed in
  SQL for every run an ingest batch touches, so listing runs never reads their events and
  re-sent spans can't make the totals drift.

## Install

The package on PyPI is **`agentflight-sdk`**; the import name is still **`agentflight`**.

```bash
pip install agentflight-sdk                # the SDK: import agentflight
pip install "agentflight-sdk[server]"      # plus the server and viewer (agentflight-server)
pip install "agentflight-sdk[openai]"      # plus the OpenAI-style client integration
pip install "agentflight-sdk[langgraph]"   # plus the LangGraph integration
```

From a checkout, `pip install -e ".[server]"` (or `.[dev]`) installs the same package.

## Quickstart

```bash
# 0. A key for the server and the SDK (never commit it)
export AGENTFLIGHT_API_KEY=$(openssl rand -hex 32)

# 1. Server + Postgres + retention job (applies Alembic migrations on start)
docker compose up --build

# or without Docker, using SQLite (from a checkout; elsewhere,
# `pip install "agentflight-sdk[server]"`):
pip install -e ".[server]"
agentflight-server migrate      # uses AGENTFLIGHT_DATABASE_URL, default sqlite:///./agentflight.db
agentflight-server serve        # http://127.0.0.1:8000

# 2. Record the toy agent (the SDK reads AGENTFLIGHT_API_KEY)
pip install -e .                # elsewhere: pip install agentflight-sdk
                                # (the package is agentflight-sdk; you import agentflight)
python examples/toy_agent.py

# 3. Inspect: open http://localhost:8000/ui and sign in with the key, or use the API
curl -H "Authorization: Bearer $AGENTFLIGHT_API_KEY" localhost:8000/v1/runs
curl -H "Authorization: Bearer $AGENTFLIGHT_API_KEY" localhost:8000/v1/runs/<run_id>
```

## Viewer

The server includes a read-only viewer at `/ui`:

- **Runs:** every run with its status, start time, duration, call counts, failures, tokens
  and cost, filtered by status, name, model, tool, errors, metadata (`key:value`), start
  time, parent run, and a toggle that hides sub-agent runs (`has_parent=false`). Filters update the table in place. A run whose recorder dropped spans is marked
  "incomplete recording: N spans dropped" (see "Dropped spans are counted" below).
- **Run page:** the run's spans as an indented tree (each span under its parent,
  siblings in start order) with a bar per span on the run's time axis, failures
  highlighted with their messages, and replay/fork runs linked to the run they came
  from. A span whose parent never arrived starts its own subtree at the left. Events load
  in pages of 100 without payloads, and the next page continues the tree where the last
  one stopped; selecting an event loads its messages, outputs, arguments and stack traces
  on demand. An incomplete recording gets a warning above the timeline. (To page in tree order the server reads every span's id and parent for each
  page, which is cheap next to loading the events themselves.) A sub-agent run links to
  the run that started it; a parent lists its sub-agent runs and marks the span each one
  started from (see [Sub-agent runs](#sub-agent-runs)).

![Run list with filters](https://raw.githubusercontent.com/shupandee/agentflight/main/docs/screenshots/run-list.png)

![Run page: span tree, timeline bars, a failed tool call, and an event's payloads](https://raw.githubusercontent.com/shupandee/agentflight/main/docs/screenshots/run-page.png)

![A LangGraph run: model and tool calls nested under graph nodes](https://raw.githubusercontent.com/shupandee/agentflight/main/docs/screenshots/span-tree.png)

![A parent run listing its sub-agent runs, each marked on the span that started it](https://raw.githubusercontent.com/shupandee/agentflight/main/docs/screenshots/sub-agent-parent.png)

![A sub-agent run whose parent was never recorded](https://raw.githubusercontent.com/shupandee/agentflight/main/docs/screenshots/sub-agent-missing-parent.png)

![A fork run, linked to its source run, with cached, overridden and live steps](https://raw.githubusercontent.com/shupandee/agentflight/main/docs/screenshots/fork-run.png)

The screenshots come from the browser smoke test against the example agents; regenerate
them with `AGENTFLIGHT_SCREENSHOT_DIR=docs/screenshots pytest tests/test_viewer_browser.py`.

It is server-rendered (Jinja2 and [htmx](https://htmx.org), vendored, no build step) and
reads through the same service functions and JSON shapes as the `/v1` API, so another
frontend can be built on the API alone. Everything a run recorded is treated as untrusted:
templates autoescape everything and nothing is marked safe, and pages are served with a
Content-Security-Policy that allows no inline script or style (`script-src 'self'`). See
[Viewer sign-in](#viewer-sign-in) for how sessions work.

## Recording API

```python
import agentflight

agentflight.configure("http://localhost:8000")


@agentflight.tool  # records name, bound args, result or exception
def search(query: str) -> list[str]: ...


with agentflight.run("support-agent", metadata={"ticket": 42}) as run:
    with agentflight.llm_call("gpt-4o-mini", messages) as call:
        resp = client.chat(...)  # your model client
        call.record(resp.text, tokens_in=..., tokens_out=..., cost_usd=...)  # cost optional
    agentflight.decision("next_step", "search", reason="needs docs")
    agentflight.state_change("plan", new_plan, old=old_plan)
    try:
        search("refund policy")
    except Exception as exc:
        agentflight.error(exc)  # handled error: recorded, run continues

agentflight.flush()  # before exit, so batched spans are sent
```

- Latency is the span duration (`duration_ms` in the API).
- An exception escaping `run`, `llm_call` or a `@tool` marks that span (and the run) as
  `error` and is re-raised unchanged.
- `@agentflight.tool` works on sync and async functions; `@agentflight.tool(name="...")`
  overrides the name.
- If the server is unreachable, export fails with a logged warning; your agent keeps running.

## Integrations

Optional extras; the core SDK depends on neither package.

### OpenAI-style chat clients

```bash
pip install "agentflight-sdk[openai]"   # import name: agentflight
```

```python
from openai import OpenAI
from agentflight.integrations.openai import instrument_openai

client = instrument_openai(OpenAI())  # same client, now recorded
with agentflight.run("support-agent"):
    resp = client.chat.completions.create(model="gpt-4o-mini", messages=msgs, tools=tools)
```

- Each `chat.completions.create` call is an `llm` span with the model, messages, request
  params (`tools`, `temperature`, ...), the full response (tool calls included) and
  `tokens_in`/`tokens_out` from `response.usage`.
- Works with `OpenAI` and `AsyncOpenAI`, and with any client that has the same
  `client.chat.completions.create(model=..., messages=...)` shape. `openai` itself is never
  imported by AgentFlight.
- Cost isn't guessed. There's no built-in price table, so pass
  `instrument_openai(client, cost=lambda model, tokens_in, tokens_out: ...)` if you want it.
- `extra_headers`, `extra_query`, `extra_body` and `timeout` are passed to the client but
  never recorded (headers can carry credentials). Everything recorded goes through your
  `redact` hook.
- Fail-open: the call happens exactly once with your arguments and its response or
  exception comes back unchanged. If response parsing or your `cost` function fails, that's
  logged and ignored. A client without the expected shape is returned untouched.
- **Not recorded yet:** `stream=True` calls (passed straight through) and the Responses API.

### LangGraph

```bash
pip install "agentflight-sdk[langgraph]"   # import name: agentflight
```

```python
from agentflight.integrations.langgraph import AgentFlightCallbackHandler

handler = AgentFlightCallbackHandler(run_name="react-agent")
graph.invoke(inputs, config={"callbacks": [handler]})
print(handler.run_id)
```

- The graph becomes a `run`. Inside an `agentflight.run(...)` it's a `node` span in that run
  instead. Each executed node is a `node` span with `agentflight.node.name`, `.step`,
  `.input` (state in) and `.output` (state update).
- Chat model and LLM calls inside nodes are `llm` spans (messages, params, response, token
  usage from `usage_metadata` or `token_usage`). LangChain tools are `tool` spans. So
  `create_react_agent`-style graphs record without any SDK calls in your code.
- LangChain's own runnables inside a node (prompts, sequences, routers) aren't recorded.
  Their children attach to the nearest recorded span.
- Errors are recorded where they happen and share one failure id up the graph. LangGraph
  interrupts aren't errors: the node's output records `{"interrupted": ...}`.
- Params whose names contain `api_key`, `secret`, `password` or `authorization` are dropped.
- Fail-open: every callback is guarded, so a broken recorder never changes the graph's
  result or exception.
- **Limitation:** spans you record yourself inside a node (`@agentflight.tool`,
  `llm_call`) attach to the surrounding `agentflight.run`, not to the node span, and are
  only part of a run if one is open.

Both examples run offline (scripted model, no API key needed):

```bash
python examples/openai_agent.py      # uses the real API if OPENAI_API_KEY is set
python examples/langgraph_agent.py
```

## OTLP ingest (any OpenTelemetry agent)

Agents that are already instrumented with OpenTelemetry (OpenLLMetry, OpenInference,
framework auto-instrumentation or your own spans) can send traces to AgentFlight over
standard **OTLP/HTTP**, without the AgentFlight SDK: `POST /v1/traces` accepts
`application/x-protobuf` and `application/json`, optionally with
`Content-Encoding: gzip`. OTLP over gRPC is not supported.

Use an `ingest` key, sent as `Authorization: Bearer <key>` (or `X-AgentFlight-Key`).
With the stock environment variables (the exporter appends `/v1/traces` to
`OTEL_EXPORTER_OTLP_ENDPOINT`; the space in the header value is written `%20`):

```bash
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8000
export OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer%20${AGENTFLIGHT_INGEST_KEY}"
export OTEL_EXPORTER_OTLP_COMPRESSION=gzip
```

Or in Python (`pip install opentelemetry-exporter-otlp-proto-http`):

```python
from opentelemetry.exporter.otlp.proto.http import Compression
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

exporter = OTLPSpanExporter(
    endpoint="http://localhost:8000/v1/traces",
    headers={"Authorization": f"Bearer {ingest_key}"},
    compression=Compression.Gzip,
)
provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(exporter))
```

Or from an OpenTelemetry Collector, next to whatever else it exports to:

```yaml
exporters:
  otlphttp/agentflight:
    endpoint: http://agentflight:8000        # the exporter appends /v1/traces
    headers:
      authorization: "Bearer ${env:AGENTFLIGHT_INGEST_KEY}"
    compression: gzip
service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [otlphttp/agentflight]
```

**How spans become runs.** One trace is one run (the run id is the trace id), as with
the SDK:

| OTLP | AgentFlight |
| --- | --- |
| Span without a parent | Closes the run: its name is the run name, its status the run status (`unset` counts as `ok`, OpenTelemetry's convention). Until it arrives the run is `running`. |
| Span without a parent that has an `agentflight.run.parent_run_id` attribute (explicit), or else span links into exactly one other trace (inferred; `AGENTFLIGHT_OTLP_INFER_PARENT_FROM_LINKS=false` turns this off) | The run is a [sub-agent run](#sub-agent-runs) of that run |
| `gen_ai.operation.name` `chat`, `text_completion`, `generate_content` or `embeddings` (or a `gen_ai.request.model` / `gen_ai.system` / `gen_ai.provider.name` without an operation) | An `llm` event: model from `gen_ai.response.model`, else `gen_ai.request.model`; tokens from `gen_ai.usage.input_tokens` / `output_tokens` (or the older `prompt_tokens` / `completion_tokens`). These count in the run's call and token totals and the `model` filter. |
| `gen_ai.operation.name` `execute_tool` (or a `gen_ai.tool.name` without an operation) | A `tool` event named by `gen_ai.tool.name` (the `tool` filter) |
| Any other span | A `span` event |
| Resource `service.name`, `service.namespace`, `service.version`, `deployment.environment(.name)` | Run metadata of the root span (the `meta=` filter) |
| Every other attribute | Kept on the event as is. Resource attributes are copied to every span as `otel.resource.*`, plus `otel.scope.name` / `otel.scope.version`, `otel.span_kind` and `otel.links`. Span events are kept. |

Spans that carry `agentflight.*` attributes (the SDK's own spans, sent through an OTLP
exporter or a Collector) are read exactly as on `POST /v1/spans`; `gen_ai.*` attributes are
not applied to them. Re-sending a batch is idempotent (spans are upserted by span id and
totals recomputed in SQL), so exporter and Collector retries can't double-count.

**Responses** follow the OTLP/HTTP spec, encoded like the request (protobuf or JSON):

| Status | When | Retried by OTLP exporters |
| --- | --- | --- |
| `200` | Stored. Spans that can't be stored (an invalid trace or span id, a name over 255 characters, an attribute over a [limit](#ingest-limits)) are left out and counted in `partial_success` (`rejected_spans`, `error_message` naming the first few); the rest of the batch is stored. | no |
| `400` | The body isn't valid OTLP protobuf/JSON, or isn't valid gzip | no |
| `401` / `403` | Missing or wrong key / a key without `ingest` | no |
| `413` | Body over `AGENTFLIGHT_MAX_BODY_BYTES`, compressed **or decompressed** (decompression stops at the cap, so a gzip bomb can't use more memory than that), or more than `AGENTFLIGHT_MAX_SPANS_PER_BATCH` spans | no |
| `415` | Another `Content-Type`, or a `Content-Encoding` other than `gzip` / `identity` | no |
| `503` | The database is unavailable (with `Retry-After`) | yes |

Errors carry a `google.rpc.Status` body. The server never answers `429` (it has no rate
limit). Unlike the SDK's exporter, stock OTLP exporters don't split a batch on `413`; the
default batch processor sends at most 512 spans per request, so keep payloads well under
16 MiB per batch or raise the cap.

**Limitations**

- **No replay for OTLP-only runs.** Replay and fork need the recorded outputs the SDK
  stores (`agentflight.llm.response`, `agentflight.tool.result`...). `gen_ai` prompts,
  completions and tool arguments stay under their own attribute names, and the viewer shows
  them, but `Recording` doesn't read them. SDK spans sent over OTLP stay replayable.
- **No cost.** The `gen_ai` conventions have no cost attribute, so `cost_usd` is only set
  from `agentflight.llm.cost_usd`.
- **Distributed traces:** a trace whose root span lives in a service that doesn't export
  to AgentFlight never closes, and shows as `abandoned` after the stale threshold. A trace
  with several parentless spans takes its name and status from the last one received.
- A span whose name, or one of whose span event names, is over 255 characters is
  rejected (counted in `partial_success`), as `POST /v1/spans` rejects it with `422`.
- The pre-1.0 `instrumentationLibrarySpans` field is ignored; send `scopeSpans`.

## Replay and fork

Re-run an agent against a recorded run. Recorded model and tool outputs are served instead of
calling anything, so a replay costs nothing and gives the same result. A fork replays up to
a step, then runs live from there.

**Know the limits first:**

- **Only intercepted calls are replayed.** Replay suits agents that only call models and
  tools. File writes, database queries, raw HTTP, the clock and randomness inside your
  agent still run live during a replay. Replay only notices them indirectly, when they
  change the input of a later step and that step diverges.
- **An incomplete recording can't replay faithfully.** If the recorder dropped spans of
  the run (full export queue), `replay()` and `fork()` log a warning as they start, before
  any step runs, and `Recording.incomplete` / `Recording.dropped_spans` say so. Missing
  steps then show up as divergences, or as live calls with `on_divergence="live"`.
- **Not covered yet:** LangGraph runs (their model and tool calls go through LangChain,
  which can't be intercepted yet; LangGraph's own checkpointer time travel covers some of
  this) and streamed OpenAI calls (`stream=True`).
- **Sequential agents only.** Steps are matched in recorded order, so calls made in
  parallel threads or tasks can come in a different order and diverge.

```python
recording = agentflight.Recording.fetch(run_id)  # or Recording.load("run.json")
for step in recording.steps:
    print(step.index, step.kind, step.name)  # 0 llm gpt-4o-mini, 1 tool search, ...

with agentflight.replay(recording) as session:
    answer = my_agent(question)  # same code as the recorded run
assert session.remaining == 0  # the replay covered every recorded step

with agentflight.fork(recording, at=1, output="no results"):
    answer = my_agent(question)  # step 0 cached, step 1 returns "no results", rest live

with agentflight.fork(recording, at=2):
    answer = my_agent(question)  # steps 0-1 cached, live from step 2 (e.g. a changed prompt)
```

**Steps.** A step is one `@agentflight.tool` call or one model call (`instrument_openai`, or
an `llm_call` block), in the order recorded. A call made inside another step (say, a model
call inside a tool) is part of that step. Serving the outer call means the inner one
never runs.

**What gets intercepted.** Only these calls are intercepted:

- `@agentflight.tool` functions: the recorded result is returned and the function doesn't
  run.
- Clients wrapped with `instrument_openai`: the client isn't called. The recorded response
  comes back as an `openai` `ChatCompletion` (or as a dict with attribute access if
  `openai` isn't installed).
- Manual `llm_call` blocks, only if your code checks `call.replayed`:

  ```python
  with agentflight.llm_call(model, messages) as call:
      if call.replayed:
          reply = call.response
      else:
          reply = client.chat(...)
          call.record(reply, tokens_in=..., tokens_out=...)
  ```

**What replay guarantees:**

- Each intercepted call is checked against its recorded step: the same kind, the same
  model or tool name, and the same inputs (messages and params, or bound tool arguments).
  Inputs are compared after the same redaction and truncation that was used for recording.
  Any mismatch, or a call beyond the end of the recording, raises `ReplayDivergence`
  naming the step and the field. Values are never included in the error.
- By default, replay never falls back to a live call. If a step can't be served, it
  raises (unlike recording, which is fail-open). A recorded failure is re-raised. Built-in exception types
  (`LookupError`, `ValueError`...) come back as that type with the recorded message. Other
  types raise `ReplayedError`, which carries `error_type` and `message`.
- A replay or fork is recorded as a new run. Its run carries
  `agentflight.replay.source_run`, `.mode` (`replay`/`fork`) and `.fork_step`. Each step span
  carries `agentflight.replay.step` and `agentflight.replay.source` (`cached`, `override`
  or `live`). Cached calls record no tokens or cost, because nothing was spent.

**Going live on divergence (opt-in).** `replay(recording, on_divergence="live")` and
`fork(..., on_divergence="live")` don't raise on the first mismatch. Instead they stop
serving recorded outputs: that step and every later call run live, and the steps are marked
`live`. The step and the reason are recorded on the session (`session.diverged_at`,
`session.divergence`), on the innermost open run as `agentflight.replay.diverged_at` and
`agentflight.replay.divergence`, and in a warning on the `agentflight` logger.

```python
with agentflight.replay(recording, on_divergence="live") as session:
    answer = my_agent(new_question)
print(session.diverged_at)  # e.g. 0: the first model call already differed
```

- This makes real model and tool calls from the divergence point on, so use it only
  where that's acceptable.
- Only mismatches switch to live. That covers a different kind, name or input, or a call
  beyond the recording. A step whose recorded output was truncated still raises
  `ReplayError`, and a recorded failure is still re-raised.

**What it doesn't guarantee:**

- **Only intercepted calls are replayed** (see the limits above).
- **Outputs come back as recorded JSON.** Tuples come back as lists, and custom objects
  come back as whatever was recorded (often a `repr` string). Redacted values come back
  redacted. A step whose recorded output was truncated raises `ReplayError` instead of
  returning a partial value. If an input was truncated, only the stored prefix is
  compared.
- **Inputs redacted by the server can't be compared.** A step whose recorded input was
  changed by [server-side redaction](#server-side-redaction-opt-in) (listed in the event's
  `redacted` field) raises `RedactedInputError`, a `ReplayDivergence`, even with
  `on_divergence="live"`. To run live from that step instead, opt in separately with
  `replay(recording, on_redacted_input="live")` (or `fork(...)`); the replayed run then
  carries `agentflight.replay.on_redacted_input = "live"`. A step whose recorded output was
  redacted by the server raises `ReplayError`; `fork(recording, at=N, output=...)` supplies
  that output.
- **Use the same `redact` hook** (and `max_attr_chars`) as when recording, or the inputs
  won't match.
- **Sequential agents only** (see above). The replay is bound to the current context (a
  `ContextVar`): asyncio tasks started inside it see it, but new threads don't.

`python examples/replay_agent.py` records the toy agent, replays it and forks it at the
tool step (needs the server running).

## Failure isolation

AgentFlight follows one rule: **observability must never become a dependency for
successful agent execution.** `tests/test_failure_isolation.py` checks each guarantee.

Guarantees:

- **Return values and exceptions pass through unchanged.** `run`, `llm_call` and `@tool`
  return what your code returns and re-raise your exact exception object, even if
  recording that exception fails.
- **Recorder failures never raise into agent code.** Each of these is caught and logged as a
  warning on the `agentflight` logger: the server being down, HTTP 4xx/5xx, redirects,
  malformed or truncated responses, transport exceptions, timeouts, exceptions from the
  exporter's `export` / `force_flush` / `shutdown`, exceptions from span processor hooks,
  and a broken tracer or serializer. If a span can't be opened, your code still runs under
  a non-recording span.
- **Weird payloads are safe.** Payloads that JSON can't serialize fall back to `repr`.
  If `repr` itself raises, the value is stored as `<unrepresentable Type>`. This covers
  circular structures, `NaN`, bytes, locks, generators and objects whose `__repr__` or
  `__str__` raise. Values are cut off at 32,000 characters.
- **No blocking in the default mode.** With `batch=True` (the default), spans are exported
  on a background thread, so a hanging server doesn't slow the agent down.
- **Run spans skip the queue.** A run's root span (the one that closes the run and
  carries its status and drop count) doesn't go through the bounded batch queue. It goes
  to a lane of its own, which a dedicated thread sends as soon as the run ends, several at
  a time if runs end together. So a run whose other spans were dropped still closes on
  the server instead of staying `running` and later showing as `abandoned`. Ending a run
  only appends the span to that lane under a lock; the agent thread never waits on the
  network. Each send has a deadline: no request starts after it, and each request's
  timeouts are capped to what is left of it (the exporter's `timeout`, 5 s by default).
  It's still best effort and fail-open: if the send fails or runs out of time, the run
  span is lost like any failed export (and the run can end up `abandoned`), and if more
  than 1,024 run spans are waiting, new ones are dropped and counted. Each run span is
  handed to the exporter once, never to both the lane and the queue. The exporter is
  only ever called by one thread at a time, so a run span can wait for a batch that is
  being sent. `flush()` and `shutdown()` wait for the lane too, within their timeouts.
- **Bounded waits.** `flush(timeout_millis=30_000)` and `shutdown(timeout_millis=30_000)`
  never raise and return within their timeout. AgentFlight enforces this itself, because
  OpenTelemetry's isn't reliable: in opentelemetry-sdk 1.45, `force_flush(300)` against a
  hanging server with four batches queued took 4.0 s (one 1 s HTTP timeout per batch).
  When the wait runs out, `flush()` returns `False` and export carries on in the
  background; whatever `shutdown()` hasn't sent by then is lost. Interpreter exit is also
  clean: AgentFlight replaces OpenTelemetry's atexit hook with a fail-safe one, bounded
  the same way.
- **A bad configuration disables recording instead of raising.**

### On-disk spool (opt-in)

```python
agentflight.configure("http://localhost:8000", spool_dir="/var/tmp/agentflight-spool")
# or AGENTFLIGHT_SPOOL_DIR=/var/tmp/agentflight-spool
```

| Setting | `configure(...)` | Environment | Default |
| --- | --- | --- | --- |
| Directory (setting it turns the spool on) | `spool_dir` | `AGENTFLIGHT_SPOOL_DIR` | unset: off |
| Kill switch | `spool_enabled=False` | `AGENTFLIGHT_SPOOL_ENABLED=false` | on (when a directory is set) |
| Size cap | `spool_max_bytes` | `AGENTFLIGHT_SPOOL_MAX_BYTES` | 64 MiB |
| Age cap | `spool_max_age_s` | `AGENTFLIGHT_SPOOL_MAX_AGE_SECONDS` | 24 h |

An explicit argument wins over the environment; the kill switch wins over a directory (so a
deployment can switch the spool off without editing code) and logs one warning at startup
saying it did. A size or age of zero or less, in code or in the environment, disables the
spool with one warning (it means "nothing on disk", not "the default") and recording
carries on. An environment value that isn't a number is ignored with a warning (the
default is used).

With a spool directory, spans that weren't stored because of a failure that may clear up
(server down, transport error, timeout, 5xx, 408/425/429, rejected credentials, the
attempt cap or the run span deadline) are written to disk, one file per failed batch,
instead of being dropped. Every later export first sends what is spooled, oldest first,
then its own batch; when a process restarts (or crashed), its first export sends what the
last one left behind. Run spans are sent before the spool is replayed.

- **Same rules as any export.** Replay shares the export's attempt cap
  (`max_export_attempts`), is split on 413 like any batch, and stops at the first other
  failure; what isn't sent stays on disk for the next export. A file that was partly sent
  keeps only the spans still unsent, in its place in line. Spans the server rejects for
  good (a single span over its body cap, other 4xx) are not spooled. The server upserts by
  span id, so a span that is sent twice (the server stored it, but the response was lost)
  is harmless.
- **Bounded.** At most `spool_max_bytes` (`AGENTFLIGHT_SPOOL_MAX_BYTES`, default 64 MiB)
  and 10,000 files; when a new batch doesn't fit, the oldest files are deleted. Files older
  than `spool_max_age_s` (`AGENTFLIGHT_SPOOL_MAX_AGE_SECONDS`, default 24 h) are deleted
  unsent. A batch bigger than the whole spool isn't kept.
- **Private, but it is sensitive data on disk.** The spool holds recorded payloads
  (messages, tool arguments and results, stack traces; after `redact`, if you set one) in
  plain, unencrypted JSON until they are sent or expire. The directory is created `0700`
  (an existing one you own is tightened to `0700`) and files, including the temporary and
  marker files, are created `0600`, whatever the umask, on systems that support Unix
  permissions (on Windows these modes do nothing; use a directory only your account can
  read). A directory that is a symlink or belongs to another user is not used. Put the
  spool on storage you would trust with the payloads themselves, and prefer a short
  `spool_max_age_s` for sensitive data.
- **Fail-open.** A full disk, a directory that can't be created or written, or a corrupt
  file (deleted when found) loses those spans, logs one warning per kind of problem and
  never raises. Files are written to a temporary name and renamed, so a crash mid-write
  leaves no half file in line (stale temporary files are cleaned up after an hour).
  `get_recorder().spool.dropped` counts the spans the spool gave up on.
- **Never on the agent's thread.** Spool writes and replay run on the export threads
  (the batch worker and the run-span lane). That's why it needs `batch=True`: with
  `batch=False` the export runs on the agent's thread, so `spool_dir` is ignored with a
  warning. It also only works with the built-in exporter.
- **Sharing a directory works but may resend duplicates; one folder per process avoids
  that.** There is no locking. File names are unique per process, a file another process
  already sent (and deleted) is skipped silently, and the size cap never deletes a file
  another process is sending: a sender marks the file it is sending, and eviction skips
  marked files (a crashed sender's marker expires after 10 minutes). If every file left is
  being sent, a new batch that doesn't fit is dropped (and counted) rather than going past
  the cap. Two processes can still send the same file; ingest is idempotent, so the cost
  is a duplicate send.

Limitations (known, not yet handled):

- **Lost spans, unless you turn on the spool.** By default, spans whose export fails are
  dropped, and if the server is unreachable the run is missing from the recording. With
  the on-disk spool (below), failures that may clear up are retried on later exports;
  without it there's no retry.
- **The batch queue is bounded.** It holds 2048 finished spans by default; set it with
  `configure(max_queue_size=...)` or `AGENTFLIGHT_MAX_QUEUE_SIZE` (OpenTelemetry's
  `OTEL_BSP_MAX_QUEUE_SIZE` also works). When the queue is full, spans are dropped rather
  than blocking the agent. A bigger queue absorbs longer bursts at the cost of memory
  (each queued span holds its payloads, up to `max_attr_chars` each).
- **Dropped spans are counted, on a best-effort basis.** AgentFlight checks the queue
  itself before handing a span to OpenTelemetry, so it knows every span it drops (on its
  own, OpenTelemetry 1.45 drops the *oldest* queued span, which can belong to another
  run). When a run ends, the spans of that run dropped so far go on the run span as
  `agentflight.run.dropped_spans`, and the viewer and replay warn about it. Run spans
  skip the queue (see "Run spans skip the queue"), so the span carrying the count isn't
  competing with the spans it counts. What this can't cover: spans of the run that end
  *after* the run span (background threads or tasks still running), a run span dropped
  because more than 1,024 were waiting to be sent, and spans lost later in export
  (server down, see "Lost spans"). Those are only in the recorder's
  total, `agentflight.get_recorder().dropped_spans`, and a warning is logged on the first
  drop. `batch=False` has no queue and never drops this way.
- **`batch=False` blocks.** It exports inside the agent's thread, so each span can block
  for up to the HTTP timeout. Use it only for tests or debugging.
- **A partly broken span processor can mis-parent later spans.** If a processor fails while
  a span is closing, the current-span context may not be restored. The agent still runs,
  but later spans in that run may be attached to the wrong parent.
- **Custom exporters must honor timeouts.** If you write a custom exporter
  that blocks forever (ignoring timeouts), only `flush`/`shutdown` timeouts protect you.
- **`BaseException`s are recorded and re-raised.** `KeyboardInterrupt` and `SystemExit` are
  never swallowed. Only ordinary `Exception`s raised by the instrumentation are suppressed.

## Overhead

Measured with [`scripts/bench_overhead.py`](https://github.com/shupandee/agentflight/blob/main/scripts/bench_overhead.py) on 2026-11-27, with run
spans on their own lane (0.1.0): **5 repeats**, each against a fresh server. Raw output:
[`docs/benchmarks/overhead-2026-11-27-run-lane.md`](https://github.com/shupandee/agentflight/blob/main/docs/benchmarks/overhead-2026-11-27-run-lane.md)
(and [`.json`](https://github.com/shupandee/agentflight/blob/main/docs/benchmarks/overhead-2026-11-27-run-lane.json)). Earlier numbers (a single run
for week 6, 5 repeats before run spans had their own lane) are in the git history.

**Method.** The "agent" makes 5 model calls and 5 tool calls that do no work, so a run is
11 spans and nearly all of its time is recording overhead. In each repeat, each scenario
runs it 2,000 times (200 for `batch=False`) after 200 warm-up runs, timing each run on
the agent thread with `time.perf_counter_ns`. The table gives the median of the five
per-repeat medians, the spread of those medians (min to max), the median p95, and the
added time per span versus the same code without AgentFlight. The HTTP scenarios export
to a real server (uvicorn + SQLite, separate process); the benchmark is pinned to one CPU
and the server to the other seven, and afterwards the spans the server stored, the spans
the SDK counted as dropped and the runs the server has with a final status are added up
over all repeats. Messages are about 100 characters ("small") or 8,000 ("large").

**Machine.** Linux 6.12 x86_64 VM (KVM), Intel Xeon, 8 vCPUs; CPython 3.13.5,
opentelemetry-sdk 1.45.2, httpx 0.29.0, uvicorn 0.54.0. The box is shared: the 1-minute
load average at the start of the repeats ranged from 0.81 to 1.24.

| Payload | Scenario | Median per run | Spread (5 medians) | p95 | Added per span | Spans stored | Dropped (SDK count / on runs) | Runs closed |
| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| small | no AgentFlight | 0.7 µs | 0.7 to 0.8 µs | 0.8 µs | — | — | — | — |
| small | SDK, no exporter | 383.3 µs | 381.3 to 389.8 µs | 458.5 µs | 34.8 µs | — | — | — |
| small | SDK + JSON serialization, no network | 532.0 µs | 528.4 to 535.9 µs | 623.2 µs | 48.3 µs | — | — | — |
| small | HTTP, `batch=True` (default), queue drained every 100 runs | 421.4 µs | 413.0 to 457.3 µs | 784.3 µs | 38.2 µs | 121,000 of 121,000 | 0 / 0 | 11,000 of 11,000 |
| small | HTTP, `batch=True`, tight loop | 420.0 µs | 416.4 to 447.4 µs | 533.9 µs | 38.1 µs | 55,718 of 121,000 | 65,282 / 65,282 | 11,000 of 11,000 |
| small | HTTP, `batch=False` | 64,347.6 µs | 59,479.7 to 65,389.2 µs | 71,882.5 µs | 5,849.7 µs | 12,100 of 12,100 | 0 / 0 | 1,100 of 1,100 |
| large | no AgentFlight | 1.5 µs | 1.4 to 1.6 µs | 1.5 µs | — | — | — | — |
| large | SDK, no exporter | 833.4 µs | 826.0 to 836.4 µs | 946.6 µs | 75.6 µs | — | — | — |
| large | SDK + JSON serialization, no network | 1,235.4 µs | 1,228.5 to 1,252.0 µs | 1,397.1 µs | 112.2 µs | — | — | — |
| large | HTTP, `batch=True` (default), queue drained every 100 runs | 875.7 µs | 865.9 to 878.7 µs | 1,640.2 µs | 79.5 µs | 121,000 of 121,000 | 0 / 0 | 11,000 of 11,000 |
| large | HTTP, `batch=True`, tight loop | 864.9 µs | 861.3 to 928.2 µs | 1,127.0 µs | 78.5 µs | 60,018 of 121,000 | 60,982 / 60,982 | 11,000 of 11,000 |
| large | HTTP, `batch=False` | 70,389.9 µs | 66,609.9 to 72,645.4 µs | 80,479.0 µs | 6,398.9 µs | 12,100 of 12,100 | 0 / 0 | 1,100 of 1,100 |

**Server ingest rate**, measured by sending already-recorded spans in 512-span batches
back to back from one thread (20 batches per payload and repeat), next to how fast the
tight loop makes spans:

| Payload | Server ingest, spans/s (median of 5) | Spread | Tight loop makes, spans/s | Tight loop: stored, spans/s |
| --- | ---: | ---: | ---: | ---: |
| small | 9,604 | 9,211 to 9,773 | 26,189 | 7,162 |
| large | 4,248 | 3,735 to 4,362 | 12,718 | 3,371 |

What this says:

- With the default `batch=True`, recording costs the agent about **38 µs per span with
  small payloads and 79 µs with 8,000-character ones**, about 0.42 ms and 0.87 ms for
  this 11-span run. In the `batch=True` rows the slowest repeat was up to 8.5% above
  the median and the fastest within 2% of it (the box is shared).
- **The dropped spans are a rate mismatch.** The tight loop makes spans 2.7 to 3.0 times
  faster than one exporter thread and this server store them (26,189 vs 9,604 spans/s
  small, 12,718 vs 4,248 large), and while the loop runs the exporter also shares the
  agent's CPU, so the server stored only 7,162 and 3,371 spans/s. The 2,048-span queue
  fills in well under a second, and about half the spans were dropped (54% and 50% over
  the five repeats). The SDK counted every one (stored + dropped = sent), **every drop
  is recorded on its run** (65,282 of 65,282 small, 60,982 of 60,982 large), and **every
  run closed** (11,000 of 11,000): run spans skip the queue.
- Real agents spend hundreds of milliseconds on each model call, far below these rates.
  A burst of thousands of spans can still lose some; `max_queue_size` buys headroom.
- `batch=False` waits for an HTTP round trip and a database commit per span, about 6 ms
  each here. Use it for short scripts and debugging, not in a loop.

These are one machine's numbers for a synthetic agent, not a guarantee; run the script on
yours (`python scripts/bench_overhead.py`, about 5 minutes; `--quick` for a short run).

## Run status

| Status | Meaning |
| --- | --- |
| `running` | The run span hasn't arrived with an end time yet. |
| `ok` / `error` | The run span ended with that status. |
| `unknown` | The run span ended without setting a status (possible with raw `/v1/spans` senders and non-SDK OTel spans; the SDK always sets one). |
| `abandoned` | Still `running`, but no span of the run has been received for `AGENTFLIGHT_STALE_AFTER_MINUTES` (default `60`, `0` turns it off), e.g. because the agent process crashed. |

`abandoned` is never stored. It's decided when runs are read, from the run's
`last_seen_ns` (server time of its last received span), so changing the threshold needs no
data fix, and a late span turns the run back into `running` or its final status. Filter
with `GET /v1/runs?status=abandoned`.

## Errors and failures

Each run summary has two error numbers:

- `errors` is the number of **distinct logical failures**.
- `error_events` is the number of events with error status.

Every error event carries `agentflight.failure.id`. The id follows the exception object, so
a tool raising `LookupError`, the agent calling `agentflight.error(exc)` on it, and the run
failing with it all share one id and count as **one** failure. An exception raised while
handling a recorded one (`raise X from exc`, or inside its `except`) inherits that id.
Independent exceptions get new ids and count separately. Every event is still stored.
Error events from SDKs that don't send ids count one each.

## Payload size and redaction

```python
agentflight.configure(
    max_attr_chars=32_000,  # per payload; None disables the cap
    redact=agentflight.redact_fields("api_key", "password", "authorization"),
)
```

- **Truncation is explicit.** A payload (messages, response, tool args/result, metadata,
  decision choice, state) longer than `max_attr_chars` is cut to that length. Its key is then
  listed in the span's `agentflight.truncated` attribute, and in the API's per-event
  `truncated` field, as
  `{"original_length": ..., "stored_length": ..., "reason": "exceeds max_attr_chars=..."}`.
  So you can always tell a truncated recording from the original message. The limit is a
  storage limit, not a model context limit.
- **Redaction.** `redact(key, value)` runs on every payload before it is serialized, and
  `key` is the attribute name (for example `agentflight.tool.args`). `redact_fields(...)`
  masks dict entries with those names (case-insensitive) at any depth. If your hook
  raises, the value is replaced with `"<redaction failed>"`; it fails closed and nothing is
  recorded.
- **Errors are redacted too.** Exception messages go through the hook with the key
  `agentflight.error.message`, and stack traces with `exception.stacktrace`, before they
  are stored (in the error attributes, the span status and the OpenTelemetry `exception`
  event). Both are plain strings, so a hook that should scrub them has to handle `str`
  values; `redact_fields(...)` only masks dict entries. Both are capped at
  `max_attr_chars`, and a cut message is listed in `agentflight.truncated`.
- **Names are not redacted.** Run, tool and model names are stored as given, because
  replay matches steps by them. Don't put personal data or secrets in run names (or tool
  names); put them in metadata or arguments, where the hook applies. This holds for
  server-side redaction too.

### Server-side redaction (opt-in)

The `redact` hook only covers what your own process records with the SDK. To enforce a
policy on everything the server stores, including spans from other services and plain
OpenTelemetry agents over [OTLP](#otlp-ingest-any-opentelemetry-agent), give the server a
TOML policy file:

```toml
# redaction.toml
version = 1
# marker = "[REDACTED:{rule}]"   # the default

[[rules]]
name = "credentials"
keys = ["api_key", "password", "authorization"]   # dict keys at any depth, any case

[[rules]]
name = "tokens"
preset = ["bearer", "aws-access-key-id", "openai-key"]

[[rules]]
name = "internal-ids"
patterns = ['\bACCT-[0-9]{8}\b']                  # Python regular expressions

[[rules]]
name = "resource-secrets"
attributes = ["otel.resource.deploy.token"]        # exact names or globs (fnmatch)
action = "drop"                                    # remove instead of masking
# scope = ["attributes", "events", "status"]       # the default: everywhere
```

```bash
AGENTFLIGHT_REDACTION_FILE=/etc/agentflight/redaction.toml agentflight-server serve
```

| Setting | Default | |
| --- | --- | --- |
| `AGENTFLIGHT_REDACTION_FILE` | unset (off) | The policy. The server refuses to start if it is missing or invalid |
| `AGENTFLIGHT_REDACTION_ON_ERROR` | `redact` | A value whose redaction fails: `redact` stores `[REDACTION_FAILED]` in its place; `reject` doesn't store that span |

- **Applied before anything is stored.** Both `POST /v1/spans` and `POST /v1/traces` run
  the policy on every span after the size checks and before the run name, metadata,
  tokens or failure ids are derived. It covers span attributes, span event attributes
  (such as OpenTelemetry `exception` events) and status messages; `scope` limits a rule to
  some of them.
- **What a rule matches.** `keys` match dict keys inside JSON values: SDK payloads, run
  metadata, OTLP map and array values, span link attributes, and plain string attributes
  that hold JSON (such as `gen_ai.input.messages`), which are parsed when they start with
  `{` or `[`. `patterns` are replaced wherever they match a string. `attributes` redact a
  whole attribute. Presets cover key-shaped secrets only: `bearer` tokens, AWS access key
  ids, and `sk-...` API keys. Emails, card numbers and the like need your own `patterns`.
- **Truncated or broken JSON is matched as text.** A payload the SDK cut at
  `max_attr_chars` (listed in `agentflight.truncated`) isn't valid JSON, so the server
  applies `patterns` and looks for `"key": value` pairs in the text instead. A secret that
  the truncation itself cut in half may not match any more; set `max_attr_chars` high
  enough, or redact on the client too.
- **Never redacted:** span and event names, and the structural attributes AgentFlight
  reads: `agentflight.kind`, run, tool, model, node, decision and state names
  (`agentflight.tool.name`, `agentflight.llm.model`...), token counts, cost, error type,
  failure ids, parent run ids, `agentflight.truncated`, `agentflight.replay.*`. Globs skip
  them, and naming one exactly in `attributes` is a startup error.
- **What was redacted is recorded.** The server writes `agentflight.redacted` on the span:
  `{key: {"rules": [...], "count": n}}` (with `"dropped": true` for dropped attributes,
  keys such as `status_message` and `events[0].exception.message` for those parts). The API
  returns it as each event's `redacted` field, also with `payloads=false`. It is always the
  server's own: a value sent by a client is replaced, and a re-sent span gets a new one.
- **Markers can be longer than the text they replace.** If a redacted value would exceed
  `AGENTFLIGHT_MAX_ATTRIBUTE_CHARS`, the whole value is stored as one marker (and
  `"over_cap": true`); it is never cut.
- **Fails closed.** If redacting a value raises, `[REDACTION_FAILED]` is stored instead of
  it. If a whole span can't be processed, every string or structured value that isn't
  structural, every span event attribute and the status message become
  `[REDACTION_FAILED]`. With `AGENTFLIGHT_REDACTION_ON_ERROR=reject` such a span isn't
  stored; the rest of the batch is. `POST /v1/spans` then answers `202` with
  `"rejected": n` and an `error_message` naming the spans (never values), and OTLP reports
  them in `partial_success`. The SDK exporter logs one warning per export for rejected
  spans and neither retries nor spools them (they would be rejected again).
- **Not retroactive.** The policy applies to spans received after a (re)start; spans
  already stored are not rewritten (and backups or database replicas keep what they have).
  A policy change needs a restart and applies to spans received from then on. There is no
  command yet to redact stored runs.
- **Cost.** Redaction runs on every span, in the request. On one machine, a
  `POST /v1/spans` of 512 spans took, without a policy file and with a two-rule policy (one
  key rule with three keys, one rule with three presets): about 40 ms and 55 ms (in-memory
  SQLite) or 56 ms and 74 ms (local Postgres) for a realistic batch (multi-line prompts,
  replies and tool output, 1.47 MB of payload), and about 44 ms and 50 ms, or 58 ms and
  66 ms, for single-line payloads with nothing to redact (2.36 MB). Payloads that contain
  a pattern's literals (for example the word "bearer", or "sk-" in "task-") cost more:
  the pattern then scans them.
  Measure with your own policy and payloads; the cost grows with payload size and rule
  count. Custom patterns get no literal pre-check. The method is in
  `docs/design/server-side-redaction.md`.
- **Client and server together** work: the client hook runs first, then the server policy.
  Markers never match a rule, so redacting twice changes nothing.
- `422` responses never echo the submitted values, and database errors are logged without
  their SQL parameters, whether or not a policy is set.

## Authentication

The server **requires an API key by default** and refuses to start without one.

| Setting | Meaning |
| --- | --- |
| `AGENTFLIGHT_API_KEY` | A key with every permission. Clients send `Authorization: Bearer <key>` (or `X-AgentFlight-Key`). |
| `AGENTFLIGHT_API_KEYS` | Scoped keys: comma-separated `<key>:<permission>[+<permission>...]`, e.g. `k1:ingest,k2:read,k3:read+delete`. |
| `AGENTFLIGHT_AUTH_DISABLED=true` | Explicit opt-out for local development only. A warning is logged at startup. |

The server starts if at least one of `AGENTFLIGHT_API_KEY` and `AGENTFLIGHT_API_KEYS` is
set. Give recorders an `ingest`-only key, so a leaked agent key can't read or delete
history, and give people and dashboards a `read` key.

- Every configured key must be at least 32 characters (`openssl rand -hex 32` gives 64);
  a shorter one stops the server at startup, and the error names the setting, not the
  key. That is what makes guessing impractical: **the sign-in page and the API have no
  rate limit on failed keys yet** (deferred; put a rate-limiting proxy in front if you
  need one now).
- Keys are compared in constant time (`hmac.compare_digest`). Wrong or missing keys get
  `401`, and the response never echoes the key.
- Endpoints require an `ingest`, `read` or `delete` permission. A valid key without the
  needed permission gets `403`. A malformed `AGENTFLIGHT_API_KEYS` (unknown permission,
  duplicate key) stops the server at startup, and the error never includes the key.
- `Authorization` and `X-AgentFlight-Key` are never logged. `redact_headers()` is the
  helper for anything that logs headers, and a log filter on the server's loggers scrubs the
  configured keys from any message as a backstop.
- **SDK:** `configure(api_key=...)`, or the `AGENTFLIGHT_API_KEY` environment variable.
  If the server rejects the key, the SDK logs one warning (without the key) and drops the
  spans. The agent keeps running.
- `/health` and `/ready` are unauthenticated so orchestrators can probe them.

### Viewer sign-in

The viewer at `/ui` asks once for a key with the `read` permission (an all-permission key
works too) and then keeps a **signed session** in a cookie:

- The cookie never contains the key. It holds an HMAC fingerprint of the key, the `read`
  permission and an expiry, signed with the session secret. Every request re-checks the
  fingerprint against the configured keys, so removing a key ends its sessions.
- The cookie is `HttpOnly`, `SameSite=Strict`, scoped to `/ui`, and `Secure` when the
  request is HTTPS (behind a TLS-terminating proxy, run uvicorn with `--proxy-headers`, or
  set `AGENTFLIGHT_COOKIE_SECURE=true`).
- Sessions are **read-only**. The `/v1` API never accepts the cookie, and the viewer has
  no delete or ingest actions.
- Sign-in and sign-out posts must carry an `Origin` header whose host matches `Host`;
  a cross-site, `null` or **missing** `Origin` gets `403` (current browsers always send
  it on form posts). A reverse proxy must pass the original `Host` through.
- With `AGENTFLIGHT_AUTH_DISABLED=true` there is no sign-in page.
- By default the signing secret is an HMAC of all configured API keys, so every worker
  and replica with the same keys accepts the same sessions and restarts don't sign anyone
  out. **Rotating, adding or removing any key ends every viewer session**; people sign in
  again. Set `AGENTFLIGHT_SESSION_SECRET` to decouple sessions from the keys.

| Setting | Default | Meaning |
| --- | --- | --- |
| `AGENTFLIGHT_SESSION_SECRET` | derived from the API keys | Override for the signing secret, at least 32 characters. |
| `AGENTFLIGHT_SESSION_HOURS` | `12` | Session lifetime. |
| `AGENTFLIGHT_COOKIE_SECURE` | `false` | Always mark the cookie `Secure`. |

## Ingest limits

Every ingest request is bounded. The defaults are deliberately generous, so they shouldn't
reject anything the SDK sends with its default settings; they will be tuned once real
batch sizes are measured. A request over a limit gets `413` and nothing from it is stored. On `POST /v1/traces` the body
cap also applies to the decompressed body, and a span over a per-span limit is rejected on
its own (see [OTLP ingest](#otlp-ingest-any-opentelemetry-agent)).

| Setting | Default | Limit |
| --- | --- | --- |
| `AGENTFLIGHT_MAX_BODY_BYTES` | `16777216` (16 MiB) | Request body size, checked from `Content-Length` and while reading chunked bodies |
| `AGENTFLIGHT_MAX_SPANS_PER_BATCH` | `2048` | Spans in one `POST /v1/spans` or `POST /v1/traces` (the SDK's batch processor sends at most 512 by default) |
| `AGENTFLIGHT_MAX_ATTRIBUTE_CHARS` | `262144` | Characters in one attribute value, span event attribute value or status message (the SDK caps payloads at 32,000 by default) |
| `AGENTFLIGHT_MAX_ATTRIBUTES_PER_SPAN` | `1024` | Attributes on one span or span event (OpenTelemetry's SDK keeps 128 by default) |
| `AGENTFLIGHT_MAX_EVENTS_PER_SPAN` | `1024` | Events on one span (OpenTelemetry's SDK keeps 128 by default) |

A full SDK batch of payload-heavy spans can be bigger than 16 MiB. When the server answers
`413`, the SDK's exporter splits the batch in half and retries each half, so only spans
over the cap on their own are dropped, with one warning per export.

Splitting is capped at `max_export_attempts` HTTP requests per export (default `32`, set
it with `configure(max_export_attempts=...)`), and the first failure that isn't a `413`
(a timeout, a transport error, a 5xx, rejected credentials) stops the export: the halves
not yet sent are dropped (or spooled, with the [on-disk spool](#on-disk-spool-opt-in))
instead of being tried against a server that is already failing. Isolating one oversized span in a full 512-span batch takes 19 requests.

Worst case for one export: a server that is down or hanging costs one `timeout` (5 s by
default), since the first failure ends the export. Only a server that keeps answering
`413` slowly can make an export take up to `max_export_attempts` × `timeout`. Replaying
the spool is part of the same export and shares its attempt cap, so it doesn't change
these bounds. With
`batch=True` (the default) exports run on a background thread; `flush()` and
`shutdown()` stop waiting at their own timeouts (see [Failure isolation](#failure-isolation)).
With `batch=False` every span is exported on its own as it ends, so there is nothing to
split and a span end blocks for at most one `timeout`.

Span, span event and attribute names are limited to 255 characters (`422` above that). If
you turn off the SDK's payload cap (`max_attr_chars=None`), raise
`AGENTFLIGHT_MAX_ATTRIBUTE_CHARS` too, or large payloads will be rejected.

## Retention and deletion

| Setting | Default |
| --- | --- |
| `AGENTFLIGHT_RETENTION_ENABLED` | `true` |
| `AGENTFLIGHT_RETENTION_DAYS` | `30` |
| `AGENTFLIGHT_CLEANUP_INTERVAL_HOURS` | `24` |

```bash
agentflight-server cleanup --dry-run    # count runs older than the retention period
agentflight-server cleanup              # delete them (and all their events)
agentflight-server cleanup --days 7     # one-off override
agentflight-server cleanup --loop       # repeat every CLEANUP_INTERVAL_HOURS (Compose runs this)
```

A run's age is measured from when it ended, or from when it started if it never finished.
Runs with no timestamps are kept. `DELETE /v1/runs/{id}` (which needs the `delete`
permission) removes a single run with all its events, including the messages and tool
payloads stored on them.

## HTTP API

The API is versioned by its `/v1` prefix. From 0.1.0 on, `/v1` only changes in ways
that don't break clients (new endpoints, optional fields, new response fields; clients
should ignore fields they don't know); a breaking change would mean `/v2`, served next to
`/v1` for a while. The full policy is in the
[changelog](https://github.com/shupandee/agentflight/blob/main/CHANGELOG.md#http-api-versioning).

| Method | Path | Permission | Description |
| --- | --- | --- | --- |
| `POST` | `/v1/spans` | ingest | Ingest a batch of spans (`{"spans": [...]}`). `202` with `accepted` (spans stored), `runs`, and `rejected`/`error_message` (spans not stored by [server-side redaction](#server-side-redaction-opt-in) in `reject` mode) |
| `POST` | `/v1/traces` | ingest | [OTLP/HTTP](#otlp-ingest-any-opentelemetry-agent) trace export (protobuf or JSON, optionally gzip) |
| `GET` | `/v1/runs` | read | List runs, newest first, with token/cost/error totals. Paging: `limit` (1-500, default 50), `offset`. Filters (all must match): `status` (`running`, `ok`, `error`, `unknown`, `abandoned`), `name` (case-insensitive substring), `since_ns`/`until_ns` (run start, Unix ns), `has_errors`, `model`, `tool` (exact), `meta=key:value` (string metadata values, exact; up to 5), `parent_run_id` (the runs that run started), `has_parent` (`true`: only sub-agent runs, `false`: only top-level runs) |
| `GET` | `/v1/runs/{run_id}` | read | One run with its events ordered by start time. Every event with payloads by default (what replay uses). `events_limit` (1-1000) and `events_offset` page the events; `payloads=false` leaves recorded payloads (messages, outputs, args, stack traces) out and lists them in each event's `omitted` |
| `GET` | `/v1/runs/{run_id}/events/{span_id}` | read | One event with all of its payloads |
| `DELETE` | `/v1/runs/{run_id}` | delete | Delete a run and all its events (`204`, or `404`) |
| `GET` | `/health` | none | Liveness (process is up) |
| `GET` | `/ready` | none | Readiness (database reachable and migrated); 503 otherwise |

Interactive docs at `/docs` when the server is running.

## Development

```bash
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
ruff check . && ruff format --check . && mypy && pytest
```

Tests use in-memory SQLite by default. Set `AGENTFLIGHT_TEST_DATABASE_URL` to a Postgres URL
to run the same suite against Postgres (CI does both).

CI also runs a Compose smoke test with a throwaway key. It brings up
`docker compose up --wait` (Postgres plus the server, both health-checked) and runs
`scripts/compose_smoke.py`, which checks that:

- missing and wrong keys are rejected;
- the toy agent still completes with a wrong key, and nothing is persisted;
- with the right key, both runs are persisted with the expected token, cost and
  (deduplicated) error totals;
- `DELETE` removes a run.

The stack is always torn down afterwards. You can run it locally:

```bash
export AGENTFLIGHT_API_KEY=$(openssl rand -hex 32)
docker compose up --build --wait
python scripts/compose_smoke.py
docker compose down --volumes
```

## Roadmap

- **Week 1: recorder core** (done): SDK, OTel exporter, ingest API, Postgres storage,
  migrations, failure isolation, tests, CI with a Compose smoke test.
- **Week 2: hardening** (done): logical failure counting, API-key auth, retention
  and deletion, explicit truncation and redaction.
- **Week 3: integrations** (done): OpenAI-style chat client instrumentation (usage from
  responses) and a LangGraph callback handler, with offline example agents.
  Deferred: streaming and the Responses API.
- **Week 4: deterministic replay and fork** (done): replay a recorded run from
  cached model and tool outputs with divergence checks, and fork from any step with a
  substituted output or changed code. Deferred: LangGraph replay, streamed calls,
  parallel tool calls, a server-side "fork" endpoint.
- **Week 5: viewer and scale fixes** (this branch): a server-rendered viewer (Jinja2 and
  htmx, no JavaScript build) with a run list, filters, a run timeline and highlighted
  failures; stored run totals and batched ingest; run filters and paged run detail;
  scoped API keys; `unknown` and `abandoned` run status; ingest limits; redacted
  exception messages and stack traces.
- **Week 6: tree, screenshots, overhead** (done): an indented span tree in the viewer,
  README screenshots, an overhead benchmark with its method and measured results, and a
  cap on the exporter's split-on-413 requests.
- **Week 7: dropped spans** (done): spans dropped from a full export queue are counted
  and shown as incomplete recordings in the viewer and replay; a configurable queue size;
  split retries stop at the first non-413 failure; bounded `flush()` and `shutdown()`;
  benchmark repeats and the server's ingest rate.
- **0.1.0** (released): run spans on their own lane, so runs with drops still close;
  release packaging (migrations in the package, `agentflight-server migrate`, a Trusted
  Publishing release workflow) and the `/v1` stability policy; an opt-in on-disk spool
  for failed exports.
- **0.2.0** (released): OTLP ingest: `POST /v1/traces` takes OTLP/HTTP (protobuf or JSON,
  gzip) from any OpenTelemetry-instrumented agent or Collector, with `gen_ai.*` mapped to
  models, tokens and tools. Deferred: OTLP over gRPC, replay from `gen_ai` content,
  cost from a price table.
- **0.3.0** (released): **sub-agent runs**: `parent_run_id` links runs started inside other runs
  across the SDK, `/v1/spans`, OTLP, the viewer and the run filters.
- **Server-side redaction** (0.3.0): a TOML policy the server applies to every span
  from `/v1/spans` and OTLP before storing it, with replay refusing redacted inputs.
  Deferred: a viewer badge and run-level count, a command to redact stored runs.
- **Next:** run diff. Deferred: a demo, overhead
  on real (non-synthetic) agent runs, and a rate limit on failed keys.

## License

MIT
