For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
DiscordPlatform
DocumentationIntegrationsAPI referenceSDKsChangelog
DocumentationIntegrationsAPI referenceSDKsChangelog
    • Overview
  • Tracing
      • OpenAI SDK
      • Instructor
      • Anthropic SDK
      • Google GenAI
      • LiteLLM
      • RubyLLM
      • Vertex AI
      • AWS Bedrock
      • Cohere
      • Groq
      • Mistral AI
      • Ollama
      • Portkey
      • Watsonx
      • Together AI
      • Aleph Alpha
      • HuggingFace
      • Replicate
      • SageMaker
      • Respan API
  • Gateway
  • Others
  • Migrating
    • Braintrust
    • Langfuse
    • Portkey
LogoLogo
DiscordPlatform
On this page
  • What is Cohere?
  • Setup
  • Configuration
  • Attributes
  • Examples
  • Streaming chat
  • Embeddings
  • Rerank
TracingLLM SDKs

Cohere (tracing)

Was this page helpful?
Previous

Groq (tracing)

Next
Built with
Set up Respan
  1. Sign up - Create an account at platform.respan.ai
  2. Create an API key - Generate one on the API keys page
Use Respan Gateway

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

Use AI

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 Python SDK provides chat, streaming chat, embedding, and rerank APIs. respan-instrumentation-cohere activates the Cohere OpenTelemetry wrapper and normalizes the emitted 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"

Optional:

$export RESPAN_BASE_URL="https://api.respan.ai/api"
$export COHERE_CHAT_MODEL="command-a-03-2025"
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
34try:
35 with respan.propagate_attributes(
36 trace_group_identifier="cohere_chat.workflow",
37 metadata={"workflow_name": "cohere_chat.workflow"},
38 ):
39 print(run_chat())
40finally:
41 respan.telemetry.flush()
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.
use_legacy_attributesboolTrueEmits span attributes compatible with the existing Respan span contract.
**instrumentor_kwargsdict{}Extra keyword arguments forwarded to the upstream instrumentor.

Attributes

Attach customer identifiers, thread IDs, and metadata to Cohere calls with propagate_attributes.

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)