# Query Spans

Source: https://agenta.ai/docs/reference/api/query-spans-rpc

`POST /tracing/spans/query`

Base URL: `https://us.cloud.agenta.ai/api` (US cloud), `https://eu.cloud.agenta.ai/api` (EU cloud) or `$AGENTA_HOST/api` (self-hosted). Authentication: `Authorization: ApiKey YOUR_API_KEY`.

:::caution[deprecated]

This endpoint has been deprecated and may be replaced or removed in future versions of the API.

:::

Query spans and traces in the tracing backend.

Use `focus` in the request body to control the response shape:

- `"trace"` (default): returns a nested `traces` tree keyed by
  `trace_id` then by span name. Children hang off their parent's
  `spans` field. Best for rendering a trace waterfall.
- `"span"`: returns a flat `spans` list. Best for paginating or
  filtering across all spans regardless of hierarchy.

Use `oldest` / `newest` (unix seconds) to window the query and
`limit` to cap the number of traces/spans returned. One response holds
at most one page: 100 rows when `limit` is not set, and never more than
1000 (the server settings `AGENTA_API_TRACING_QUERY_DEFAULT_LIMIT` and
`AGENTA_API_TRACING_QUERY_MAX_LIMIT`). When the page is full, the
response has a `windowing` object: send it as `windowing` to read the
next page.

Span `attributes` here leave out `ag.data.parameters.agent.skills` (the
skill text of an agent run). To read a whole trace, filter only on
`trace_id`, or call `GET /traces/{trace_id}`.

The response preserves the Agenta `ag.*` attribute namespace and
includes computed metrics (`ag.metrics.duration`, `ag.metrics.tokens`,
`ag.metrics.costs`) on each span. The `traces` tree returned here is
the same shape that `POST /tracing/spans/ingest` accepts as its
`traces` field.

## Parameters

- `focus` (in query, string, one of: "trace", "span")
- `format` (in query, string, one of: "agenta", "opentelemetry")
- `oldest` (in query, string | integer)
- `newest` (in query, string | integer)
- `limit` (in query, integer)
- `interval` (in query, integer)
- `rate` (in query, number)
- `filter` (in query, any)

## Response schemas

### 200: Successful Response

- `count` (integer): Total number of matching traces or spans in the window. Default: `0`.
- `spans` (array of object, nullable): Flat list of spans, populated when the query was run with `focus="span"`.
  - `created_at` (string (date-time), nullable)
  - `updated_at` (string (date-time), nullable)
  - `deleted_at` (string (date-time), nullable)
  - `created_by_id` (string (uuid), nullable)
  - `updated_by_id` (string (uuid), nullable)
  - `deleted_by_id` (string (uuid), nullable)
  - `trace_id` (string, required)
  - `span_id` (string, required)
  - `parent_id` (string, nullable)
  - `trace_type` (string, one of: "invocation", "annotation", "unknown", nullable)
  - `span_type` (string, one of: "agent", "chain", "workflow", "task", "tool", "embedding", "query", "llm", "completion", "chat", "rerank", "unknown", nullable)
  - `span_kind` (string, one of: "SPAN_KIND_UNSPECIFIED", "SPAN_KIND_INTERNAL", "SPAN_KIND_SERVER", "SPAN_KIND_CLIENT", "SPAN_KIND_PRODUCER", "SPAN_KIND_CONSUMER", nullable)
  - `span_name` (string, nullable)
  - `start_time` (string (date-time) | integer, nullable)
  - `end_time` (string (date-time) | integer, nullable)
  - `status_code` (string, one of: "STATUS_CODE_UNSET", "STATUS_CODE_OK", "STATUS_CODE_ERROR", nullable)
  - `status_message` (string, nullable)
  - `session_id` (string, nullable)
  - `turn_id` (string, nullable)
  - `user_id` (string, nullable)
  - `agent_id` (string, nullable)
  - `attributes` (object, nullable)
  - `references` (array of object, nullable)
    - `version` (string, nullable)
    - `slug` (string, nullable)
    - `id` (string (uuid), nullable)
    - `attributes` (object, nullable)
  - `links` (array of object, nullable)
    - `span_id` (string, nullable)
    - `trace_id` (string, nullable)
    - `attributes` (object, nullable)
  - `hashes` (array of object, nullable)
    - `id` (string (uuid), nullable)
    - `attributes` (object, nullable)
  - `exception` (object, nullable)
  - `events` (array of object, nullable)
    - `name` (string, required)
    - `timestamp` (string (date-time) | integer, required)
    - `attributes` (object, nullable)
- `traces` (object, nullable): Nested tree of spans keyed by `trace_id` → span name, populated when the query was run with `focus="trace"` (default).
- `windowing` (object, nullable): Windowing of the next page. Present when this page is full (it holds `windowing.limit` spans, or traces with `focus="trace"`); send it as `windowing` in the next request. When the last page is full, the page after it is empty.
  - `newest` (string (date-time), nullable)
  - `oldest` (string (date-time), nullable)
  - `next` (string (uuid), nullable)
  - `limit` (integer, nullable)
  - `order` (string, one of: "ascending", "descending", nullable)
  - `interval` (integer, nullable)
  - `rate` (number, nullable)

### 422: Validation Error

- `detail` (array of object)
  - `loc` (array of string | integer, required)
  - `msg` (string, required)
  - `type` (string, required)
  - `input` (any)
  - `ctx` (Context)
