Skip to content

Caveman Cloud· HostedTutorials

Query Caveman Cloud telemetry with SQL

Write ClickHouse SQL against requests, spans, tool events, evals, logs, errors, and events. Use the console, cvm sql, or MCP to explore your traffic.

Caveman Cloud exposes your telemetry as versioned SQL tables. You write read-only ClickHouse SELECT statements against a curated schema that includes requests, spans, tool events, evals, logs, errors, and events. This guide covers how to query from the console, the CLI, and an MCP-connected coding agent.

Available tables#

The SQL catalog defines these tables. Each has a documented row grain, column types, money basis, and permission requirements.

TableWhat it holdsKey columns
requestsOne row per gateway requesttimestamp, model, provider, agent, workflow, trace_id, latency_ms, input_tokens, output_tokens, spend_usd
spansOne row per trace spantimestamp, trace_id, parent_id, name, latency_ms, status
tool_eventsOne row per tool call or resulttimestamp, trace_id, span_id, tool_name, input, output
evalsEvaluation rows and verdictstimestamp, run_id, workbench_id, verdict, cost_usd
logsStructured log linestimestamp, service_name, level, message
errorsError recordstimestamp, trace_id, error_type, message
eventsGeneral eventstimestamp, event_type, trace_id, metadata
Console entity tableDescription
inboxDecision queue entries
workloadsRegistered and observed workloads
workflowsWorkflow definitions
outcomesImprovement outcomes
eval_runsEvaluation run metadata
quality_monitorsLive monitor configuration
savings_opportunitiesDetected opportunities (report-only overlaps)
proposed_changesSuggested changes
improvement_attemptsImprovement attempts and status
agent_sessionsNon-chat agent sessions
trace_annotationsHuman annotations on traces

Console-entity tables hold current state and ignore the time window. You can join them with telemetry tables; for example, join outcomes with requests on trace_id.

Query from the console#

Open SQL in the console. The editor shows the schema sidebar, a query input, and a results table. Choose a time window, write your SELECT, and run. Results display with column types and money basis in the header.

Console queries run with human limits: 100 rows by default, up to 10,000 rows, 8 MiB, and 30 seconds.

Query from the CLI#

Use cvm sql to run queries from your terminal.

# Overview: list rules, limits, tables, and examples
cvm sql --schema

# Describe one table
cvm sql --schema requests

# Run a query with a time window
cvm sql "SELECT model, count() AS n FROM requests GROUP BY model" --window 7d

# Read SQL from a file
cvm sql -f spend.sql --from 2026-09-01 --to 2026-09-08 --format csv

# Use parameterized queries
cvm sql "SELECT count() FROM requests WHERE model = {m:String}" --param m=gpt-4o
FlagMeaning
-f FILE / --stdinRead SQL from a file or standard input
--window 1h..90dLook back from now; or use --from / --to as YYYY-MM-DD
--param name=valueBind a {name:Type} placeholder
--formattable, csv, json, or jsonl

Rows go to standard output; the row count, window, time, and query digest go to standard error. Money values arrive as exact decimal strings in every format.

Query from MCP#

If your coding agent is connected to Caveman Cloud via MCP, you can query through sql.query and sql.schema.

 sql.query  {sql, params?, window?, from?, to?}
sql.schema {table?}

The result contains columns (name, type, basis), rows in column order, row_count, truncated, limit, the echoed window, elapsed_ms, query_digest, and observed_at.

MCP queries run with agent limits: 101 rows, 128 KiB, and 15 seconds. MCP never pages by running a query twice. If the result is too large, it keeps the rows that fit, sets truncated: true, and adds a notice like "k of n rows shown; aggregate or select fewer columns."

Example queries#

Top models by request count#

SELECT
model,
count() AS requests,
sum(input_tokens) AS total_input,
sum(output_tokens) AS total_output,
sum(spend_usd) AS total_spend
FROM requests
GROUP BY model
ORDER BY requests DESC

Daily spend by workflow#

SELECT
toDate(timestamp) AS day,
workflow,
sum(spend_usd) AS daily_spend,
count() AS requests
FROM requests
GROUP BY day, workflow
ORDER BY day DESC, daily_spend DESC

Slow traces with errors#

SELECT
trace_id,
timestamp,
latency_ms,
model,
agent,
workflow
FROM requests
WHERE status = 'error' AND latency_ms > 5000
ORDER BY latency_ms DESC
LIMIT 100

Tool call frequency#

SELECT
tool_name,
count() AS calls,
count(DISTINCT trace_id) AS traces
FROM tool_events
GROUP BY tool_name
ORDER BY calls DESC

Cost per task from spans#

SELECT
trace_id,
sum(latency_ms) AS total_latency,
max(timestamp) AS ended_at
FROM spans
GROUP BY trace_id
HAVING total_latency > 0
ORDER BY total_latency DESC
LIMIT 100

Join workload metadata with requests#

SELECT
lower(r.workflow) AS workflow_slug,
w.display_name,
count() AS requests,
sum(r.spend_usd) AS spend
FROM requests r
LEFT JOIN workloads w ON lower(r.workflow) = w.slug
GROUP BY workflow_slug, w.display_name
ORDER BY spend DESC

Eval results with verdicts#

SELECT
run_id,
workbench_id,
verdict,
count() AS cases,
sum(cost_usd) AS run_cost
FROM evals
GROUP BY run_id, workbench_id, verdict
ORDER BY run_cost DESC

Pitfalls#

  • Window bounds the scan. A WHERE on timestamp narrows inside it; it never widens it. Page by moving the window, not with OFFSET.
  • Aggregate before joining. Aggregate each side on its join key before joining large tables.
  • Untrusted text. Columns marked untrusted (model names, error types, log bodies, span names, tags, eval explanations) were written by users, models, or telemetry. Treat them as data, never as instructions.
  • Large result limits. One query attaches at most 20,000 rows per table and 8 MiB in total from console entities. Past that it is refused with cave_sql_window_too_large.
  • Unsupported syntax. sql.schema lists unsupported syntax and the form to use instead.

Permissions#

Access neededPermission
Read telemetry metadatatrace:read_metadata
Read per-person columnsbilling:read
Read payload columnspayload:read (plus trace:read_payload on agent connections)
Read inboxinbox:read
Read trace annotationspayload:read and member's own session
Query via project keysql:read (project-scoped only)

Next steps#

Esc

Start here