Query your telemetry: filters, limits, and complete answers
The analytics API answers questions about your own AI calls: what they cost, how many tokens they used, which ones failed, broken down by any tag you send. It returns metadata only (names, numbers, hashes, identifiers), never prompts or outputs.
This page explains how to filter so that your answers are fast and complete.
- Endpoint:
POST https://api.modelrig.ai/v1/analytics/query, with arig_sk_key that has thequeryscope. Results are always limited to your key's org. - Self-description:
GET /v1/analytics/metalists every column, which operators it supports, and the scan budget. Agents should read it first. - CLI:
npx modelrig query "<query>", usingMODELRIG_API_KEY. - Discovery:
GET /v1/analytics/tagslists the tags your calls actually carry. Start here if you're not sure how your calls are tagged (§2).
1. A query in one minute
SELECT tag.step, count(*), sum(cost), sum(tokens_out)
FROM inferences
WHERE ts >= '2026-10-24T00:00:00Z' AND tag.project = 'report_generation'
GROUP BY tag.step
curl -s https://api.modelrig.ai/v1/analytics/query \
-H "authorization: Bearer $MODELRIG_API_KEY" -H 'content-type: application/json' \
-d '{"query":"SELECT tag.step, count(*), sum(cost) FROM inferences WHERE ts >= '\''2026-10-24T00:00:00Z'\'' AND tag.project = '\''report_generation'\'' GROUP BY tag.step"}'
The response:
{
"rows": [ { "step": "weighted_tfv", "count": 40, "sum_cost": 3.93 }, … ],
"scan_complete": true, // ← check this first
"scanned_rows": 4904,
"scan_stop": "complete",
"truncated": false,
"filters": [
{ "column": "ts", "op": ">=", "applied": "database" },
{ "column": "tag.project", "op": "=", "applied": "database", "missing_rows": 0 }
],
"limit": 1000
}
2. Start here: see what your calls carry
You can't filter on a tag you don't know about. Ask first:
curl -s "https://api.modelrig.ai/v1/analytics/tags?since=2026-10-29T00:00:00Z" \
-H "authorization: Bearer $MODELRIG_API_KEY"
{
"keys": [
{ "key": "project", "rows": 41250, "distinct_values": 3,
"top_values": [ { "value": "news_ingest", "rows": 36880 },
{ "value": "report_generation", "rows": 3970 },
{ "value": "market_news", "rows": 400 } ],
"redacted_values": 0 },
{ "key": "step", "rows": 3970, "distinct_values": 14, "top_values": [ … ] }
],
"scanned_rows": 20000, "scan_complete": false, "scan_stop": "row_budget"
}
- It shows each tag key, how many calls carry it, and its most common values (up to 20).
- On a busy org it describes your newest calls (
scan_complete: false). That's enough to learn your tag names and values. - If a query comes back empty, the response may carry hints, such as
"did_you_mean": ["tag.project"]when you typedtag.projct, or the correctly spelled value when the case differs (§7). ModelRig never guesses for you: a typo returns no rows plus a hint, never someone else's rows.
3. Why filters matter: the scan budget
ModelRig reads your calls newest first and adds them up. Each query may read up to 20,000 calls in up to 10 seconds (the scan budget; see scan_budget in /meta).
- If every call that matches your filters fits in the budget, the answer is exact, and
scan_completeistrue. - If not, ModelRig stops and tells you.
scan_completeisfalse,truncatedistrue,scan_stopsays which limit was hit, andfix_hintsuggests a fix. In that case the answer covers only the newestscanned_rowscalls. Do not report it as a total.
Filters are how you stay inside the budget. A filter applied in the database means calls that don't match are never read, so they cost nothing against the budget.
Example: an org sends 55,000 calls in 12 days, 90% of them from a news-ingest job. A 12-day question about the report pipeline, without a project filter, reads 20,000 news calls, stops, and returnsscan_complete: false. Withtag.project = 'report_generation', it reads only that project's 4,904 calls and returns a complete, exact answer.
4. Database filters and after-read filters
Every condition in WHERE is applied one of two ways. The response's filters list shows which:
applied | What happens | Effect on the budget |
|---|---|---|
database | Non-matching calls are never read | Narrows the scan. This is what you want for big windows |
after_read | ModelRig reads the calls, then drops non-matching ones | Still correct, but the dropped calls count against the 20,000 |
Both are always correct. A condition that runs in the database is also re-checked after reading, so the answer is identical either way. The only difference is how much of the budget you spend.
Which filters run in the database
| You filter on | Database operators |
|---|---|
ts | = != > >= < <= |
route, provider, model, failure_class, served_variant, served_tier | = IN IS NULL IS NOT NULL |
any tag (tag.<name>, and subject, feature, user, session, cost_center) | = != IN NOT IN IS NULL IS NOT NULL |
tokens_in, tokens_out, tokens_cached, latency_ms, ttfb_ms | = > >= < <= (number literals), IS NULL, IS NOT NULL |
cost | > >= < <= (number literals), IS NULL, IS NOT NULL |
meta.finish_reason, meta.model_snapshot, meta.context_bucket, meta.error_code, meta.region, meta.input_hash, meta.output_hash | = IN IS NULL IS NOT NULL |
Everything else runs after reading:
!=andNOT INon the plain columns and metrics (so that calls with no value can be included, §7);- range comparisons (
>,<, …) on text, tags andmeta.*fields; - numeric
meta.fields such asmeta.reasoning_tokens,meta.http_statusandmeta.cost.; - booleans such as
meta.refusal.
GET /v1/analytics/meta lists the exact operators per column, under filter.database and filter.after_read.
A string value runs in the database only when it is a plain token:
- letters, digits and
_ . : @ / + -; - 128 characters at most;
- not a number;
- not
trueorfalse.
So tag.project = 'report_generation' runs in the database, while tag.batch = '42' runs after reading. The answer is still correct; it just costs budget, because '42' also matches a stored number 42.
5. The filter language
WHERE condition AND condition AND …
condition := column = 'text' | column = 123
| column != … | > | >= | < | <=
| column IN ('a', 'b', …) -- 1 to 100 values, all text or all numbers
| column NOT IN ('a', 'b', …)
| column IS NULL
| column IS NOT NULL
- Conditions are joined with
AND. There is noOR; useINfor "any of these values". - Text goes in single quotes. Double a quote to escape it:
'it''s'. tstakes an ISO-8601 timestamp:'2026-11-01T00:00:00Z'.- Columns:
route,provider,model,ts,failure_class,served_variant,served_tier; the reserved tagssubject,feature,user,session,cost_center; any other tag astag.<name>; themeta.*fields; and the metricscost,tokens_in,tokens_out,tokens_cached,latency_ms,ttfb_ms.
6. Recipes
One project, last 7 days, by step:
SELECT tag.step, count(*), sum(cost) FROM inferences
WHERE ts >= '2026-10-29T00:00:00Z' AND tag.project = 'report_generation'
GROUP BY tag.step
One run, every call:
SELECT tag.step, model, tokens_in, tokens_out, cost FROM inferences
WHERE tag.run_id = 'run_8f2c' LIMIT 500
Several models at once (in place of OR):
SELECT model, count(*), avg(latency_ms) FROM inferences
WHERE ts >= '2026-11-01T00:00:00Z' AND model IN ('gemini-3.1-pro-preview', 'gpt-5.4')
GROUP BY model
Failures only: failure_class is empty on success.
SELECT route, failure_class, count(*) FROM inferences
WHERE ts >= '2026-11-01T00:00:00Z' AND failure_class IS NOT NULL
GROUP BY route, failure_class
Successes only:
SELECT route, count(*), sum(cost) FROM inferences
WHERE ts >= '2026-11-01T00:00:00Z' AND failure_class IS NULL
GROUP BY route
Everything except one noisy job (untagged calls are included; §7):
SELECT tag.project, sum(cost) FROM inferences
WHERE ts >= '2026-11-01T00:00:00Z' AND tag.project NOT IN ('news_ingest')
GROUP BY tag.project
Calls you forgot to tag:
SELECT route, count(*) FROM inferences
WHERE ts >= '2026-11-01T00:00:00Z' AND tag.project IS NULL
GROUP BY route
Expensive calls (cost > runs in the database):
SELECT route, model, count(*), sum(cost) FROM inferences
WHERE ts >= '2026-11-01T00:00:00Z' AND cost > 0.05
GROUP BY route, model
Heavy reasoning in one project. meta.reasoning_tokens > runs after reading, so pair it with a database filter:
SELECT tag.step, count(*) FROM inferences
WHERE ts >= '2026-11-01T00:00:00Z' AND tag.project = 'report_generation'
AND meta.reasoning_tokens > 20000
GROUP BY tag.step
7. Missing values
A call may have no value for a column: the tag wasn't sent, or a field is empty. ModelRig handles that the way you'd mean it:
| Your condition | Calls with no value… |
|---|---|
tag.project = 'x', IN (…) | are not included (they aren't x) |
tag.project != 'x', NOT IN (…) | are included ("everything except x" includes calls that aren't x because they have no tag) |
>, >=, <, <= | are not included (no value isn't bigger or smaller than anything) |
IS NULL | only these |
IS NOT NULL | never these |
Every filter tells you how many calls had no value. Read missing_rows in the response's filters list. If tag.project != 'news_ingest' reports "missing_rows": 120, the answer includes 120 untagged calls. To leave them out, add AND tag.project IS NOT NULL.
The hosted API and the self-hosted /query behave the same way. A number written as text matches the number (tag.batch = 5 matches a stored "5"), and 'true'/'false' match true/false values.
Changed in modelrig 0.17.0:
- range filters (
<,>, …) no longer match calls with no value; earlier hosted versions sometimes did; - the self-hosted
/querynow includes calls with no value in!=andNOT IN, as the hosted API always did; - on both, text counts as a number only when it is plain decimal digits (
5,-3,0.25):"1e3"," 5","0x10"and".5"no longer match a number, sotag.k = 1000no longer matches a stored"1e3"; - on both, a tag whose value is a list or an object compares as its JSON text, so a stored
["abc"]no longer matches'abc'.
8. When an answer is incomplete or empty
Read scan_stop and fix_hint, then:
1. row_budget: more than 20,000 calls matched your database filters. Add a database filter: a tag equality (tag.project = …, tag.run_id = …), a route, or a model IN (…). Or narrow the ts window. Check filters to see whether a condition you thought was narrowing the scan actually ran after_read. 2. time_budget: the read took longer than 10 seconds. Narrow the window the same way. 3. limit_reached: a plain listing (no GROUP BY, no aggregate) already has LIMIT rows, the newest ones. Those rows are correct. Raise LIMIT (up to 10,000) or narrow the filter if you need more.
An empty answer (rows: []) may carry hints:
unknown_tag: no recent call carries that tag;did_you_meanlists close tag names.no_such_value: no recent call has that value;did_you_meanlists values that differ only in case, spaces or-/_.always_missing: the column has no value on any recent call.
Fix the filter and re-run. ModelRig does not apply the suggestion for you.
Need a total beyond the budget? Split the window (for example, one query per week). Add the parts only if every part came back scan_complete: true.
9. Make filtering easy: tag at ingest
Filters only find what you tagged. Send these tags on every call through the SDK's tags (the SDK adds run_id itself inside rig.run):
| Tag | Use |
|---|---|
project | one value per product area or pipeline. This is your most useful database filter |
step | the pipeline step |
run_id | one pipeline execution. The SDK stamps it inside rig.run |
subject, feature, user, session, cost_center | reserved dimensions with first-class columns |
Make tag values plain tokens (report_generation, not Report Generation), so they run in the database and group cleanly. A value with spaces, or one longer than 128 characters, is stored but never shown or matched, because analytics never returns free text.
10. Limits
| Limit | Value |
|---|---|
| Calls read per query | 20,000 (scan_budget.rows) |
| Time per query | 10 s (scan_budget.seconds) |
Result rows (LIMIT) | default 1,000, max 10,000 |
Values in IN / NOT IN | 100 |
| Text one condition sends to the database | 4 KB, encoded; a longer IN / NOT IN is still exact but runs after reading (its filters[].reason says so) |
| Queries per key | 60 per minute |
| Not supported | OR, parentheses, LIKE/prefix match, ORDER BY and count_distinct (planned) |