Trace Cohere Command model calls with Netra auto-instrumentation. Monitor prompts, completions, embeddings, token usage, and latency.
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:Set your Netra endpoint and API key before you call Netra.init(). Without NETRA_OTLP_ENDPOINT, the SDK prints spans to the console instead of sending them to Netra, and your dashboard stays empty.
Set these wherever your app runs, including Docker containers and CI jobs.
from netra import Netraimport cohere# Initialize Netra SDKNetra.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 automaticco = 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 SDKawait 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 automaticconst 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 Netraimport cohereNetra.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 tracedstream = 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 tracedconst 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 Netraimport cohereNetra.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 Netraimport cohereNetra.init( app_name="my-cohere-app", headers=f"x-api-key={YOUR_NETRA_API_KEY}")# Set session contextNetra.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 sessionresponse = 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 contextNetra.setSessionId("session-123");Netra.setUserId("user-456");const cohere = new Cohere({ apiKey: process.env.COHERE_API_KEY});// All subsequent calls are tracked under this sessionconst response = await cohere.chat({ model: "command-r-plus", message: "Hello"});
Selective Instrumentation
Enable only Cohere instrumentation if needed:
from netra import Netrafrom netra.instrumentation.instruments import InstrumentSetNetra.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])});