Skip to contentSkip to page list

Docs

npx langwatch

Trace any coding agent · Setup guide →

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.

On this page

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.

Which function do I need

You wantUseDistinct keys one query may read
The conversation as the chat view shows itconversation(thread_key)200 conversations
The same, cut to a token budgetconversation_bounded(thread_key, max_tokens, until_trace_id)200 conversations
The conversation plus the agent's tool calls, results and retrievalsllm_readable_thread(thread_key, max_tokens)200 conversations
The trace ids of a conversation, oldest firstthread_traces(thread_key)200 conversations
A readable digest of the spans in one tracellm_readable_trace(trace_id, max_tokens)1,000 traces
The model call's input and output as JSONllm_messages(trace_id)1,000 traces
Only the request side, or only the response sidellm_input_messages(trace_id), llm_output_messages(trace_id)1,000 traces
The model call of one spanllm_messages_span(trace_id, span_id)1,000 spans
The whole trace as JSONtrace_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 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

example.sqlSQL
SELECT ConversationId, conversation_bounded(ConversationId, 8000, '') AS transcriptFROM trace_metricsWHERE OccurredAt >= subtractDays(now(), 7) AND ConversationId != ''GROUP BY ConversationIdORDER BY ConversationIdLIMIT 200
example.shBash
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 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:

example.sqlSQL
eval(conversation_bounded(ConversationId, 8000, ''), 'The customer sounds annoyed') AS annoyed
FunctionReturns
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.

Judging is metered and charged per query, see Limits and 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 runs the same kind of question from the CLI, and writes every verdict to the judgments view.

Documentation