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

# Cohere

> Trace Cohere chat API calls in TypeScript and Python with wrapCohere() or patch_cohere().

<Note>
  Available for: **Python** and **TypeScript**.
</Note>

Wrap your Cohere `ClientV2` with `wrapCohere()` (TypeScript) to trace `chat()` and `chatStream()` calls — or patch the `cohere` module with `patch_cohere()` (Python).

## Installation

<CodeGroup>
  ```bash TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  npm install @zespan/sdk cohere-ai
  ```

  ```bash Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  pip install zespan cohere
  ```
</CodeGroup>

## Setup

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import { CohereClientV2 } from "cohere-ai";
  import { wrapCohere } from "@zespan/sdk";
  import { zespan } from "@zespan/sdk";

  zespan.init({ apiKey: process.env.ZESPAN_API_KEY! });

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

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import os
  import zespan

  zespan.init(api_key=os.environ["ZESPAN_API_KEY"])
  zespan.patch_cohere()

  import cohere  # import after patching
  client = cohere.ClientV2(api_key=os.environ["COHERE_API_KEY"])
  ```
</CodeGroup>

In TypeScript, `wrapCohere()` returns a wrapped client instance. In Python, `patch_cohere()` patches `ClientV2.chat` and `ClientV2.chat_stream` at the class level, so every client constructed afterward is traced automatically.

## Example

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  const response = await cohere.chat({
    model: "command-r-plus",
    messages: [{ role: "user", content: "Explain embeddings briefly." }],
  });
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  response = client.chat(
      model="command-r-plus",
      messages=[{"role": "user", "content": "Explain embeddings briefly."}],
  )
  ```
</CodeGroup>

`chatStream()` (TypeScript) and `chat_stream()` (Python) are traced end-to-end in both languages, including time-to-first-token — parsed from Cohere's `content-delta`, `tool-call-start`/`tool-call-delta`, and `message-end` stream events.

## What gets captured

| Field         | Details                                                                |
| ------------- | ---------------------------------------------------------------------- |
| Model         | `command-r-plus`, `command-r`, `command-light`, etc.                   |
| Input tokens  | From `usage.billed_units.input_tokens` (falls back to `usage.tokens`)  |
| Output tokens | From `usage.billed_units.output_tokens` (falls back to `usage.tokens`) |
| Cost          | Calculated from token counts and Cohere pricing                        |
| Latency       | Total request duration                                                 |
| Finish reason | `COMPLETE`, `MAX_TOKENS`, `TOOL_CALL`, `ERROR`                         |
| Tool calls    | Tool name and parsed arguments, both languages                         |

<Note>
  Cohere's response text lives in `message.content`, a list of typed blocks (`{type: "text", text: "..."}`) rather than a plain string — the wrapper flattens this automatically before storing prompt/completion text.
</Note>
