# Fetch Analytics

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

`POST /tracing/analytics/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.

:::

Aggregate span metrics into time buckets.

Runs filtering and windowing identical to `POST /tracing/spans/query`,
then bucketizes the matched spans by time and computes one or more
metric summaries per bucket. Use this to build charts of latency,
cost, token usage, or custom numeric and categorical attributes.

## Request body

- `filtering` — same shape as the query endpoint, scoped to the spans
  that contribute to the analytics.
- `windowing` — `oldest`/`newest` for the time range (matched against
  each span's `start_time`), `interval` for bucket width (in minutes),
  and an optional `rate` to sample a share of the traces. Figures
  describe the sample only. When `interval` is set, buckets with no spans are returned empty.
- `formatting.focus` — `trace` (default) summarizes root spans only,
  which carry the cumulative metrics of their trace. `span`
  summarizes every span.
- `specs` — a list of `MetricSpec` entries describing which
  attributes to summarize and how. Each spec declares a `type`
  (`numeric/continuous`, `numeric/discrete`, `binary`,
  `categorical/single`, `categorical/multiple`, `string`, `json`,
  or `*` for auto) and a dotted `path` into the span (for example
  `attributes.ag.metrics.costs.cumulative.total`).

## Response

Buckets are returned in chronological order. Each bucket carries a
`metrics` dict keyed by spec path. See [Tracing — the ag.*
namespace](https://agenta.ai/docs/reference/api-guide/tracing.md#the-ag-attribute-namespace)
for the cumulative/incremental metric layout on each span.

## Errors

- `504` — the query ran past the database statement timeout. Retry
  with a shorter time range. Other failures still return an empty
  result.

## 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)
- `interval` (in query, integer)
- `rate` (in query, number)
- `filter` (in query, any)
- `specs` (in query, any)

## Response schemas

### 200: Successful Response

- `count` (integer): Number of time buckets returned. Default: `0`.
- `buckets` (array of object): Time-bucketed aggregates. Each bucket's `metrics` dict is keyed by the dotted `path` of the corresponding `MetricSpec`, ordered oldest to newest. Default: `[]`.
  - `timestamp` (string (date-time), required)
  - `interval` (integer, required)
  - `metrics` (object, nullable)
- `query` (object): The resolved query used to compute the buckets.
  - `formatting` (object, nullable)
    - `focus` (string, one of: "trace", "span", nullable)
    - `format` (string, one of: "agenta", "opentelemetry", nullable)
  - `windowing` (object, nullable)
    - `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)
  - `filtering` (object, nullable)
    - `operator` (string, one of: "and", "or", "not", "nand", "nor") Default: `"and"`.
    - `conditions` (array of object | Filtering (recursive)) Default: `[]`.
- `specs` (array of object): The resolved metric specs applied in each bucket. Default: `[]`.
  - `type` (string, one of: "numeric/continuous", "numeric/discrete", "binary", "categorical/single", "categorical/multiple", "string", "json", "none", "*") Default: `"none"`.
  - `path` (string) Default: `"*"`.
  - `bins` (integer, nullable)
  - `vmin` (number, nullable)
  - `vmax` (number, nullable)
  - `edge` (boolean, nullable)
- … more fields: see the OpenAPI specification.

### 422: Validation Error

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

### 504: The analytics query ran past the database statement timeout. `detail` carries a message; retry with a shorter time range.
