Traces
Give related events a shared trace id, then list traces and read one back with its spans.
A trace is a set of events sharing one trace_id. The SDK creates one for you
on the first publish, but for anything multi-step you want to pin the trace
yourself so every step lands under the same id.
Tracing GenAI model calls? The recommended path is
OpenTelemetry-native telemetry: reuse your own
TracerProvider and emit standard gen_ai.* spans over OTLP instead of
event-model traces.
Pin a trace to a unit of work
from axonpush import AxonPush, EventType, get_or_create_trace
with AxonPush() as client:
trace = get_or_create_trace()
client.events.publish(
"research.start",
{"goal": "Find recent papers on transformer architectures"},
channel_id,
agent_id="research-agent",
trace_id=trace.trace_id,
span_id=trace.next_span_id(),
event_type=EventType.AGENT_START,
)
client.events.publish(
"web_search",
{"query": "transformer architecture papers 2026"},
channel_id,
agent_id="research-agent",
trace_id=trace.trace_id,
span_id=trace.next_span_id(),
event_type=EventType.AGENT_TOOL_CALL_START,
metadata={"tool_name": "web_search"},
)get_or_create_trace() returns the trace already active on the current
context, or creates and installs one. Because it is stored in a
contextvars.ContextVar, each asyncio task, and each thread that copies its
parent’s context, sees its own value, so concurrent requests do not braid
their traces together.
Pass an id to adopt one that came from elsewhere, such as a request header:
trace = get_or_create_trace("3f1c9d84-6a2b-4c7e-9f10-2b8d5e6a1c47")That installs the id as the current context and returns it. current_trace()
reads the active context without creating one, returning None when there is
none.
TraceContext is a plain dataclass, not a context manager, with TraceContext(...) raises. To scope a trace, call get_or_create_trace(id) at
the top of the unit of work; the context var keeps it isolated per task.
TraceContext
| Member | Returns | What it is |
|---|---|---|
trace_id | str | A UUID4 string, generated on construction unless supplied. |
next_span_id() | str | A fresh 16-character lowercase hex span id. |
w3c_trace_id() | str | The trace id as a 32-hex W3C id, hashed if it is not already hex. |
traceparent(span_id=None) | str | A sampled W3C traceparent header value. |
You rarely build spans by hand. The transport already injects
X-Axonpush-Trace-Id, X-Axonpush-Span-Id and traceparent on every request
while a trace is active, and the framework integrations map their own run ids
onto span_id.
Read a trace back
client.traces has two methods: list (GET /traces) and get
(GET /traces/{traceId}). client.traces_v2 still resolves, but it is a
deprecated alias of client.traces with the same two methods.
detail = client.traces.get(trace.trace_id)
for span in detail.spans or []:
print(span.span_id, span.event_type, span.operation_name, span.duration_ms)get() returns the trace id and its spans, each a full stored
event record. Ingest is eventually consistent,
a trace requested in the same millisecond as the last publish may not see it
yet.
List traces
page = client.traces.list(since="2026-08-01T00:00:00Z", limit=20)
for t in page.traces or []:
print(t.trace_id, t.span_count, f"{t.duration_ms:.0f}ms", t.outcome)list() takes since, until, limit and offset as keywords and returns
summaries newest first. Page with offset.
TraceSummary
| Field | Type | Meaning |
|---|---|---|
trace_id | str | The trace id. |
started_at / ended_at | str | First and last span. |
duration_ms | float | Wall time in milliseconds. |
span_count | int | Spans in the trace. |
error_count | int | Error spans. |
first_error_type | str | The first error type seen, if any. |
outcome | str | Derived trace outcome. |
root_span_name | str | Name of the root span. |
models / providers | list[str] | Distinct models and providers seen. |
total_tokens | int | Token total, when known. |
cost_usd | float | Attributed cost, when known. |
service_name, environment, release | str | Where the trace ran. |
session_id, customer, workflow | str | Grouping attributes, when present. |
The API also serves trace groups (GET /trace-groups) and richer trace filters
(model, provider, agent, tool, status, errorsOnly and more). The
0.0.19 client does not wrap them; call the HTTP API directly.
Async
Every method has an async sibling:
async with AsyncAxonPush() as client:
page = await client.traces.list(limit=20)
detail = await client.traces.get(trace_id)