Skip to main content
@openrouter/agent is OpenRouter’s agent toolkit package for agent loops. Braintrust traces @openrouter/agent through auto-instrumentation or wrapOpenRouterAgent.
This guide covers manual instrumentation. For quicker setup, use auto-instrumentation.

Setup

Install the dependencies:
# pnpm
pnpm add braintrust @openrouter/agent
# npm
npm install braintrust @openrouter/agent
Set your API keys:
.env
OPENROUTER_API_KEY=<your-openrouter-api-key>
BRAINTRUST_API_KEY=<your-braintrust-api-key>

# If you are self-hosting Braintrust, set the URL of your hosted dataplane
# BRAINTRUST_API_URL=<your-braintrust-api-url>
Braintrust supports @openrouter/agent v0.1.2 and later.

Trace automatically

Braintrust can auto-instrument OpenRouter.callModel() calls. This is the recommended setup for most projects.
import { initLogger } from "braintrust";
import { OpenRouter } from "@openrouter/agent";

initLogger({
  projectName: "My Project",
  apiKey: process.env.BRAINTRUST_API_KEY,
});

const client = new OpenRouter({ apiKey: process.env.OPENROUTER_API_KEY });
const result = client.callModel({
  model: "openai/gpt-5-mini",
  input: "What is observability?",
});

const text = await result.getText();
Run with the import hook:
node --import braintrust/hook.mjs app.js
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Wrap an OpenRouter Agent client

If you prefer explicit instrumentation, wrap the client with wrapOpenRouterAgent.
import { initLogger, wrapOpenRouterAgent } from "braintrust";
import { OpenRouter } from "@openrouter/agent";

initLogger({
  projectName: "My Project",
  apiKey: process.env.BRAINTRUST_API_KEY,
});

const client = wrapOpenRouterAgent(
  new OpenRouter({ apiKey: process.env.OPENROUTER_API_KEY }),
);

const result = client.callModel({
  model: "openai/gpt-5-mini",
  input: "Reply with exactly: traced",
});

const text = await result.getText();
Braintrust records the top-level callModel() span, token usage, and final response. When your agent loop invokes tools, those tool calls appear as child spans in the trace.