# LangWatchQL extraction and eval functions

Pull conversation transcripts, model calls and whole traces into a query result, and ask a question of each one with an eval function.

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

## What are extraction functions

An extraction function reads the content of a conversation, a trace or a span and puts it into a column of your result. For example, `conversation_bounded(ConversationId, 8000, '')` returns each conversation as a transcript of at most 8,000 tokens, so you can export a week of chats to a file with one query.

Write each one in the top-level `SELECT` list, with an alias (`AS transcript`). Anywhere else, or without an alias, the query is refused. The one exception is an eval function: its text argument may hold one extraction function, but not a second level, as in the `eval` example below. Exact signatures are in the [extraction functions reference](https://langwatch.ai/docs/api-reference/query/extraction-functions).

## Which function do I need

| You want | Use | Distinct keys one query may read |
| --- | --- | --- |
| The conversation as the chat view shows it | `conversation(thread_key)` | 200 conversations |
| The same, cut to a token budget | `conversation_bounded(thread_key, max_tokens, until_trace_id)` | 200 conversations |
| The conversation plus the agent's tool calls, results and retrievals | `llm_readable_thread(thread_key, max_tokens)` | 200 conversations |
| The trace ids of a conversation, oldest first | `thread_traces(thread_key)` | 200 conversations |
| A readable digest of the spans in one trace | `llm_readable_trace(trace_id, max_tokens)` | 1,000 traces |
| The model call's input and output as JSON | `llm_messages(trace_id)` | 1,000 traces |
| Only the request side, or only the response side | `llm_input_messages(trace_id)`, `llm_output_messages(trace_id)` | 1,000 traces |
| The model call of one span | `llm_messages_span(trace_id, span_id)` | 1,000 spans |
| The whole trace as JSON | `trace_json(trace_id)` | 1,000 traces |

The cap counts distinct conversations, traces or spans, not rows. A row that repeats a key does not count again, so a query grouping ten thousand rows onto 50 conversations reads 50. A query that needs more keys than the cap is refused with `lwql_app_function_key_cap`.

These functions return message content, so the project's [data privacy policy](https://langwatch.ai/docs/platform/data-privacy) must let you see both **Input** and **Output**. Two exceptions: `llm_input_messages` needs only **Input**, and `thread_traces` returns only ids and needs neither. Content set to **Restricted** is shown only to the audience of the rule, and **Dropped** content is never stored.

## Export a week of conversations

```sql
SELECT ConversationId, conversation_bounded(ConversationId, 8000, '') AS transcript
FROM trace_metrics
WHERE OccurredAt >= subtractDays(now(), 7) AND ConversationId != ''
GROUP BY ConversationId
ORDER BY ConversationId
LIMIT 200
```

```bash
langwatch query --sql-file transcripts.sql --projects <project id> --format jsonl --out transcripts.jsonl
```

`LIMIT 200` is the conversation cap, so a week with more conversations returns only the first 200 ids. To get the next 200, add `OFFSET 200` after the `LIMIT`, and write each page to its own file, because `--out` replaces the file. Replace `subtractDays(now(), 7)` with a fixed start time and add a fixed end time, so that conversations that arrive during the export do not shift the pages.

A page with fewer than 200 rows is the last page, unless it went over the [response size limit](https://langwatch.ai/docs/features/langwatchql/limits-and-cost#limits) and lost its trailing rows. `--format jsonl` holds rows only, so use `--format json` to see the diagnostics when you read large values. If a page has `APP_FUNCTION_RESULT_TRUNCATED`, add the number of rows it returned to the `OFFSET`, and go on.

## What are eval functions

An eval function asks a question in plain words of a text and returns the answer as a column. You can give it any text column, including the output of an extraction function, so one query can judge each conversation in a time window, up to the cap in the table above:

```sql
eval(conversation_bounded(ConversationId, 8000, ''), 'The customer sounds annoyed') AS annoyed
```

| Function | Returns |
| --- | --- |
| `eval(text, instructions)` | A probability from 0 to 1 |
| `eval_criteria(text, instructions, criteria)` | The same, with two entries saying what counts as yes and what does not |
| `eval_passed(text, instructions, threshold)` | 1 or 0 |
| `eval_score(text, instructions, min, max)` | A score on a scale of at most 10 levels |
| `eval_category(text, instructions, options)` | One of 2 to 255 options, each written `name: meaning` |
| `eval_category_probs(text, instructions, options)` | A JSON object of each option and its probability |

One query judges at most 1,000 distinct texts. If the text comes from an extraction function, that function's cap also applies, so a query that judges conversations reads at most 200. Eval functions need a run that reads one project, in an organization where Instant Evals is switched on: see [The eval function is not available to you](https://langwatch.ai/docs/features/langwatchql/common-questions#the-eval-function-is-not-available-to-you).

Judging is metered and charged per query, see [Limits and cost](https://langwatch.ai/docs/features/langwatchql/limits-and-cost#what-does-a-query-cost). A judged column is null for a row the judge skipped, and the response then includes the `INSTANT_EVAL_SKIPPED` diagnostic.

**Also check:** [Instant Evals](https://langwatch.ai/docs/features/instant-evals/overview) runs the same kind of question from the CLI, and writes every verdict to the `judgments` view.

> ## 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.
