---
title: Integrate Cohere with Langfuse
sidebarTitle: Cohere
description: Guide on integrating Cohere with Langfuse via the OpenAI SDK for observability and debugging.
category: Integrations
logo: /images/integrations/cohere_icon.svg
---

# Observability for Cohere with Langfuse

This guide shows you how to integrate Cohere with Langfuse using the OpenAI SDK Compatibility API. Trace and monitor your applications seamlessly.

> **What is Cohere?** [Cohere](https://docs.cohere.com/docs/) is an AI platform that provides state-of-the-art language models via API, allowing developers to build applications with natural language understanding capabilities.

> **What is Langfuse?** [Langfuse](https://langfuse.com) is an open source AI engineering platform for tracing, monitoring, and debugging LLM applications.

## Step 1: Install Dependencies

Ensure you have the necessary Python packages installed:

```python
%pip install openai langfuse
```

## Step 2: Set Up Environment Variables

```python
import os

# Get keys for your project from the project settings page
# https://cloud.langfuse.com

os.environ.setdefault("LANGFUSE_PUBLIC_KEY", "pk-lf...");
os.environ.setdefault("LANGFUSE_SECRET_KEY", "sk-lf...");
os.environ.setdefault("LANGFUSE_BASE_URL", "https://cloud.langfuse.com"); # 🇪🇺 EU region
# Other Langfuse data regions include 🇺🇸 US: https://us.cloud.langfuse.com, 🇯🇵 Japan: https://jp.cloud.langfuse.com and ⚕️ HIPAA: https://hipaa.cloud.langfuse.com

# Set your Cohere API key from your Cohere account settings
os.environ.setdefault("COHERE_API_KEY", "...");
```

## Step 3: Use Cohere with the OpenAI SDK

Leverage the Compatibility API by replacing the base URL with Cohere's endpoint when initializing the client.

```python
# Instead of importing openai directly, use Langfuse's drop-in replacement
from langfuse.openai import openai

client = openai.OpenAI(
  api_key=os.environ.get("COHERE_API_KEY"),
  base_url="https://api.cohere.ai/compatibility/v1"  # Cohere Compatibility API endpoint
)
```

## Step 4: Run an Example

The example below demonstrates a basic chat completion request. All API calls are automatically traced by Langfuse.

```python
response = client.chat.completions.create(
  model="command-r7b-12-2024",  # Replace with the desired Cohere model
  messages=[
    {"role": "system", "content": "You are an assistant."},
    {"role": "user", "content": "Tell me about the benefits of using Cohere with Langfuse."}
  ],
  name="Cohere-Trace"
)

print(response.choices[0].message.content)
```

## Step 5: See Traces in Langfuse

After running the example, log in to Langfuse to view the detailed traces, including request parameters, response content, token usage, and latency metrics.

![Langfuse Trace Example](https://langfuse.com/images/cookbook/integration_cohere/cohere-example-trace.png)

_[Public example trace](https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/17d82424-f22f-46d1-a63b-6ec3e2c3da1e?timestamp=2025-03-05T11%3A35%3A26.398Z&observation=490e73b2-fdf5-40ad-95d7-a1d0bd054e0e)_

## Resources

- [Cohere Documentation](https://docs.cohere.com/docs/compatibility-api)
- [Langfuse](https://langfuse.com)
- [Langfuse OpenAI Integration Guide](https://langfuse.com/integrations/model-providers/openai-py)

<!-- agent-instructions -->

---

## Agent Instructions

This page is part of the [Langfuse](https://langfuse.com) documentation, published as plain Markdown for AI agents. Every page is available as Markdown by appending `.md` to its URL, or by sending an `Accept: text/markdown` header. This page: `https://langfuse.com/integrations/model-providers/cohere.md`.

### Querying these docs

If the answer is not on this page, query the documentation instead of guessing:

- **Semantic search** across all Langfuse docs, returning an answer with the relevant pages and excerpts. Ask a specific, self-contained question:

  ```bash
  curl -sG "https://langfuse.com/api/search-docs" --data-urlencode "query=How do I trace a LangGraph agent?"
  ```

- **Index of every page**: <https://langfuse.com/llms.txt>, with per-section indexes [llms-docs.txt](https://langfuse.com/llms-docs.txt), [llms-integrations.txt](https://langfuse.com/llms-integrations.txt), and [llms-self-hosting.txt](https://langfuse.com/llms-self-hosting.txt).

### Before writing Langfuse code

- **Install the [Langfuse Agent Skill](https://langfuse.com/docs/api-and-data-platform/features/agent-skill).** It encodes Langfuse's own best practices for instrumentation, prompt management, and evaluation, and materially improves results.
- **Read [What does a good trace look like?](https://langfuse.com/docs/observability/best-practices.md)** before instrumenting an application.
- **Verify endpoints, parameters, and response fields** against the [API reference](https://api.reference.langfuse.com) instead of inferring them from code examples.
- **Use the [Langfuse CLI](https://langfuse.com/docs/api-and-data-platform/features/cli)** (`npx langfuse-cli api <resource> <action>`) to read or write traces, prompts, datasets, and scores from the terminal.

Found an error in these docs? Please open an issue at <https://github.com/langfuse/langfuse-docs/issues>.
