Skip to main content

Installation

Install the Netra SDK along with the Cohere client library:
pip 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?"}]
)

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="")

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
)

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

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}
)

Next Steps

Last modified on February 3, 2026