OpenTelemetry Cohere Instrumentation
This library provides OpenTelemetry instrumentation for the Cohere Python SDK, enabling automatic tracing of calls to Cohere's language model endpoints. It helps monitor performance, token usage, and costs associated with LLM applications. Regularly updated, the current version is 0.58.0, with releases occurring frequently, sometimes multiple times a month.
Warnings
- breaking Breaking changes related to OpenTelemetry GenAI Semantic Conventions (gen_ai) have been introduced in versions 0.55.0 and later. Attribute names for LLM-related spans may have changed to align with the new conventions (e.g., `gen_ai.request.model` instead of older variations).
- gotcha By default, this instrumentation logs sensitive data like prompts, completions, and embeddings to span attributes, which might contain highly sensitive user data.
- gotcha The `CohereInstrumentor().instrument()` call must be executed *before* any `cohere.Client()` instance is created or any Cohere API calls are made, as instrumentation typically relies on monkey-patching existing libraries.
- gotcha Simply installing `opentelemetry-instrumentation-cohere` and calling `instrument()` will not make traces visible. You must also configure an OpenTelemetry SDK (e.g., `TracerProvider`, `SpanProcessor`, and an `Exporter`) in your application.
Install
-
pip install opentelemetry-instrumentation-cohere cohere opentelemetry-sdk
Imports
- CohereInstrumentor
from opentelemetry.instrumentation.cohere import CohereInstrumentor
Quickstart
import os
import cohere
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.cohere import CohereInstrumentor
# 1. Set up OpenTelemetry SDK for console output
resource = Resource.create({"service.name": "my-cohere-app"})
provider = TracerProvider(resource=resource)
processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
# 2. Instrument Cohere
CohereInstrumentor().instrument()
# 3. Use Cohere client (ensure COHERE_API_KEY environment variable is set)
cohere_api_key = os.environ.get('COHERE_API_KEY', '')
if not cohere_api_key:
print("Warning: COHERE_API_KEY environment variable not set. Cohere client may fail.")
# For demonstration, you might want to uncomment and replace 'YOUR_COHERE_API_KEY' for testing
# cohere_api_key = 'YOUR_COHERE_API_KEY'
if cohere_api_key:
try:
client = cohere.Client(cohere_api_key)
response = client.chat(
model="command",
message="Hello, how are you today?"
)
print("\nCohere Response:", response.text)
print("\nTraces should be printed to the console above.")
except Exception as e:
print(f"Error calling Cohere: {e}")
else:
print("Skipping Cohere call because API key is not available.")