> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://respan.ai/docs/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://respan.ai/docs/_mcp/server.

# Cohere (tracing)

> Trace Cohere SDK chat, streaming, embeddings, and rerank calls with Respan in Python or TypeScript.

#### Set up Respan

1. **Sign up** - Create an account at [platform.respan.ai](https://platform.respan.ai)
2. **Create an API key** - Generate one on the [API keys page](https://platform.respan.ai/platform/gateway/api-keys)

#### Use Respan Gateway

See [Cohere gateway setup](/docs/gateway/cohere) to route this integration through the Respan gateway.

#### Use AI

Add the [Docs MCP](/docs/documentation/get-started/mcp/docs-mcp) to your AI coding tool to get help building with Respan. No API key needed.

```json
{
  "mcpServers": {
    "respan-docs": {
      "url": "https://mcp.respan.ai/mcp/docs"
    }
  }
}
```

#### Example projects

* [Python examples](https://github.com/respanai/respan-example-projects/tree/main/python/tracing/cohere)
* [TypeScript examples](https://github.com/respanai/respan-example-projects/tree/main/typescript/tracing/cohere)

## What is Cohere?

The [Cohere SDKs](https://docs.cohere.com/) provide chat, streaming chat, embedding, and rerank APIs. `respan-instrumentation-cohere` and `@respan/instrumentation-cohere` normalize Cohere spans to the Respan span contract before export.

## Setup

#### Install packages

**`Python`**

```bash Python
pip install respan-ai respan-instrumentation-cohere cohere python-dotenv
```

**`TypeScript`**

```bash TypeScript
npm install @respan/respan @respan/tracing @respan/instrumentation-cohere cohere-ai dotenv
```

#### Set environment variables

```bash
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
export CO_API_KEY="YOUR_COHERE_API_KEY"
export COHERE_API_KEY="YOUR_COHERE_API_KEY"
```

Optional:

```bash
export RESPAN_BASE_URL="https://api.respan.ai/api"
export COHERE_CHAT_MODEL="command-a-03-2025"
```

`CO_API_KEY` is used by the Python SDK. `COHERE_API_KEY` is used by the TypeScript SDK. `RESPAN_API_KEY` is used by Respan for trace export.

#### Initialize and run

**`Python`**

```python Python
import os

import cohere
from dotenv import load_dotenv
from respan import Respan, workflow
from respan_instrumentation_cohere import CohereInstrumentor

load_dotenv()

respan = Respan(
    api_key=os.environ["RESPAN_API_KEY"],
    base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
    instrumentations=[CohereInstrumentor()],
)
client = cohere.ClientV2(api_key=os.environ["CO_API_KEY"])


@workflow(name="cohere_chat.workflow")
def run_chat() -> str:
    response = client.chat(
        model=os.getenv("COHERE_CHAT_MODEL", "command-a-03-2025"),
        messages=[
            {
                "role": "user",
                "content": "In one sentence, explain what Cohere rerank is useful for.",
            }
        ],
        temperature=0.1,
        max_tokens=80,
    )
    return response.message.content[0].text


with respan.propagate_attributes(
    trace_group_identifier="cohere_chat.workflow",
    metadata={"workflow_name": "cohere_chat.workflow"},
):
    print(run_chat())
```

**`TypeScript`**

```typescript TypeScript
import "dotenv/config";
import * as Cohere from "cohere-ai";
import { CohereClientV2 } from "cohere-ai";
import { Respan } from "@respan/respan";
import { CohereInstrumentor } from "@respan/instrumentation-cohere";

const respan = new Respan({
  apiKey: process.env.RESPAN_API_KEY,
  baseURL: process.env.RESPAN_BASE_URL,
  instrumentations: [new CohereInstrumentor({ sdkModule: Cohere })],
});
await respan.initialize();

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

try {
  const result = await respan.withWorkflow({ name: "cohere_chat.workflow" }, async () => {
    return cohere.chat({
      model: process.env.COHERE_CHAT_MODEL ?? "command-a-03-2025",
      messages: [
        {
          role: "user",
          content: "In one sentence, explain what Cohere rerank is useful for.",
        },
      ],
      temperature: 0.1,
      maxTokens: 80,
    });
  });
  console.log(result.message?.content);
} finally {
  await respan.shutdown();
}
```

#### View your trace

Open the [Traces page](https://platform.respan.ai/platform/traces) and search for the workflow name `cohere_chat.workflow`.

## Configuration

| Parameter               | Type                     | Default     | Description                                                                                                                  |
| ----------------------- | ------------------------ | ----------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `exception_logger`      | `logging.Logger \| None` | `None`      | Optional logger passed to the underlying Cohere OpenTelemetry instrumentor.                                                  |
| `sdkModule`             | `object \| undefined`    | `undefined` | TypeScript only. Pass the imported `cohere-ai` module so the instrumentor patches the same module instance used by your app. |
| `use_legacy_attributes` | `bool`                   | `True`      | Python only. Emits span attributes compatible with the existing Respan span contract.                                        |
| `**instrumentor_kwargs` | `dict`                   | `{}`        | Python only. Extra keyword arguments forwarded to the upstream instrumentor.                                                 |

## Attributes

Attach customer identifiers, thread IDs, and metadata to Cohere calls with `propagate_attributes` or `propagateAttributes`.

**`Python`**

```python Python
with respan.propagate_attributes(
    customer_identifier="user_123",
    thread_identifier="conversation_456",
    trace_group_identifier="cohere_support_chat.workflow",
    metadata={"plan": "pro", "workflow_name": "cohere_support_chat.workflow"},
):
    response = client.chat(
        model="command-a-03-2025",
        messages=[{"role": "user", "content": "Summarize our support policy."}],
    )
```

**`TypeScript`**

```typescript TypeScript
await respan.propagateAttributes(
  {
    customer_identifier: "user_123",
    thread_identifier: "conversation_456",
    trace_group_identifier: "cohere_support_chat.workflow",
    metadata: { plan: "pro", workflow_name: "cohere_support_chat.workflow" },
  },
  async () => {
    return cohere.chat({
      model: "command-a-03-2025",
      messages: [{ role: "user", content: "Summarize our support policy." }],
    });
  }
);
```

| Attribute                | Type   | Description                                          |
| ------------------------ | ------ | ---------------------------------------------------- |
| `customer_identifier`    | `str`  | Identifies the end user in Respan analytics.         |
| `thread_identifier`      | `str`  | Groups related messages into a conversation.         |
| `trace_group_identifier` | `str`  | Groups spans by workflow name.                       |
| `metadata`               | `dict` | Custom key-value pairs merged with default metadata. |

## Examples

### Streaming chat

**`Python`**

```python Python
stream = client.chat_stream(
    model="command-a-03-2025",
    messages=[{"role": "user", "content": "Write a short haiku about observability."}],
)

for event in stream:
    if event.type == "content-delta":
        print(event.delta.message.content.text, end="", flush=True)
```

**`TypeScript`**

```typescript TypeScript
const stream = await cohere.chatStream({
  model: "command-a-03-2025",
  messages: [{ role: "user", content: "Write a short haiku about observability." }],
});

for await (const event of stream) {
  if (event.type === "content-delta") {
    process.stdout.write(event.delta?.message?.content?.text ?? "");
  }
}
```

### Embeddings

**`Python`**

```python Python
response = client.embed(
    model="embed-v4.0",
    input_type="search_document",
    texts=[
        "Respan records traces and spans for AI systems.",
        "Cohere embeddings convert text into vectors.",
    ],
    embedding_types=["float"],
)
print(len(response.embeddings.float_))
```

**`TypeScript`**

```typescript TypeScript
const response = await cohere.embed({
  model: "embed-v4.0",
  inputType: "search_document",
  texts: [
    "Respan records traces and spans for AI systems.",
    "Cohere embeddings convert text into vectors.",
  ],
  embeddingTypes: ["float"],
});
console.log(response.embeddings?.float?.length);
```

### Rerank

**`Python`**

```python Python
response = client.rerank(
    model="rerank-v3.5",
    query="Which document explains search result ranking?",
    documents=[
        "Rerank models reorder candidate documents by query relevance.",
        "Chat models generate conversational responses.",
    ],
    top_n=1,
)
print(response.results[0].index)
```

**`TypeScript`**

```typescript TypeScript
const response = await cohere.rerank({
  model: "rerank-v3.5",
  query: "Which document explains search result ranking?",
  documents: [
    "Rerank models reorder candidate documents by query relevance.",
    "Chat models generate conversational responses.",
  ],
  topN: 1,
});
console.log(response.results?.[0]?.index);
```