> This page is for version v2 API (default).
> For other versions, use one of these documentation indexes:
> - v2 API (default): https://docs.cohere.com/v2/llms.txt
> - v1 API: https://docs.cohere.com/v1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.cohere.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.cohere.com/_mcp/server.

# Reranking - quickstart

> A quickstart guide for performing reranking with Cohere's Reranking models (v2 API).

## About reranking

Cohere's reranking models are available via the Rerank endpoint. This endpoint provides a powerful semantic boost to the search quality of any keyword or vector search system.

This quickstart guide shows you how to perform reranking with the Rerank endpoint.

## Setup

First, install the Cohere Python SDK with the following command.

```bash
pip install -U cohere
```

Next, import the library and create a client.

#### Cohere Platform

**`PYTHON`**

```python PYTHON
import cohere

co = cohere.ClientV2(
    "COHERE_API_KEY"
)  # Get your free API key here: https://dashboard.cohere.com/api-keys
```

#### Private Deployment

**`PYTHON`**

```python PYTHON
import cohere

co = cohere.ClientV2(
    api_key="",  # Leave this blank
    base_url="<YOUR_DEPLOYMENT_URL>",
)
```

#### Bedrock

**`PYTHON`**

```python PYTHON
import cohere

co = cohere.BedrockClientV2(
    aws_region="AWS_REGION",
    aws_access_key="AWS_ACCESS_KEY_ID",
    aws_secret_key="AWS_SECRET_ACCESS_KEY",
    aws_session_token="AWS_SESSION_TOKEN",
)

# Get the model name: https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
```

#### SageMaker

**`PYTHON`**

```python PYTHON
import cohere

co = cohere.SagemakerClientV2(
    aws_region="AWS_REGION",
    aws_access_key="AWS_ACCESS_KEY_ID",
    aws_secret_key="AWS_SECRET_ACCESS_KEY",
    aws_session_token="AWS_SESSION_TOKEN",
)
```

#### Azure AI

**`PYTHON`**

```python PYTHON
import cohere

co = cohere.ClientV2(
    api_key="AZURE_API_KEY",
    base_url="AZURE_ENDPOINT",  # example: "https://cohere-command-r-plus-08-2024-xyz.eastus.models.ai.azure.com/"
)
```

## Retrieved Documents

First, define the list of documents to be reranked.

**`PYTHON`**

```python PYTHON
documents = [
    "Reimbursing Travel Expenses: Easily manage your travel expenses by submitting them through our finance tool. Approvals are prompt and straightforward.",
    "Working from Abroad: Working remotely from another country is possible. Simply coordinate with your manager and ensure your availability during core hours.",
    "Health and Wellness Benefits: We care about your well-being and offer gym memberships, on-site yoga classes, and comprehensive health insurance.",
    "Performance Reviews Frequency: We conduct informal check-ins every quarter and formal performance reviews twice a year.",
]
```

## Reranking

Then, perform reranking by passing the documents and the user query to the Rerank endpoint.

#### Cohere Platform

**`PYTHON`**

```python PYTHON
# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents

results = co.rerank(
    model="rerank-v4.0-pro", query=query, documents=documents, top_n=2
)

for result in results.results:
    print(result)
```

#### Private Deployment

**`PYTHON`**

```python PYTHON
# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents
results = co.rerank(
    model="rerank-v4.0-pro", query=query, documents=documents, top_n=2
)

for result in results.results:
    print(result)
```

#### Bedrock

**`PYTHON`**

```python PYTHON
# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents

results = co.rerank(
    model="YOUR_MODEL_NAME", query=query, documents=documents, top_n=2
)

for result in results.results:
    print(result)
```

#### SageMaker

**`PYTHON`**

```python PYTHON
# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents
results = co.rerank(
    model="YOUR_ENDPOINT_NAME",
    query=query,
    documents=documents,
    top_n=2,
)

for result in results.results:
    print(result)
```

#### Azure AI

**`PYTHON`**

```python PYTHON
# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents

results = co.rerank(
    model="model",  # Pass a dummy string
    query=query,
    documents=documents,
    top_n=2,
)

for result in results.results:
    print(result)
```

```mdx wordWrap
document=None index=2 relevance_score=0.115670934
document=None index=1 relevance_score=0.01729751
```

## Further Resources

* [Rerank endpoint API reference](https://docs.cohere.com/reference/rerank)
* [Documentation on reranking](https://docs.cohere.com/docs/rerank-overview)
* [LLM University chapter on reranking](https://cohere.com/llmu/reranking)