zespan-fastapi package adds ASGI middleware to your FastAPI application that automatically creates a trace context for every incoming HTTP request. You can then use the @observe_llm and @observe_span decorators to attach LLM call details and custom spans to that context without passing trace objects through your call stack.
Installation
zespan-fastapi requires httpx for flushing traces. Install it alongside the package if it is not already in your environment:
Adding the middleware
ImportZespanFastAPIMiddleware and ZespanConfig, then register the middleware with your FastAPI app using add_middleware.
One trace per request
A request that arrives without atraceparent header is one trace. The middleware runs your app inside a fresh Zespan context, so every model call the handler makes (two or ten) lands in the same trace instead of each call starting its own. That context’s root span is never sent, so the trace has no extra empty span. It stays set until the response body has finished, including a streamed response body, and it is left when the request ends, even if the handler raises. Concurrent requests never share a trace.
- A request that carries a valid
traceparentcontinues that trace, as before. - With
trust_inbound_traceparent=Falsethe header is ignored and the request is still its own fresh trace. - Two agent runs inside one request share the trace.
To keep the earlier behaviour (no trace of its own; each model call starts one), set
ZESPAN_HTTP_TRACE_PER_REQUEST=off (also 0 or false). It is read on every request, so you can change it without a redeploy of the middleware.ZespanConfig options
Override the Zespan API base URL. Use this only if you are self-hosting the ingest endpoint.
When
False, the middleware passes all requests through without tracing. Useful for disabling tracing in test environments.Fraction of requests to trace, between
0.0 and 1.0. Set to 0.1 to trace 10% of requests in high-traffic environments.When
True, the request body is captured and attached to the trace. Enable only after reviewing your data retention policy — request bodies may contain sensitive data.Field names whose values are redacted before any data is stored. Applied regardless of
capture_request_body.Tracing LLM calls with @observe_llm
Use the @observe_llm decorator on any async function that makes an LLM call. The decorator captures the model name, provider, duration, and token usage (extracted automatically from the response object if it has a .usage attribute).
@observe_llm parameters:
Tracing custom operations with @observe_span
Use @observe_span to instrument any async function as a named span. The decorator records the function name, duration, and status (success or error) as attributes on the current request’s trace context.
@observe_span parameters:
Setting custom attributes
Useset_attribute to attach arbitrary key-value data to the current request’s trace context. Call it from anywhere in your request handler — the attribute is included in the trace when it flushes.
Getting the current trace ID
Useget_current_trace_id() to retrieve the trace ID for the current request. This is useful for logging or for correlating Zespan data with your own observability stack.

