Observability
Cohere (tracing)
Set up Respan
- Sign up - Create an account at platform.respan.ai
- Create an API key - Generate one on the API keys page
Paste this into your coding agent:
Help me set up Respan tracing with the Cohere integration.
Follow https://www.respan.ai/docs/integrations/cohere.mdWhat is Cohere?
The Cohere SDKs provide chat, streaming chat, embedding, and rerank APIs. respan-instrumentation-cohere and @respan/instrumentation-cohere normalize Cohere spans to the Respan span contract before export.
Setup
Install packages
pip install respan-ai respan-instrumentation-cohere cohere python-dotenvSet environment variables
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
export CO_API_KEY="YOUR_COHERE_API_KEY"
export COHERE_API_KEY="YOUR_COHERE_API_KEY"Optional:
export RESPAN_BASE_URL="https://api.respan.ai/api"
export COHERE_CHAT_MODEL="command-a-03-2025"CO_API_KEY is used by the Python SDK. COHERE_API_KEY is used by the TypeScript SDK. RESPAN_API_KEY is used by Respan for trace export.
Initialize and run
import os
import cohere
from dotenv import load_dotenv
from respan import Respan, workflow
from respan_instrumentation_cohere import CohereInstrumentor
load_dotenv()
respan = Respan(
api_key=os.environ["RESPAN_API_KEY"],
base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
instrumentations=[CohereInstrumentor()],
)
client = cohere.ClientV2(api_key=os.environ["CO_API_KEY"])
@workflow(name="cohere_chat.workflow")
def run_chat() -> str:
response = client.chat(
model=os.getenv("COHERE_CHAT_MODEL", "command-a-03-2025"),
messages=[
{
"role": "user",
"content": "In one sentence, explain what Cohere rerank is useful for.",
}
],
temperature=0.1,
max_tokens=80,
)
return response.message.content[0].text
with respan.propagate_attributes(
trace_group_identifier="cohere_chat.workflow",
metadata={"workflow_name": "cohere_chat.workflow"},
):
print(run_chat())View your trace
Open the Traces page and search for the workflow name cohere_chat.workflow.
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
exception_logger | logging.Logger | None | None | Optional logger passed to the underlying Cohere OpenTelemetry instrumentor. |
sdkModule | object | undefined | undefined | TypeScript only. Pass the imported cohere-ai module so the instrumentor patches the same module instance used by your app. |
use_legacy_attributes | bool | True | Python only. Emits span attributes compatible with the existing Respan span contract. |
**instrumentor_kwargs | dict | {} | Python only. Extra keyword arguments forwarded to the upstream instrumentor. |
Attributes
Attach customer identifiers, thread IDs, and metadata to Cohere calls with propagate_attributes or propagateAttributes.
with respan.propagate_attributes(
customer_identifier="user_123",
session_identifier="conversation_456",
trace_group_identifier="cohere_support_chat.workflow",
metadata={"plan": "pro", "workflow_name": "cohere_support_chat.workflow"},
):
response = client.chat(
model="command-a-03-2025",
messages=[{"role": "user", "content": "Summarize our support policy."}],
)| Attribute | Type | Description |
|---|---|---|
customer_identifier | str | Identifies the end user in Respan analytics. |
session_identifier | str | Groups related messages into a conversation. |
trace_group_identifier | str | Groups spans by workflow name. |
metadata | dict | Custom key-value pairs merged with default metadata. |
Examples
Streaming chat
stream = client.chat_stream(
model="command-a-03-2025",
messages=[{"role": "user", "content": "Write a short haiku about observability."}],
)
for event in stream:
if event.type == "content-delta":
print(event.delta.message.content.text, end="", flush=True)Embeddings
response = client.embed(
model="embed-v4.0",
input_type="search_document",
texts=[
"Respan records traces and spans for AI systems.",
"Cohere embeddings convert text into vectors.",
],
embedding_types=["float"],
)
print(len(response.embeddings.float_))Rerank
response = client.rerank(
model="rerank-v3.5",
query="Which document explains search result ranking?",
documents=[
"Rerank models reorder candidate documents by query relevance.",
"Chat models generate conversational responses.",
],
top_n=1,
)
print(response.results[0].index)