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

# Cohere

> Trace Cohere Command model calls with Netra auto-instrumentation. Monitor prompts, completions, embeddings, token usage, and latency automatically.

<img src="https://mintcdn.com/netra/u6ajHWd7ki_9CRWQ/images/integration-logos/llm-providers/cohere.png?fit=max&auto=format&n=u6ajHWd7ki_9CRWQ&q=85&s=a2a8b9ed7d82601e2924858078977bbe" alt="Cohere" width="350" height="80" data-path="images/integration-logos/llm-providers/cohere.png" />

## Installation

Install the Netra SDK along with the Cohere client library:

<CodeGroup>
  ```bash Python theme={null}
  pip install netra-sdk cohere
  ```

  ```bash TypeScript theme={null}
  npm install netra-sdk cohere
  ```
</CodeGroup>

## Usage

Netra automatically instruments Cohere API calls when you initialize the SDK. The instrumentation captures chat completions, streaming responses, and rerank operations.

## Basic Setup

Initialize Netra at the start of your application to enable automatic Cohere instrumentation:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  import cohere

  # Initialize Netra SDK
  Netra.init(
      app_name="my-cohere-app",
      headers=f"x-api-key={YOUR_NETRA_API_KEY}",
      trace_content=True  # Capture prompts and completions
  )

  # Use Cohere normally - instrumentation is automatic
  co = cohere.ClientV2(api_key="your-cohere-api-key")

  response = co.chat(
      model="command-r-plus",
      messages=[{"role": "user", "content": "What is machine learning?"}]
  )
  ```

  ```typescript TypeScript theme={null}
  import { Netra } from "netra-sdk";
  import Cohere from "cohere-ai";

  // Initialize Netra SDK
  await Netra.init({
    appName: "my-cohere-app",
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    traceContent: true  // Capture prompts and completions
  });

  // Use Cohere normally - instrumentation is automatic
  const cohere = new Cohere({
    apiKey: process.env.COHERE_API_KEY
  });

  const response = await cohere.chat({
    model: "command-r-plus",
    message: "What is machine learning?"
  });
  ```
</CodeGroup>

## Streaming Responses

Netra automatically handles streaming responses from Cohere:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  import cohere

  Netra.init(
      app_name="my-cohere-app",
      headers=f"x-api-key={YOUR_NETRA_API_KEY}"
  )

  co = cohere.ClientV2(api_key="your-cohere-api-key")

  # Streaming is automatically traced
  stream = co.chat_stream(
      model="command-r-plus",
      messages=[{"role": "user", "content": "Tell me a story"}]
  )

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

  ```typescript TypeScript theme={null}
  import { Netra } from "netra-sdk";
  import Cohere from "cohere-ai";

  await Netra.init({
    appName: "my-cohere-app",
    headers: `x-api-key=${process.env.NETRA_API_KEY}`
  });

  const cohere = new Cohere({
    apiKey: process.env.COHERE_API_KEY
  });

  // Streaming is automatically traced
  const stream = await cohere.chatStream({
    model: "command-r-plus",
    message: "Tell me a story"
  });

  for await (const chunk of stream) {
    if (chunk.eventType === "text-generation") {
      process.stdout.write(chunk.text);
    }
  }
  ```
</CodeGroup>

## Rerank Operations

Rerank API calls are also automatically instrumented:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  import cohere

  Netra.init(
      app_name="my-cohere-app",
      headers=f"x-api-key={YOUR_NETRA_API_KEY}"
  )

  co = cohere.ClientV2(api_key="your-cohere-api-key")

  documents = [
      "Python is a programming language",
      "The sky is blue",
      "Machine learning uses algorithms"
  ]

  results = co.rerank(
      model="rerank-english-v2.0",
      query="programming",
      documents=documents
  )
  ```

  ```typescript TypeScript theme={null}
  import { Netra } from "netra-sdk";
  import Cohere from "cohere-ai";

  await Netra.init({
    appName: "my-cohere-app",
    headers: `x-api-key=${process.env.NETRA_API_KEY}`
  });

  const cohere = new Cohere({
    apiKey: process.env.COHERE_API_KEY
  });

  const documents = [
    "Python is a programming language",
    "The sky is blue",
    "Machine learning uses algorithms"
  ];

  const results = await cohere.rerank({
    model: "rerank-english-v2.0",
    query: "programming",
    documents: documents
  });
  ```
</CodeGroup>

## Session Tracking

Associate Cohere operations with user sessions for better analytics:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  import cohere

  Netra.init(
      app_name="my-cohere-app",
      headers=f"x-api-key={YOUR_NETRA_API_KEY}"
  )

  # Set session context
  Netra.set_session_id("session-123")
  Netra.set_user_id("user-456")

  co = cohere.ClientV2(api_key="your-cohere-api-key")

  # All subsequent calls are tracked under this session
  response = co.chat(
      model="command-r-plus",
      messages=[{"role": "user", "content": "Hello"}]
  )
  ```

  ```typescript TypeScript theme={null}
  import { Netra } from "netra-sdk";
  import Cohere from "cohere-ai";

  await Netra.init({
    appName: "my-cohere-app",
    headers: `x-api-key=${process.env.NETRA_API_KEY}`
  });

  // Set session context
  Netra.setSessionId("session-123");
  Netra.setUserId("user-456");

  const cohere = new Cohere({
    apiKey: process.env.COHERE_API_KEY
  });

  // All subsequent calls are tracked under this session
  const response = await cohere.chat({
    model: "command-r-plus",
    message: "Hello"
  });
  ```
</CodeGroup>

## Selective Instrumentation

Enable only Cohere instrumentation if needed:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from netra.instrumentation.instruments import InstrumentSet

  Netra.init(
      app_name="my-cohere-app",
      headers=f"x-api-key={YOUR_NETRA_API_KEY}",
      instruments={InstrumentSet.COHERE}
  )
  ```

  ```typescript TypeScript theme={null}
  import { Netra, NetraInstruments } from "netra-sdk";

  await Netra.init({
    appName: "my-cohere-app",
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    instruments: new Set([NetraInstruments.COHERE])
  });
  ```
</CodeGroup>

# Next Steps

* [Quick Start Guide](https://docs.getnetra.ai/quick-start/python) - Get started with Netra SDK
* [Auto Instrumentation](https://docs.getnetra.ai/tracing/auto-instrumentation) - Learn about automatic instrumentation
* [Session Management](https://docs.getnetra.ai/tracing/session) - Track user sessions and context
* [The Cohere Platform](https://docs.cohere.com/docs/the-cohere-platform) - Official Cohere documentation
