What is LangWatchQL
LangWatchQL is the ClickHouse SQL dialect that LangWatch accepts over its analytics views. You send one SELECT statement, optionally with a WITH clause, and you get back columns and rows. The database runs it with readonly = 1, so a statement cannot change your data.
Name a view without a database, for example traces or trace_metrics_by_minute, as the examples in this guide do. A bare name resolves to the database your deployment serves, so the same statement runs on LangWatch Cloud and on a self-hosted deployment. Your deployment's own prefix also works (langwatch.traces on LangWatch Cloud), but any other prefix is refused.
A self-hosted deployment serves the database named in its CLICKHOUSE_URL. LangWatchQL is unavailable when CLICKHOUSE_URL has no database in it. The full list, with every column and the permission it needs, is in the Query API reference, and GET /api/v1/query/schema returns the list your key can query today.
What is LangWatchQL for
Use LangWatchQL for a join across views, a percentile on one group of traces, rows exported to a file, or a question an agent asks from code. Dashboards read their data with LangWatchQL too: each widget runs named LangWatchQL statements.
For finding individual traces with their spans and evaluations, use the trace filter instead. Querying your data compares the two languages.
How do I run a query
You can send the same statement from REST, the CLI or MCP, and each one needs the analytics:view permission. REST and the CLI sign in with an API key, while the hosted MCP server also accepts OAuth sign-in. See MCP.
POST /api/v1/query takes the statement in the sql field and answers with columns, rows, statistics and diagnostics.
curl -X POST https://app.langwatch.ai/api/v1/query \ -H "X-Auth-Token: $LANGWATCH_API_KEY" \ -H "Content-Type: application/json" \ -d '{"sql": "SELECT TraceId, TraceName, OccurredAt, TotalDurationMs FROM traces WHERE OccurredAt >= subtractDays(now(), 1) AND ContainsErrorStatus ORDER BY OccurredAt DESC LIMIT 100"}'A key that reaches several projects must name them: send projects, a list of ids or "all", or projectId for one. Without it the request is refused with 422 projects_required. Request fields, auth headers and status codes are in Run a LangWatchQL query.
langwatch query runs a statement and prints a table. Add --format json, jsonl or csv for other formats, and --out to write a file.
npm install -g langwatchlangwatch loginlangwatch query --projects <project id> "SELECT TraceId, TraceName, OccurredAt, TotalDurationMsFROM tracesWHERE OccurredAt >= subtractDays(now(), 1) AND ContainsErrorStatusORDER BY OccurredAt DESCLIMIT 100"| Command | What it does |
|---|---|
langwatch query --sql-file <path> | Reads the statement from a file |
langwatch query --project <id>, --projects <id,id>, --projects all | Names the projects to read. A key that reaches several projects must name them |
langwatch query --param key=value | Binds a {key:Type} parameter; repeat it for each one |
langwatch query --start <datetime> --end <datetime> | Fills the reserved period parameters |
langwatch query --page-by keyset | Pages through a large result, see Limits and cost |
langwatch query schema | Lists the views and columns your key can query |
langwatch query examples | Prints the worked examples, filter them with --tag |
langwatch query reference | Prints the full language reference |
langwatch analytics query is a different command for timeseries charts. It does not take LangWatchQL.
The LangWatch MCP server gives your coding agent two tools for LangWatchQL:
| Tool | What it does |
|---|---|
discover_schema | With category: "lwql", returns the views, columns and functions as markdown |
run_query | Runs a statement, with optional parameters and projects (project ids or "all", needed when the key reaches several projects), and renders up to 50 rows as a table with the real row count, the time it took and any diagnostics |
Ask your agent a question such as "which model cost the most last week" and it can look up the schema, write the statement and run it.