Braintrust
Sync Braintrust logs, scores and expected answers into a dataset.
What syncs#
Each root span in a project's logs becomes one record. Its input and output become messages, scores carry over by name, and expected (from logFeedback or edited in the UI) becomes expected_output.
| Braintrust | grad record |
|---|---|
| Span ID | id (the merge key for late feedback) |
input / output | messages |
scores | scores |
expected | expected_output |
metadata (model, version, user) | metadata |
tags | metadata.tags |
Tag spans with the model and version#
import { initLogger } from "braintrust";
const logger = initLogger({ projectName: "support-bot" });
const spanId = await logger.traced(async (span) => {
const reply = "..."; // call the model
span.log({
input: question,
output: reply,
metadata: { model: "your-org/support-sft", version: "v42" },
tags: ["prod"],
});
return span.id; // send this to your client so feedback can reference it
});
Send feedback from production#
logFeedback attaches scores, an expected answer and a comment to the span:
logger.logFeedback({
id: spanId,
scores: { resolved: 0 },
expected: "Open Billing > Invoices and click Download.",
comment: "Pointed to the wrong menu",
});
In Python it is logger.log_feedback(id=span_id, scores={...}, expected=..., comment=...). Each write to expected replaces the previous one. Feedback logged later is picked up by the next sync (see How syncing works).
Connect it to grad#
Store the key once as a workspace secret (it reads the value from stdin and never returns it), then add a source to a dataset. The schedule is a cron expression; leave it out to sync only when you ask.
grad secrets set braintrust-prod # paste a Braintrust API key
grad sources add braintrust \
--dataset support-prod \
--host https://api.braintrust.dev \
--secret braintrust-prod \
--project support-bot \
--schedule "*/30 * * * *"
grad sources list
grad sources sync src_01J9X
How syncing works#
Each sync is incremental: it pulls traces added since the last sync, plus scores and corrections added to traces it already has, so feedback that arrives days later still counts. Records are matched by the provider's trace ID and the latest score wins. Braintrust bumps a span's transaction id on every feedback write, so a sync picks up every later edit, however old the span.
Syncs add dataset versions (a scheduled one at most every version_interval, 15 minutes by default; see Versions). Old versions never change, so a run keeps the exact data it pinned. If you edit old scores or change the mapping, grad sources sync src_01J9X --full (or source.sync(full=True)) rebuilds the dataset from the provider as a new version.
source.sync() returns the import job (call .wait() to block), or the sync already in progress; on the CLI, add --wait. A source syncs at most once a minute, and one that keeps failing backs off and then pauses; see When a sync fails.
A one-off grad import of an exported file is a snapshot: it never gets later feedback. Use a source for the feedback loop.
Filter what syncs#
Every source takes the shared filters, sample_rate and record caps. grad puts tags, name, model, environment, version, release (as metadata.*), metadata and from/to in the SQL query; user_id and session_id are matched after fetching.
Choose what to train on#
Records keep the provider's scores in scores and its model and version tags in metadata, so you can pick a slice before training. A filter runs on the server, costs nothing to re-run, and saves the slice as its own dataset:
from grad import Grad
client = Grad()
good = client.datasets.get("support-prod").filter(
scores={"resolved": ">=1"},
metadata={"version": "v42"},
name="support-prod-v42-good",
)
print(good.count())
A router can use the raw dataset as is: it learns from every score, good and bad.
Keys and hosts#
- Project (required): the project id or name whose logs to sync, for example
support-bot. Add one source per project. - Key: a Braintrust API key from your organization settings, for a user or service account that can read the project's logs.
- Hosts:
https://api.braintrust.dev(the default),https://api-eu.braintrust.dev(EU), or your self-hosted data plane URL. Hosts must be publichttps://URLs; for a server on a private network, see Private hosts.
Private hosts#
Sync workers only call public https:// hosts, so a provider key can't be pointed at an internal service by mistake. To sync from a provider on your own network, list its address range in GRAD_SOURCES_EGRESS_ALLOW (comma-separated CIDRs, such as 10.20.0.0/16) on the import workers; plain http:// is then allowed to those addresses too. Store the key with that host too (grad secrets set NAME --allowed-host HOST, or allowed_hosts=[...]): a secret only goes to the public provider hosts unless it names others. On Helm, set it in workers.import.env and allow the range in networkPolicy.workerEgress (see Self-hosting).