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.

1{
2 "mcpServers": {
3 "respan-docs": {
4 "url": "https://mcp.respan.ai/mcp/docs"
5 }
6 }
7}

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

1import os
2
3import cohere
4from dotenv import load_dotenv
5from respan import Respan, workflow
6from respan_instrumentation_cohere import CohereInstrumentor
7
8load_dotenv()
9
10respan = Respan(
11 api_key=os.environ["RESPAN_API_KEY"],
12 base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
13 instrumentations=[CohereInstrumentor()],
14)
15client = cohere.ClientV2(api_key=os.environ["CO_API_KEY"])
16
17
18@workflow(name="cohere_chat.workflow")
19def run_chat() -> str:
20 response = client.chat(
21 model=os.getenv("COHERE_CHAT_MODEL", "command-a-03-2025"),
22 messages=[
23 {
24 "role": "user",
25 "content": "In one sentence, explain what Cohere rerank is useful for.",
26 }
27 ],
28 temperature=0.1,
29 max_tokens=80,
30 )
31 return response.message.content[0].text
32
33
34with respan.propagate_attributes(
35 trace_group_identifier="cohere_chat.workflow",
36 metadata={"workflow_name": "cohere_chat.workflow"},
37):
38 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.

1with respan.propagate_attributes(
2 customer_identifier="user_123",
3 thread_identifier="conversation_456",
4 trace_group_identifier="cohere_support_chat.workflow",
5 metadata={"plan": "pro", "workflow_name": "cohere_support_chat.workflow"},
6):
7 response = client.chat(
8 model="command-a-03-2025",
9 messages=[{"role": "user", "content": "Summarize our support policy."}],
10 )
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

1stream = client.chat_stream(
2 model="command-a-03-2025",
3 messages=[{"role": "user", "content": "Write a short haiku about observability."}],
4)
5
6for event in stream:
7 if event.type == "content-delta":
8 print(event.delta.message.content.text, end="", flush=True)

Embeddings

1response = client.embed(
2 model="embed-v4.0",
3 input_type="search_document",
4 texts=[
5 "Respan records traces and spans for AI systems.",
6 "Cohere embeddings convert text into vectors.",
7 ],
8 embedding_types=["float"],
9)
10print(len(response.embeddings.float_))

Rerank

1response = client.rerank(
2 model="rerank-v3.5",
3 query="Which document explains search result ranking?",
4 documents=[
5 "Rerank models reorder candidate documents by query relevance.",
6 "Chat models generate conversational responses.",
7 ],
8 top_n=1,
9)
10print(response.results[0].index)