# LangWatchQL

Read-only SQL over your traces, spans, evaluations, simulations and coding-agent sessions, from the REST API, the CLI or an MCP client.

> Page: https://langwatch.ai/docs/features/langwatchql/overview
> Source: https://github.com/langwatch/langwatch/blob/main/docs/features/langwatchql/overview.mdx
> Index of all pages: https://langwatch.ai/docs/llms.txt

## 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](https://langwatch.ai/docs/api-reference/query/overview#queryable-views), 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](https://langwatch.ai/docs/features/dashboards#how-the-data-is-read) 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](https://langwatch.ai/docs/features/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](https://langwatch.ai/docs/integration/mcp).

**REST**

`POST /api/v1/query` takes the statement in the `sql` field and answers with `columns`, `rows`, `statistics` and `diagnostics`.

```bash
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](https://langwatch.ai/docs/api-reference/query/run-a-langwatchql-query).

**CLI**

`langwatch query` runs a statement and prints a table. Add `--format json`, `jsonl` or `csv` for other formats, and `--out` to write a file.

```bash
npm install -g langwatch
langwatch login
langwatch query --projects <project id> "SELECT TraceId, TraceName, OccurredAt, TotalDurationMs
FROM traces
WHERE OccurredAt >= subtractDays(now(), 1)
  AND ContainsErrorStatus
ORDER BY OccurredAt DESC
LIMIT 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](https://langwatch.ai/docs/features/langwatchql/limits-and-cost#how-do-i-get-more-than-10000-rows) |
| `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.

**MCP**

The [LangWatch MCP server](https://langwatch.ai/docs/integration/mcp) 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.

## Where to go next

- [Your first queries](https://langwatch.ai/docs/features/langwatchql/first-query): One worked query for each main view
- [Read and judge conversations](https://langwatch.ai/docs/features/langwatchql/extraction-functions): Pull transcripts and model calls into a result, and ask a question of each one
- [Limits and cost](https://langwatch.ai/docs/features/langwatchql/limits-and-cost): Row cap, timeouts, rate limits per plan and what is charged
- [Common questions](https://langwatch.ai/docs/features/langwatchql/common-questions): The error messages you can get and how to fix each one

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.
