Query API
The query API answers questions the dashboard does not ask for you: cost by a custom attribute, latency by route and day, which tools fail most, what slow LLM spans have in common. You send one structured request, a JSON object naming a dataset, a kind of query and its members, and get rows back. It is not SQL: every request is validated and compiled by the server, and a request can only read your own workspace.
Two datasets
Section titled “Two datasets”| Dataset | One row per | Use it for |
|---|---|---|
calls |
Stored LLM call, as usage and billing count them | Spend, tokens, latency and errors |
spans |
Span of a trace: LLM, agent, tool and other steps, deduplicated | Structure: which tools and agents ran, how long they took, how they ended |
Both are content-free: no prompt, output or tool argument text can be read.
Custom attributes are fields too: metadata.<key> on calls (the metadata the
SDK attaches), attr.<key> and attr_num.<key> on spans (string and numeric
span attributes). After an export is sent twice, calls counts each copy, as
usage does, and spans counts the span once.
Four kinds of query
Section titled “Four kinds of query”kind |
Answers | Members |
|---|---|---|
aggregate |
Measures, optionally grouped by up to three fields (at most one custom attribute) and time at a granularity |
aggregations, group_by, order_by, limit |
heatmap |
Counts or sums over time and one numeric axis, on log2 buckets | x, y, value |
compare |
Which values are over-represented in a selection against a baseline | selection, baseline, dimensions, top_values |
rows |
Individual rows for drill-down, paged with a cursor | fields, order_by, limit, cursor |
Every request also has a window (days from 1 to 90, or an exact since
and until; rows at most 7 days) and may have filters (eq, neq, in,
not_in, gt, gte, lt, lte, exists, missing, starts_with, and
one level of any_of). neq and not_in keep rows that have no value; add
exists to drop them.
The schema tool lists every field with its type, operators and whether it can be grouped, aggregated or used as a heatmap axis, so an agent can write a valid request without guessing. It also lists the custom keys most used in your workspace: names only, from rows your key may read. Key names are whatever your instrumentation sent, so they appear there as they are. The list is refreshed every few minutes, so a name can still appear for up to 10 minutes after the data that carried it is deleted.
The tools
Section titled “The tools”| MCP tool | HTTP | Does |
|---|---|---|
metergraph_get_schema |
GET /v1/agent/query/schema |
Datasets, fields, limits, budgets, the earliest readable time, the custom keys most used in the last 7 days, and the samples below |
metergraph_run_query |
POST /v1/agent/query/run |
Runs one request and returns its rows |
metergraph_explain_query |
POST /v1/agent/query/explain |
Checks a request without running it: the normalized request, the plan, the budget, and a coarse ceiling on the rows it would read |
They need a Metadata key (or a Debug or Replay key, which include Metadata
access) and are read-only. The arguments of metergraph_run_query and
metergraph_explain_query are the request itself; over HTTP it is the body,
sent as application/json. The answer is the same over MCP, HTTP and the CLI.
From the CLI, with a Metadata sign-in:
metergraph tools call metergraph_get_schema --jsonmetergraph tools call metergraph_run_query --args-file query.json --jsonThe answer
Section titled “The answer”A run answer has the window it read (since, until, and clamped when
your retention cut it short), columns with their types and units, rows,
totals for a grouped aggregate (over every group, not only those returned),
page (truncated, and next_cursor for rows: send the same request with
cursor for the next page; a cursor lasts 24 hours), evidence and
warnings. Percentiles are approximate.
Budgets and limits
Section titled “Budgets and limits”Each query has a budget: up to 5 seconds and 200 million rows read, or 10
seconds and 500 million rows for a compare and windows over 30 days (over 7
days for spans). A query over its budget fails with
query_budget_exceeded and a reason (rows, bytes, memory, time,
groups, result or quota): narrow the window, add filters or group by
fewer values. By default a workspace’s dashboard runs 60 queries a minute,
its keys and OAuth connections together another 60, each key at most 30.
One dashboard query and one query by your keys can run at once; the schema
tool gives the figures for your caller. Over a limit the answer is rate_limited with
Retry-After. Identical requests are answered from a short-lived cache.
Errors
Section titled “Errors”A refusal is an error document with a stable code, a message, and where
they apply a pointer to the part of the request at fault (/filters/0/field,
say), a reason, and details.
| Code | Means |
|---|---|
invalid_argument |
The request is malformed, names an unknown field, or is over a limit (details.limit names it) |
forbidden |
A filter names an environment your key cannot read |
unsupported_capability |
The query API is not turned on for this workspace |
query_budget_exceeded |
The query is over its budget |
rate_limited |
Too many queries, or the store is busy; retry after Retry-After |
store_unavailable |
The analytics store cannot answer now; retry after Retry-After |
Keys limited to some environments
Section titled “Keys limited to some environments”A key limited to some environments can use the tools. It reads only rows of
those environments, never untagged rows, and rows returns only traces whose
every row is in them. It is not shown read estimates or which budget a query
exceeded, because both would count rows outside its environments.
Samples
Section titled “Samples”Each of these is a complete request. metergraph_get_schema returns them too.
Cost and calls by model, last 7 days
Section titled “Cost and calls by model, last 7 days”{ "dataset": "calls", "kind": "aggregate", "window": { "days": 7 }, "group_by": [ { "field": "model" } ], "aggregations": [ { "op": "sum", "field": "cost_usd", "as": "cost" }, { "op": "count", "as": "calls" } ], "order_by": [ { "by": "cost", "direction": "desc" } ], "limit": 20}p50 and p95 latency per route per day
Section titled “p50 and p95 latency per route per day”{ "dataset": "calls", "kind": "aggregate", "window": { "days": 14 }, "group_by": [ { "field": "route" }, { "field": "time", "granularity": "1d" } ], "aggregations": [ { "op": "p50", "field": "duration_ms", "as": "p50_ms" }, { "op": "p95", "field": "duration_ms", "as": "p95_ms" } ]}Error rate by provider and error type
Section titled “Error rate by provider and error type”{ "dataset": "calls", "kind": "aggregate", "window": { "days": 7 }, "group_by": [ { "field": "provider" }, { "field": "error_type" } ], "aggregations": [ { "op": "rate", "field": "is_error", "as": "error_rate" }, { "op": "count", "as": "calls" } ]}Tool spans by tool name and status
Section titled “Tool spans by tool name and status”{ "dataset": "spans", "kind": "aggregate", "window": { "days": 7 }, "filters": [ { "field": "span_kind", "op": "eq", "value": "tool" } ], "group_by": [ { "field": "name" }, { "field": "status" } ], "aggregations": [ { "op": "count", "as": "spans" }, { "op": "p95", "field": "duration_ms", "as": "p95_ms" } ]}Cost by a custom attribute, from calls, the billing-consistent dataset
Section titled “Cost by a custom attribute, from calls, the billing-consistent dataset”{ "dataset": "calls", "kind": "aggregate", "window": { "days": 30 }, "group_by": [ { "field": "metadata.customer_tier" } ], "aggregations": [ { "op": "sum", "field": "cost_usd", "as": "cost" } ]}Latency heatmap for one route
Section titled “Latency heatmap for one route”{ "dataset": "calls", "kind": "heatmap", "window": { "days": 7 }, "filters": [ { "field": "route", "op": "eq", "value": "support-triage" } ], "x": { "field": "time", "buckets": 84 }, "y": { "field": "duration_ms", "scale": "log2", "min": 16, "max": 65536 }, "value": { "op": "count" }}What is different about slow LLM spans
Section titled “What is different about slow LLM spans”{ "dataset": "spans", "kind": "compare", "window": { "days": 7 }, "filters": [ { "field": "span_kind", "op": "eq", "value": "llm" } ], "selection": [ { "field": "duration_ms", "op": "gte", "value": 5000 } ], "baseline": [ { "field": "duration_ms", "op": "lt", "value": 5000 } ], "dimensions": [ "model", "provider", "route", "environment", "attr.metergraph.attr.region" ], "top_values": 10}The 50 most expensive calls yesterday, for drill-down
Section titled “The 50 most expensive calls yesterday, for drill-down”{ "dataset": "calls", "kind": "rows", "window": { "days": 1 }, "fields": [ "time", "trace_id", "route", "model", "cost_usd", "duration_ms" ], "order_by": [ { "by": "cost_usd", "direction": "desc" } ], "limit": 50}Root spans (spans with no parent) by service, with latency
Section titled “Root spans (spans with no parent) by service, with latency”{ "dataset": "spans", "kind": "aggregate", "window": { "days": 7 }, "filters": [ { "field": "parent_span_id", "op": "missing" } ], "group_by": [ { "field": "service_name" } ], "aggregations": [ { "op": "count", "as": "root_spans" }, { "op": "p95", "field": "duration_ms", "as": "p95_ms" } ]}