---
title: "Query Spans"
method: POST
path: "/spans/query"
tags: ["Traces"]
---

# Query Spans

`POST /spans/query`

Query spans as a flat list.

Thin wrapper over the shared span-query backend that forces
`focus = "span"`. Use this when you want a paged list of spans
regardless of trace hierarchy — for example, to surface all LLM
calls across traces or to stream spans into an external system.

## Request body

- `filtering` — span-level conditions (fields on `Span` and
  `attributes` paths).
- `windowing` — cursor pagination and time range (see
  [Query Pattern](/reference/api-guide/query-pattern#windowing)).
- `query_ref`, `query_variant_ref`, `query_revision_ref` — resolve
  filtering and windowing from a saved query revision. If the
  revision's stored `formatting.focus` is `trace`, this endpoint
  returns `409` — call `POST /traces/query` for that revision.

## Response

Returns `{count, spans}`. For the nested per-trace shape, call
`POST /traces/query` or `POST /tracing/spans/query` with
`focus="trace"` instead.

## Request body

- SpansQueryRequest — Request body for `POST /spans/query`.
  - `filtering` FilteringInput
    - `operator` 'and' | 'or' | 'not' | 'nand' | 'nor'
    - `conditions` union[]
      - union
        - Condition
          - `field` string, required
          - `key` string, nullable
          - `value` union
            - string
            - integer
            - number
            - boolean
            - unknown[]
              - …
            - object
          - `operator` union
            - 'is' | 'is_not'
            - 'eq' | 'neq' | 'gt' | 'lt' | 'gte' | 'lte' | 'btwn'
            - 'startswith' | 'endswith' | 'contains' | 'matches' | 'like'
            - 'in' | 'not_in'
            - 'has' | 'has_not'
            - 'exists' | 'not_exists'
          - `options` union
            - TextOptions
              - …
            - ListOptions
              - …
        - FilteringInput — recursive
  - `windowing` Windowing
    - `newest` string, date-time, nullable
    - `oldest` string, date-time, nullable
    - `next` string, uuid, nullable
    - `limit` integer, nullable
    - `order` 'ascending' | 'descending', nullable
    - `interval` integer, nullable
    - `rate` number, nullable
  - `query_ref` Reference
    - `version` string, nullable
    - `slug` string, nullable
    - `id` string, uuid, nullable
  - `query_variant_ref` Reference
    - `version` string, nullable
    - `slug` string, nullable
    - `id` string, uuid, nullable
  - `query_revision_ref` Reference
    - `version` string, nullable
    - `slug` string, nullable
    - `id` string, uuid, nullable

## Response `200`

Successful Response

- SpansResponse
  - `count` integer — Total number of matching spans in the window.
  - `spans` SpanOutput[], nullable — Flat list of matching spans.
    - `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` 'invocation' | 'annotation' | 'unknown'
    - `span_type` 'agent' | 'chain' | 'workflow' | 'task' | 'tool' | 'embedding' | 'query' | 'llm' | 'completion' | 'chat' | 'rerank' | 'unknown'
    - `span_kind` 'SPAN_KIND_UNSPECIFIED' | 'SPAN_KIND_INTERNAL' | 'SPAN_KIND_SERVER' | 'SPAN_KIND_CLIENT' | 'SPAN_KIND_PRODUCER' | 'SPAN_KIND_CONSUMER'
    - `span_name` string, nullable
    - `start_time` union
      - string, date-time
      - integer
    - `end_time` union
      - string, date-time
      - integer
    - `status_code` 'STATUS_CODE_UNSET' | 'STATUS_CODE_OK' | 'STATUS_CODE_ERROR'
    - `status_message` string, nullable
    - `session_id` string, nullable
    - `user_id` string, nullable
    - `agent_id` string, nullable
    - `attributes` object, nullable
    - `references` OTelReferenceOutput[], nullable
      - `version` string, nullable
      - `slug` string, nullable
      - `id` string, uuid, nullable
      - `attributes` object, nullable
    - `links` OTelLinkOutput[], nullable
      - `span_id` string, nullable
      - `trace_id` string, nullable
      - `attributes` object, nullable
    - `hashes` OTelHashOutput[], nullable
      - `id` string, uuid, nullable
      - `attributes` object, nullable
    - `exception` object, nullable
    - `events` OTelEventOutput[], nullable
      - `name` string, required
      - `timestamp` union, required
        - string, date-time
        - integer
      - `attributes` object, nullable

## Other responses

- `422` — Validation Error

---

[API](https://skmtc.net/agenta-ai/apis/agenta-api.md) · [All operations](https://skmtc.net/agenta-ai/apis/agenta-api/llms.txt) · [OpenAPI document](https://skmtc-service-staging.skmtc.workers.dev/v1/apis/agenta-ai/agenta-api/versions/d43a05d98a9b/schema)
