Skip to main content
Applies to:
  • Plan -
  • Deployment -

Summary

Goal: Query sibling traces by shared metadata fields to reconstruct multi-turn agent conversations inside a custom scorer. Features: SQL queries via the /btql API endpoint, metadata.thread_id filtering, subfield indexing, trace-scope scoring helpers.

Configuration steps

Step 1: Choose a tracing model

Two patterns are supported. Single-trace is preferred when possible. Option A — Single trace per conversation (recommended) Model each conversation as one Braintrust trace with multiple spans (one per turn). Use span.export() to continue the same trace across turns. Trace-scope helpers then work without any cross-trace queries.
Option B — Separate trace per turn If each HTTP request must produce its own trace, correlate traces via metadata.thread_id and query them with SQL.

Step 2: Set metadata.thread_id on every trace

Step 3: Query sibling traces via SQL inside a scorer

Custom Python scorers receive BRAINTRUST_API_KEY automatically. Use it to call /btql. Always bound the query with a time window and LIMIT to control latency and cost.
For EU organizations, set BRAINTRUST_API_URL to https://api-eu.braintrust.dev. For self-hosted deployments, set it to your Braintrust data-plane URL.

Step 4: Enable subfield indexing on metadata.thread_id

If metadata.thread_id is high-cardinality and queried frequently, enable subfield indexing in your project settings. This reduces lookup latency for that specific filter. Still pair with a time range and LIMIT — indexing speeds up lookups but doesn’t eliminate duplicated scorer work.

Step 5: Scope scoring to the final turn only

Avoid running cross-trace BTQL queries from every span scorer invocation. Run the full reconstruction only on the final turn, or use a batch/offline eval.

Version requirements