@cloudflare/think is an agent framework for Cloudflare Workers built on Durable Objects. Each agent extends the Think class and runs multi-turn reasoning loops, delegating inference and tool execution to the AI SDK. Braintrust traces every runTurn call as a task span and nests the AI SDK calls and tool invocations beneath it.
For Cloudflare Workers deployments, use manual instrumentation with
wrapCloudflareThink(). 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@cloudflare/think, then set your API keys. Requires @cloudflare/think v0.13.0 or later.1
Install packages
2
Set environment variables
.env
Manual instrumentation
Manual instrumentation is the recommended approach for Cloudflare Workers. CallwrapCloudflareThink() on the @cloudflare/think module at module scope, then extend the wrapped Think class so every agent instance receives Braintrust tracing without any changes to your agent logic. 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-think-manual.ts
wrapCloudflareThink() accepts the module namespace (import * as cloudflareThink from "@cloudflare/think") and patches the Think prototype in place, so all subclasses inherit tracing without modifying your agent definitions.Because Think agents are Durable Objects, your wrangler.jsonc needs the nodejs_compat compatibility flag, a Durable Object binding for your agent class, and a migration that registers it. Store BRAINTRUST_API_KEY as a Wrangler secret.wrangler.jsonc
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
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:- Task spans for each
Think.runTurncall, with the turn input messages as input and the final assistant response as output. - Model name, provider, and token usage as metadata on the task span.
- Nested LLM spans for each AI SDK call issued during the turn, with messages, parameters, response content, and token usage.
- Tool call spans for each tool invoked during the turn, with tool name, arguments, and result.
- Errors captured on the span if the turn fails or the stream encounters an error.