Setup
Install the Braintrust and@openrouter/sdk packages, then set your API keys.1
Install packages
2
Set environment variables
.env
Auto-instrumentation
To trace OpenRouter SDK calls without modifying your application code, initialize Braintrust normally, then run your app with Braintrust’s import hook to patch the OpenRouter SDK at runtime.1
Initialize Braintrust and call OpenRouter
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.
Manual instrumentation
To trace OpenRouter clients manually, wrap them yourself withwrapOpenRouter. Use this when you want to instrument specific clients individually rather than all of them globally.What Braintrust traces
Braintrust instruments the@openrouter/sdk client and creates an LLM-typed span per call:- Chat completion spans (
openrouter.chat.send), capturing messages as input, request parameters as metadata, and the response choices plus token usage as output. Streaming responses are aggregated (including reasoning and tool calls) and record first-token timing. - Embedding spans (
openrouter.embeddings.generate), capturing input texts as input and the first embedding’s vector length as output. - Rerank spans (
openrouter.rerank.rerank), capturing the query and documents as input, document count and request parameters as metadata, and results as a list of{ index, relevance_score }items. - Response spans (
openrouter.beta.responses.send), capturing input and request parameters as metadata and the response output plus token usage as output, including streaming. - Request metadata (model and provider parsed from the OpenRouter
provider/modelID) and token usage metrics (prompt, completion, and total).