> ## Documentation Index
> Fetch the complete documentation index at: https://docs.braintrust.dev/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

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

**TypeScript**

## Setup

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.

**1. Install packages**

```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
```

**2. 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>
```

## Auto-instrumentation

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.

**1. Initialize Braintrust and call Cohere**

```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);
```

**2. 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 and run the compiled file with the import hook, or [run TypeScript directly](https://docs.braintrust.dev/instrument/trace-llm-calls.md#auto-instrumentation) with a TypeScript loader.

> [!NOTE]
>
> If you're using a bundler, see [Trace LLM calls](https://docs.braintrust.dev/instrument/trace-llm-calls.md#auto-instrumentation) for plugin and loader setup.

## Manual instrumentation

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.

```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);
```

## What Braintrust traces

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.

## Resources

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

**Python**

## Setup

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

**1. Install packages**

```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
```

**2. 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>
```

## Auto-instrumentation

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

```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)
```

## Manual instrumentation

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.

```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)
```

## What Braintrust traces

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 appear as a list of `{index, relevance_score}` items (capped at 100). When you request `relevant_spans`, the `relevant_spans` request parameter is recorded in metadata, and each result that returns matching passages includes them as `relevant_spans`, a list of `{start, end, score}` items.
* 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.

## Resources

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


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.