> ## 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.

# Cohere

> Trace Cohere SDK calls in Braintrust to debug prompts, evaluate models, and monitor production usage

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.

[Cohere](https://cohere.com/) provides chat, embeddings, reranking, and audio transcription models. Braintrust traces Cohere SDK calls, including streaming chat and tool calls.

<View title="TypeScript" icon="https://img.logo.dev/typescriptlang.org?token=pk_BdcHD9e5SCW3j1rnJkNyMQ">
  <h2 id="setup-typescript">
    Setup
  </h2>

  Install the Braintrust and `cohere-ai` packages, then set your API keys. Requires `cohere-ai` v7.0.0 or later, including both the v7 request shape and the current v8 SDK.

  <Steps>
    <Step title="Install packages">
      <CodeGroup>
        ```bash pnpm theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        pnpm add braintrust cohere-ai
        ```

        ```bash npm theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        npm install braintrust cohere-ai
        ```
      </CodeGroup>
    </Step>

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

      # For organizations on the EU data plane, use https://api-eu.braintrust.dev
      # For self-hosted deployments, use your data plane URL
      # BRAINTRUST_API_URL=<your-braintrust-api-url>
      ```
    </Step>
  </Steps>

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

  To trace Cohere SDK calls without modifying your application code, initialize Braintrust normally, then run your app with Braintrust's import hook to patch the Cohere SDK at runtime.

  <Steps>
    <Step title="Initialize Braintrust and call Cohere">
      <CodeGroup>
        ```javascript title="trace-cohere-auto.js" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        import { initLogger } from "braintrust";
        import { CohereClientV2 } from "cohere-ai";

        initLogger({
          projectName: "cohere-example",
          apiKey: process.env.BRAINTRUST_API_KEY,
        });

        const client = new CohereClientV2({
          token: process.env.COHERE_API_KEY,
        });

        const response = await client.chat({
          model: "command-a-03-2025",
          messages: [
            {
              role: "user",
              content: "Explain tracing in one sentence.",
            },
          ],
          maxTokens: 64,
          temperature: 0,
        });

        console.log(response.message?.content);
        ```
      </CodeGroup>
    </Step>

    <Step title="Run with the import hook">
      ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      node --import braintrust/hook.mjs trace-cohere-auto.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>
    </Step>
  </Steps>

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

  To trace Cohere clients manually, wrap them yourself with `wrapCohere()`. Use this when you want to instrument specific clients individually rather than all of them globally.

  <CodeGroup>
    ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
    import { initLogger, wrapCohere } from "braintrust";
    import { CohereClientV2 } from "cohere-ai";

    initLogger({
      projectName: "cohere-example",
      apiKey: process.env.BRAINTRUST_API_KEY,
    });

    const client = wrapCohere(
      new CohereClientV2({
        token: process.env.COHERE_API_KEY,
      }),
    );

    const response = await client.chat({
      model: "command-a-03-2025",
      messages: [
        {
          role: "user",
          content: "Explain tracing in one sentence.",
        },
      ],
      maxTokens: 64,
      temperature: 0,
    });

    console.log(response.message?.content);
    ```
  </CodeGroup>

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

  Braintrust patches the `cohere-ai` SDK and creates an LLM-typed span per call:

  * Chat completion spans (`cohere.chat` and `cohere.chatStream`), with messages and request parameters as input; chat output (including aggregated tool calls for streaming) and token usage; first-token timing for streaming.
  * Extended thinking content for reasoning models, captured from streaming `cohere.chatStream` responses as structured `thinking` content blocks in the span output.
  * Embedding spans (`cohere.embed`), with input texts and request parameters; output summarized as the first embedding's vector length.
  * Rerank spans (`cohere.rerank`), with query and documents as input; results as a list of `{index, relevance_score}` items.
  * Token usage metrics (prompt, completion, total, plus cached prompt tokens and reasoning tokens when reported).
  * Request metadata (model and selected request parameters) and response metadata (response ID, finish reason, generation ID, response type, and Cohere API version when present).
  * Errors captured on every call.

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

  * [Cohere TypeScript SDK](https://github.com/cohere-ai/cohere-typescript)
  * [Cohere API reference](https://docs.cohere.com/reference/about)
</View>

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

  Install the Braintrust and Cohere packages, then set your API keys. Requires `cohere` v5.0.0 or later.

  <Steps>
    <Step title="Install packages">
      <CodeGroup>
        ```bash uv theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        uv add braintrust cohere
        ```

        ```bash pip theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
        pip install braintrust cohere
        ```
      </CodeGroup>
    </Step>

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

      # For organizations on the EU data plane, use https://api-eu.braintrust.dev
      # For self-hosted deployments, use your data plane URL
      # BRAINTRUST_API_URL=<your-braintrust-api-url>
      ```
    </Step>
  </Steps>

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

  To trace Cohere SDK calls without modifying your application code, call `braintrust.auto_instrument()` before creating your Cohere client.

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

    import braintrust

    braintrust.auto_instrument()
    braintrust.init_logger(
        api_key=os.environ["BRAINTRUST_API_KEY"],
        project="cohere-example",  # Replace with your project name
    )

    import cohere

    client = cohere.ClientV2(os.environ["CO_API_KEY"])
    response = client.chat(
        model="command-a-03-2025",
        messages=[{"role": "user", "content": "Explain tracing in one sentence."}],
    )

    print(response.message.content[0].text)
    ```
  </CodeGroup>

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

  To trace Cohere clients manually, wrap them yourself with `wrap_cohere()`. Use this when you want to instrument specific clients individually rather than all of them globally.

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

    from braintrust import init_logger
    from braintrust.integrations.cohere import wrap_cohere
    import cohere

    init_logger(
        api_key=os.environ["BRAINTRUST_API_KEY"],
        project="cohere-example",  # Replace with your project name
    )

    client = wrap_cohere(cohere.ClientV2(os.environ["CO_API_KEY"]))
    response = client.chat(
        model="command-a-03-2025",
        messages=[{"role": "user", "content": "Explain tracing in one sentence."}],
    )

    print(response.message.content[0].text)
    ```
  </CodeGroup>

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

  Braintrust patches `cohere.Client`, `cohere.AsyncClient`, `cohere.ClientV2`, and `cohere.AsyncClientV2` and creates an LLM-typed span per call:

  * Chat completion spans (`cohere.chat` and `cohere.chat_stream`), with messages and request parameters as input; chat output (including aggregated tool calls for streaming) and token usage; first-token timing for streaming.
  * Tool call spans (`tool: <function_name>`) for each tool call returned by v1 and v2 chat responses (sync and streaming), with the tool arguments as input and `tool_call_id` and `tool_type` as metadata.
  * Embedding spans (`cohere.embed`), with input texts and request parameters; output summarized as embedding count and the first embedding's vector length.
  * Rerank spans (`cohere.rerank`), with query and documents as input; results as a list of `{index, relevance_score}` items (capped at 100).
  * Audio transcription spans (`cohere.audio.transcriptions.create`), with the input audio captured as an attachment and the transcribed text as output. Requires `cohere>=6.1.0`, v1 clients only.
  * Token usage metrics (prompt, completion, total, plus cached prompt tokens when reported).
  * Request metadata (model and selected request parameters) and response metadata (response ID, generation ID, response type, finish reason, and Cohere API version when present).
  * Errors captured on every call.

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

  * [Cohere Python SDK](https://github.com/cohere-ai/cohere-python)
  * [Cohere API reference](https://docs.cohere.com/reference/about)
</View>
