> ## Documentation Index
> Fetch the complete documentation index at: https://braintrust.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenRouter

> Trace OpenRouter SDK calls with Braintrust

If you are a coding agent, prefer the Braintrust [`bt` CLI](/reference/cli/quickstart) for repeatable, scriptable work: running evals, instrumenting code, querying logs, syncing data, managing functions, and configuring coding agents. Use the MCP server for reasoning over Braintrust data in conversation, such as ad-hoc lookups and exploration from your IDE.

[OpenRouter](https://openrouter.ai/) lets you call models from many providers through a single API.

<Tabs>
  <Tab title="TypeScript" icon="https://img.logo.dev/typescriptlang.org?token=pk_BdcHD9e5SCW3j1rnJkNyMQ">
    <Tip>
      If you're using OpenRouter's agent toolkit package (`@openrouter/agent`), see [OpenRouter Agent](/integrations/agent-frameworks/openrouter-agent).
    </Tip>

    <h2 id="setup-typescript">
      Setup
    </h2>

    <Steps>
      <Step title="Install packages">
        ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        # pnpm
        pnpm add braintrust @openrouter/sdk
        # npm
        npm install braintrust @openrouter/sdk
        ```
      </Step>

      <Step title="Set environment variables">
        ```bash title=".env" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        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>
        ```
      </Step>
    </Steps>

    <h2 id="auto-instrumentation-typescript">
      Auto-instrumentation
    </h2>

    Braintrust provides automatic tracing for OpenRouter calls. This is the recommended setup for most projects.

    <CodeGroup>
      ```javascript title="app.js" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      import { initLogger } from "braintrust";
      import { OpenRouter } from "@openrouter/sdk";

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

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

      const response = await client.chat.send({
        chatRequest: {
          model: "openai/gpt-5-mini",
          messages: [{ role: "user", content: "What is observability?" }],
        },
      });
      ```
    </CodeGroup>

    Run with the import hook:

    ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
    node --import braintrust/hook.mjs app.js
    ```

    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.

    <Note>
      If you're using a bundler, see [Trace LLM calls](/instrument/trace-llm-calls#auto-instrumentation) for plugin and loader setup.
    </Note>

    <h2 id="manual-instrumentation-typescript">
      Manual instrumentation
    </h2>

    Trace an OpenRouter client explicitly by wrapping it with `wrapOpenRouter`.

    <CodeGroup>
      ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      import { initLogger, wrapOpenRouter } from "braintrust";
      import { OpenRouter } from "@openrouter/sdk";

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

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

      const response = await client.chat.send({
        chatRequest: {
          model: "openai/gpt-5-mini",
          messages: [{ role: "user", content: "What is observability?" }],
        },
      });
      ```
    </CodeGroup>

    <h2 id="what-traced-typescript">
      What Braintrust traces
    </h2>

    For the `@openrouter/sdk` package, Braintrust traces:

    * Chat completions via `chat.send()`, including streaming
    * Embeddings via `embeddings.generate()`
    * Reranking via `rerank.rerank()`, with `topN` and document count in metadata and each result captured as `{ index, relevance_score }`
    * Responses API via `beta.responses.send()`, including streaming
  </Tab>

  <Tab title="Python" icon="https://img.logo.dev/python.org?token=pk_BdcHD9e5SCW3j1rnJkNyMQ">
    <h2 id="setup-python">
      Setup
    </h2>

    <Steps>
      <Step title="Install packages">
        ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        pip install braintrust openrouter
        ```
      </Step>

      <Step title="Set environment variables">
        ```bash title=".env" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        OPENROUTER_API_KEY=<your-openrouter-api-key>
        BRAINTRUST_API_KEY=<your-braintrust-api-key>
        ```
      </Step>
    </Steps>

    <h2 id="auto-instrumentation-python">
      Auto-instrumentation
    </h2>

    Braintrust provides automatic tracing for OpenRouter calls. This is the recommended setup for most projects.

    <CodeGroup>
      ```python Python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      import os

      import braintrust

      braintrust.auto_instrument()
      braintrust.init_logger(project="My Project")

      from openrouter import OpenRouter

      client = OpenRouter(api_key=os.environ["OPENROUTER_API_KEY"])

      response = client.chat.send(
          model="openai/gpt-5-mini",
          messages=[{"role": "user", "content": "What is observability?"}],
      )
      ```
    </CodeGroup>

    <h2 id="manual-instrumentation-python">
      Manual instrumentation
    </h2>

    Trace the OpenRouter Python SDK by wrapping the client with `wrap_openrouter()`.

    <CodeGroup>
      ```python Python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      import os

      from braintrust import init_logger, wrap_openrouter
      from openrouter import OpenRouter

      init_logger(project="My Project")

      client = wrap_openrouter(OpenRouter(api_key=os.environ["OPENROUTER_API_KEY"]))

      response = client.beta.responses.send(
          model="openai/gpt-5-mini",
          input="Summarize tracing in one sentence.",
      )

      # Response shape varies; adjust indexing for your model and request.
      print(response.output[1].content[0].text)
      ```
    </CodeGroup>

    <h3 id="openai-compatible-python">
      OpenAI-compatible endpoint
    </h3>

    If your app already uses the OpenAI Python SDK with OpenRouter's OpenAI-compatible endpoint, keep that setup and use `wrap_openai()`.

    <CodeGroup>
      ```python Python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      import os

      from braintrust import init_logger, wrap_openai
      from openai import OpenAI

      init_logger(project="My Project")

      client = wrap_openai(
          OpenAI(
              base_url="https://openrouter.ai/api/v1",
              api_key=os.environ["OPENROUTER_API_KEY"],
          )
      )

      response = client.responses.create(
          model="openai/gpt-5-mini",
          input="Explain routing in one sentence.",
      )

      print(response.output_text)
      ```
    </CodeGroup>

    <h2 id="what-traced-python">
      What Braintrust traces
    </h2>

    For the `openrouter` SDK, Braintrust traces:

    * Chat completions via `chat.send()` and `send_async()`, including streaming
    * Embeddings via `embeddings.generate()` and the async variant
    * Responses API calls via `beta.responses.send()` and `send_async()`, including streaming

    <h2 id="resources-python">
      Resources
    </h2>

    * [OpenRouter Python SDK](https://pypi.org/project/openrouter/)
    * [Trace LLM calls](/instrument/trace-llm-calls)
    * [OpenAI integration](/integrations/ai-providers/openai)
  </Tab>
</Tabs>
