Revision 1, checked Oct 11, 2026. Installs exactly cohere 7.2.0, creates the files and adds the key names to your .env. It shows everything first and asks before changing anything.
Adapt to my stackProShowing the original (Python). Pick your stack to see it rewritten.
Free accounts can open one adapted recipe a month. Sign in to try it.
A small Python script that sends one question and five text passages to Cohere's Rerank API and gets back the passages ordered by how relevant each one is to the question. Each result has an index (the zero-based position of the passage in your original list) and a relevance_score between 0 and 1. You can put this after any search step (keyword, vector or database) to reorder the results before showing them or passing them to a chat model.
Prerequisites
Python 3.10 or newer (the cohere package requires >=3.10,<4.0)
A Cohere API key in CO_API_KEY. Create one at https://dashboard.cohere.com/api-keys. Trial keys are free but limited: 10 Rerank requests per minute and 1,000 API calls a month.
Steps
Install the official Cohere Python SDK.
Shell
pip install cohere==7.2.0
Set your API key as an environment variable.
Shell
export CO_API_KEY="your-api-key"
On Windows PowerShell use $env:CO_API_KEY = "your-api-key" instead.
Create rerank.py with the example from the Cohere Rerank overview. It uses the model rerank-v4.0-pro, passes the passages as a plain list of strings, and asks for the top 5 with top_n.
Python
import cohereco = cohere.ClientV2()query = "What is the capital of the United States?"docs = [ "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.", "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.", "Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.", "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.", "Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.",]results = co.rerank( model="rerank-v4.0-pro", query=query, documents=docs, top_n=5)
To use your own data, replace query with your question and docs with your own list of passages. Lower top_n to keep only the best few.
Test it
The script from the docs stores the response in results without printing it, so run it and print that variable:
Shell
python -c "import rerank; print(rerank.results)"
You should see a V2RerankResponse whose results list is ordered from most to least relevant. In the documented example output the first item is index=3 (the Washington, D.C. passage) with relevance_score=0.943264, followed by indexes 2, 1, 0 and 4. Your scores may differ slightly. To read the ranked passages, loop over results.results and look up docs[item.index].
Not confirmed
The official SDK README says cohere.Client() reads CO_API_KEY. The docs example calls cohere.ClientV2() with no key and the curl examples use CO_API_KEY, but no page read states in words that ClientV2() reads that variable.
The exact click path for creating a key on the dashboard was not opened (it needs a sign in). Only the URL https://dashboard.cohere.com/api-keys is given by the docs.
Pricing for Rerank was not checked. The example response shows usage billed as search_units.
The maximum number of documents per request was not stated on the pages read. The API reference lists an optional max_tokens_per_doc parameter with a default of 4096.
The example was not run in this research; the output above is the one printed in the docs.