Skip to navigation

Cohere (tracing)

  1. Sign up - Create an account at platform.respan.ai
  2. Create an API key - Generate one on the API keys page

See Cohere gateway setup to route this integration through the Respan gateway.

Add the Docs MCP to your AI coding tool to get help building with Respan. No API key needed.

{
"mcpServers": {
"respan-docs": {
"url": "https://mcp.respan.ai/mcp/docs"
}
}
}

What 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

1

Install packages

pip install respan-ai respan-instrumentation-cohere cohere python-dotenv
2

Set 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.

3

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())
4

View your trace

Open the Traces page and search for the workflow name cohere_chat.workflow.

Configuration

ParameterTypeDefaultDescription
exception_loggerlogging.Logger | NoneNoneOptional logger passed to the underlying Cohere OpenTelemetry instrumentor.
sdkModuleobject | undefinedundefinedTypeScript only. Pass the imported cohere-ai module so the instrumentor patches the same module instance used by your app.
use_legacy_attributesboolTruePython only. Emits span attributes compatible with the existing Respan span contract.
**instrumentor_kwargsdict{}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",
thread_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."}],
)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
trace_group_identifierstrGroups spans by workflow name.
metadatadictCustom 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)