Skip to main content
Cloudflare Agents are Durable Object–backed workers built with the agents npm package. Braintrust traces every runAgentTool call, capturing the agent class name, tool inputs, tool outputs, and errors.
For Cloudflare Workers deployments, use manual instrumentation with wrapCloudflareAgent(). The --import auto-instrumentation hook only runs under Node, not in the Cloudflare Workers runtime (workerd). See the Cloudflare setup guide for enabling nodejs_compat and flushing traces with ctx.waitUntil().

Setup

Install Braintrust alongside the agents package, then set your API keys. Requires agents v0.17.0 or later.
1

Install packages

2

Set environment variables

.env

Manual instrumentation

Manual instrumentation is the recommended approach for Cloudflare Workers. Call wrapCloudflareAgent() on the base Agent class at module scope so all subclasses inherit Braintrust tracing without any changes to your class definitions. Initialize the logger inside fetch with your env bindings, then pass logger.flush() to ctx.waitUntil() so buffered traces ship after the response returns.
cloudflare-agent-manual.ts
wrapCloudflareAgent() returns the same class it receives after patching its prototype. Calling it on the base Agent class instruments all subclasses without modifying your class definitions.Deploying to Cloudflare Workers requires the nodejs_compat compatibility flag and storing BRAINTRUST_API_KEY as a Wrangler secret. See the Cloudflare setup guide for the full deployment configuration.

Auto-instrumentation

Auto-instrumentation patches the SDK at runtime without modifying your application code, but the --import hook only runs under Node (local development or tests), not in the Cloudflare Workers runtime. For a deployed Worker, use manual instrumentation above.
1

Initialize Braintrust and define your agent

2

Run with the import hook

The --import hook only patches the SDK when your code runs under Node, such as local development or tests. It does not run in the Cloudflare Workers runtime (workerd), so a Worker deployed with Wrangler stays uninstrumented. To trace a deployed Worker, use manual instrumentation with wrapCloudflareAgent().
The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

What Braintrust traces

Braintrust captures:
  • Tool spans for each runAgentTool call, named after the agent class (for example, MyAgent).
  • Tool input passed to the agent tool call.
  • Tool output returned by the agent, or an error if the call fails.

Resources