Skip to main content
Cohere

Installation

Install the Netra SDK along with the Cohere client library:
pip install netra-sdk cohere
npm install netra-sdk cohere

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:
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?"}]
)
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?"
});

Streaming Responses

Netra automatically handles streaming responses from Cohere:
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="")
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);
  }
}

Rerank Operations

Rerank API calls are also automatically instrumented:
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
)
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
});

Session Tracking

Associate Cohere operations with user sessions for better analytics:
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"}]
)
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"
});

Selective Instrumentation

Enable only Cohere instrumentation if needed:
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}
)
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])
});

Next Steps

Last modified on March 17, 2026