observeAgents
The page's side of a trace: observeAgents() and <ObserveAgents> turn each run and tool call into spans and post them to your relay.
The run and its tool calls happen in the browser: the cursor moving, the person confirming, a call cancelled because
they scrolled away. Only the page can time those, so the page records them. <ObserveAgents> (or observeAgents()
without React) subscribes to the run events, turns each run into an invoke_agent span and each tool call into an
execute_tool span, and posts them to your relay. Render it once, next to the chat.
Import
import { consoleExporter, observeAgents } from "@webmcp-agents/telemetry";
import type {
BrowserSpanExporter,
ObserveAgentsOptions,
SpanRecord,
StopObserving,
} from "@webmcp-agents/telemetry";
import { ObserveAgents } from "@webmcp-agents/telemetry/react";
Example
enabled is your consent banner’s answer, read when each run starts:
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} />;
}Render <ObserveRuns /> once, inside <WebAgentsProvider>. Without React, call observeAgents() once when the page
starts; it returns a function that stops observing:
import { observeAgents } from "@webmcp-agents/telemetry";
// A stand-in for your consent banner's state.
declare const consent: { analytics: boolean };
// No framework: call it once. Each run the person agreed to (read
// when the run starts) is posted to /api/telemetry.
export const stop = observeAgents({
enabled: () => consent.analytics,
});
Options
<ObserveAgents> takes the same options as props.
exporters?readonly (string | OtlpTarget | BrowserSpanExporter)[]
Where spans go. Default: the page's global tracer provider when one is registered (the OpenTelemetry Web SDK, Sentry, Datadog RUM), else the app's relay at `"/api/telemetry"`. A string is the URL of a relay; an OTLP target posts OTLP JSON straight to a collector (its headers end up in the page: never put a key there). `[]` sends nothing and builds no span.
readonly (string | OtlpTarget | BrowserSpanExporter)[]tracerProvider?TracerProviderOption
A provider to use as is, `"global"` to require the page's registered one, or `"own"` never to use it. Default: the global one when registered.
TracerProviderOptionserviceName?string
`service.name` of the browser's spans. Default `location.host`.
stringsampling?Sampling
Which runs are exported, decided at each run's start. Default `{ type: "always" }`.
SamplingrecordInputs?boolean
Put tool inputs on `execute_tool` spans (`gen_ai.tool.call.arguments`), redacted. Default false.
booleanrecordOutputs?boolean
Put tool results on `execute_tool` spans (`gen_ai.tool.call.result`), redacted. Default false.
booleanredact?false | RedactOptions | Redactor
Redaction of everything exported. Default: the page's redactor (`configureRedaction`), read at each use. `false` only for local debugging.
false | RedactOptions | Redactorenabled?() => boolean
Consent: read when each run starts; `false` and that run produces no span and no request. Default `() => true`.
() => booleanspanFilter?(span: SpanRecord) => boolean
Keep (true) or drop (false) a span, after redaction. A throw drops it.
(span: SpanRecord) => booleanmetadataMapping?Record<string, string>
Copy an attribute to the name a backend groups by, e.g. `{ "webmcp.conversation.id": "session.id" }`.
Record<string, string>batchMs?number
Send at most this often, in ms. Default 2000.
numberbatchSize?number
Send sooner once this many spans are waiting. Default 64.
number<ObserveAgents> takes the same options as props (ObserveAgentsProps is ObserveAgentsOptions). enabled is read
at each run’s start from the latest props, so an inline function is fine; the others are read once, when it mounts.
SpanRecord
What a spanFilter and an exporter receive, already redacted.
namestring
`invoke_agent <agent>`, `execute_tool <tool>`, `chat <model>`, or a custom span's own name.
stringtraceIdstring
32 hex digits: the run's trace, shared with the server's spans of the same run.
stringspanIdstring
16 hex digits. A run span's id is the one its `traceparent` carries, so the server's step spans are its children.
stringparentSpanId?string
The parent span: the run for a call or step; the delegating call for a sub-agent's run.
stringstartTimenumber
Start, ms since the epoch.
numberendTimenumber
End, ms since the epoch.
numberattributesAttributes
The span's attributes (names in `semconv`).
Attributesevents{ name: string; time: number; attributes?: Attributes }[]
`webmcp.park`, `webmcp.yield`, `webmcp.resume` on a tool call; the app's own (`recordAgentEvent`) on a run.
{ name: string; time: number; attributes?: Attributes }[]status{ code: "unset" | "ok" | "error"; message?: string }
`error` for a failed run, call or custom span (with `error.type` set), else `unset`.
{ code: "unset" | "ok" | "error"; message?: string }Returns
observeAgents()returns aStopObserving: call it to stop (buffered spans are sent first; no later run is observed). Itsflush()sends what is buffered now and resolves once the exporters took it; it never rejects.<ObserveAgents>renders nothing. It stops, and sends what is buffered, when it unmounts.consoleExporter()takes no options and returns aBrowserSpanExporterwhoseexport(spans)logs[web-agents/telemetry] <span name>and the span record for each span. It sends nothing.
Your backend
The page’s spans go through the relay, and the backend’s key stays on the server.
The page’s spans go through the relay like for any backend: the agent key never reaches the page. Instana’s own website monitoring (EUM) is separate and can run beside it.
Notes
What it costs the page
Up front, about 0.7 KB (min+gz): the subscription. The span builder, about 3.5 KB, loads when the first run the
person agreed to starts. A page where no agent runs, or where consent is never given, never downloads it. A run’s
spans (its sub-agents’ included) are sent together when the run ends, parents first, in one request; a run sends
early once 128 spans wait or after 5 minutes. What is still buffered is sent with keepalive when the page is
hidden.
A page that already runs OpenTelemetry
If the page registered an OpenTelemetry Web SDK provider (yours, or one from Sentry or Datadog RUM), the spans go
to that provider instead of the relay, and join the page’s own traces. The bridge to it, and
@opentelemetry/api, load only on such a page. Pass tracerProvider: "own" to keep using the relay anyway.
While you build: print instead of send
consoleExporter() prints each span in the browser’s console, redacted, and sends nothing. Any object with an
export(spans) method works as an exporter; SpanRecord is what
it receives.
observe.ts
import {
consoleExporter,
observeAgents,
} from "@webmcp-agents/telemetry";
// While you build: print each span in the browser's console, and
// send nothing anywhere.
export const stop = observeAgents({ exporters: [consoleExporter()] });No relay
A relay that answers 404 or 405 is taken as absent: nothing more is sent.
The sampled flag
A run this page records (consent given, kept by sampling) is marked at its start (ref.sampled,
onAgentRun), so its step requests send a traceparent ending in -01 and the
server’s step spans sit under the run span. Any other run sends -00, and the server keeps its own tree.
Content
Tool inputs and results are left out unless you record them, and are redacted either way: the run events they come from are redacted too.
On-device runs
A run on the on-device model has no server. Its model steps become chat spans built in the browser, without
token counts.