Send traces to Braintrust
Six steps to agent traces in Braintrust, through its span processor or the environment alone.
Six steps, from the packages to one trace per turn in Braintrust: the same six on every backend’s page. The framework picker above the code picks your routes; the backend stays picked for the pages after this one.
Install the packages
The telemetry layer and its OpenTelemetry packages, plus Braintrust’s span processor (the OpenTelemetry code moved to @braintrust/otel from braintrust in 1.0).
npm install @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otel @braintrust/otelpnpm add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otel @braintrust/otelyarn add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otel @braintrust/otelbun add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otel @braintrust/otelnub add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otel @braintrust/otelaube add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otel @braintrust/otelSet the environment
The processor reads BRAINTRUST_API_KEY, BRAINTRUST_PARENT and BRAINTRUST_API_URL:
BRAINTRUST_API_KEY=sk-…Without the package
The environment alone is enough; skip @braintrust/otel and use the generic layer in step 3:
# Braintrust without its package. EU: https://api-eu.braintrust.dev/otel
OTEL_EXPORTER_OTLP_ENDPOINT=https://api.braintrust.dev/otel
OTEL_EXPORTER_OTLP_HEADERS=authorization=Bearer%20<BRAINTRUST_API_KEY>,x-bt-parent=project_name:field-storeBraintrust’s docs do not state the protocol for traces; its own package sends JSON, which the built-in exporter sends too.
Add the server layer
Braintrust’s span processor, with the project. parent is project_name:<name>, project_id:<id> or experiment_id:<id>.
import { BraintrustSpanProcessor } from "@braintrust/otel";
import { defineObservability } from "@webmcp-agents/telemetry/server";
// BRAINTRUST_API_KEY comes from the environment.
export const observability = defineObservability({
spanProcessors: [
new BraintrustSpanProcessor({ parent: "project_name:field-store" }),
],
});In Astro, process.env does not hold .env.local in development: read the variables with getSecret from astro:env/server and pass them as env, as the Astro view of the generic layer does. The chat route passes the layer to the handler:
import { createChatHandler } from "@webmcp-agents/chat/server";
import { observability } from "../src/lib/observability";
import { agents } from "../src/agents";
const handler = createChatHandler({ agents, observability });
export const GET = handler;
export const POST = handler;import { createChatHandler } from "@webmcp-agents/chat/server";
import { agents } from "@/agents";
import { observability } from "@/lib/observability";
const handler = createChatHandler({ agents, observability });
export const GET = handler;
export const POST = handler;import { createFileRoute } from "@tanstack/react-router";
import { createChatHandler } from "@webmcp-agents/chat/server";
import { agents } from "@/agents";
import { observability } from "@/lib/observability";
const handler = createChatHandler({ agents, observability });
export const Route = createFileRoute("/api/chat")({
server: {
handlers: {
GET: ({ request }) => handler(request),
POST: ({ request }) => handler(request),
},
},
});import type { APIRoute } from "astro";
import { getSecret } from "astro:env/server";
import { createChatHandler } from "@webmcp-agents/chat/server";
import { agents } from "@/agents";
import { observability } from "@/lib/observability";
export const prerender = false;
const handler = createChatHandler({
agents,
observability,
env: {
ANTHROPIC_API_KEY: getSecret("ANTHROPIC_API_KEY"),
WEB_AGENTS_MODEL: getSecret("WEB_AGENTS_MODEL"),
},
});
export const GET: APIRoute = ({ request }) => handler(request);
export const POST: APIRoute = ({ request }) => handler(request);Add the relay route
The page’s spans come to your own endpoint, so the backend’s key never reaches the browser. The relay refuses other origins and requests with no Origin (a script, not a page); the guard is the per-IP limit for the rest. Keep its counts in a shared store in production, as for the chat (Limits and budgets).
import { observability } from "../src/lib/observability";
// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).
// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
const who = req.headers.get("x-forwarded-for") ?? "local";
const now = Date.now();
const last = seen.get(who);
const entry =
last && now - last.since < 60_000 ? last : { since: now, count: 0 };
entry.count++;
seen.set(who, entry);
// about one batch per run: 60 a minute is plenty for a person
if (entry.count <= 60) return undefined;
return new Response(null, { status: 429 });
};
export const POST = observability.relay({ guard: perIp });import { observability } from "@/lib/observability";
// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).
// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
const who = req.headers.get("x-forwarded-for") ?? "local";
const now = Date.now();
const last = seen.get(who);
const entry =
last && now - last.since < 60_000 ? last : { since: now, count: 0 };
entry.count++;
seen.set(who, entry);
// about one batch per run: 60 a minute is plenty for a person
if (entry.count <= 60) return undefined;
return new Response(null, { status: 429 });
};
export const POST = observability.relay({ guard: perIp });import { createFileRoute } from "@tanstack/react-router";
import { observability } from "@/lib/observability";
// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).
// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
const who = req.headers.get("x-forwarded-for") ?? "local";
const now = Date.now();
const last = seen.get(who);
const entry =
last && now - last.since < 60_000 ? last : { since: now, count: 0 };
entry.count++;
seen.set(who, entry);
// about one batch per run: 60 a minute is plenty for a person
if (entry.count <= 60) return undefined;
return new Response(null, { status: 429 });
};
const relay = observability.relay({ guard: perIp });
export const Route = createFileRoute("/api/telemetry")({
server: {
handlers: {
POST: ({ request }) => relay(request),
},
},
});import type { APIRoute } from "astro";
import { observability } from "@/lib/observability";
export const prerender = false;
// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).
// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
const who = req.headers.get("x-forwarded-for") ?? "local";
const now = Date.now();
const last = seen.get(who);
const entry =
last && now - last.since < 60_000 ? last : { since: now, count: 0 };
entry.count++;
seen.set(who, entry);
// about one batch per run: 60 a minute is plenty for a person
if (entry.count <= 60) return undefined;
return new Response(null, { status: 429 });
};
const relay = observability.relay({ guard: perIp });
export const POST: APIRoute = ({ request }) => relay(request);Observe the page
One component turns each run and tool call on the page into spans and posts them to the relay. Render it once, inside <WebAgentsProvider>, next to the chat. Without React, call observeAgents() once instead.
import { ObserveAgents } from "@webmcp-agents/telemetry/react";
import { consent } from "@/lib/consent";
// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
return <ObserveAgents enabled={() => consent.analytics} />;
}"use client";
import { ObserveAgents } from "@webmcp-agents/telemetry/react";
import { consent } from "@/lib/consent";
// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
return <ObserveAgents enabled={() => consent.analytics} />;
}import { ObserveAgents } from "@webmcp-agents/telemetry/react";
import { consent } from "@/lib/consent";
// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
return <ObserveAgents enabled={() => consent.analytics} />;
}import { ObserveAgents } from "@webmcp-agents/telemetry/react";
import { consent } from "@/lib/consent";
// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
return <ObserveAgents enabled={() => consent.analytics} />;
}Check it in Braintrust
Start the app and send one message in the chat. Braintrust maps the gen_ai.* attributes to its inputs, outputs and metrics, so the default conventions: "genai" fits. It documents no session or user attribute: filter by webmcp.conversation.id, which every span carries.
Sources, and what was not confirmed
- OpenTelemetry: the processor, endpoint and headers
- Attributes
Not confirmed: OTLP JSON for traces on a docs page (confirmed only by the vendor’s package).
Next
The telemetry APIs
What you just set up, one API per page: the layer, where spans go, the relay, the page’s observer, sampling and the conversation key, each showing what differs for Braintrust.