Rerank

Re-rank a list of documents against a query using Cohere, Voyage, Jina, or Mixedbread reranker models.

Overview

The Rerank API takes a query and a list of candidate documents and returns the documents re-sorted by relevance. Reranking is the standard second stage of a retrieval pipeline: a vector index returns 50-100 candidates by embedding similarity, then a reranker uses a cross-encoder to score each (query, document) pair and surface the top K.

AllRoutes supports the major commercial rerankers behind a single endpoint, normalizing Cohere's, Voyage's, Jina's, and Mixedbread's wire formats so you can swap providers by changing one model identifier.

Endpoint

POST https://api.allroutes.ai/v1/rerank

Request Body

FieldTypeRequiredDescription
modelstringYesReranker model identifier (e.g., rerank-english-v3.0, voyage-rerank-2)
querystringYesThe query to rank documents against
documentsarrayYesArray of strings or {text: string} objects
top_nintegerNoReturn only the top N results (default: all)
return_documentsbooleanNoInclude document text in the response (default: true)
max_chunks_per_docintegerNoCohere/Jina chunking limit

Example Request

curl https://api.allroutes.ai/v1/rerank \
  -H "Authorization: Bearer allroutes_sk_..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "rerank-english-v3.0",
    "query": "What is the capital of France?",
    "documents": [
      "Paris is the capital and most populous city of France.",
      "Berlin is the capital of Germany.",
      "The Eiffel Tower is in Paris.",
      "France has a population of about 67 million.",
      "Madrid is the capital of Spain."
    ],
    "top_n": 3
  }'

Response Format

{
  "id": "rerank-abc123",
  "model": "rerank-english-v3.0",
  "results": [
    {
      "index": 0,
      "relevance_score": 0.987,
      "document": {"text": "Paris is the capital and most populous city of France."}
    },
    {
      "index": 2,
      "relevance_score": 0.612,
      "document": {"text": "The Eiffel Tower is in Paris."}
    },
    {
      "index": 3,
      "relevance_score": 0.481,
      "document": {"text": "France has a population of about 67 million."}
    }
  ],
  "usage": {
    "search_units": 5,
    "billed_units": {"search_units": 5}
  }
}
FieldDescription
results[].indexPosition of the document in the original documents array
results[].relevance_scoreScore 0.0-1.0; higher is more relevant
results[].document.textOriginal text (omitted when return_documents: false)
usage.search_unitsBilling unit (one per query-document pair)

Results are always sorted by relevance_score descending.

Supported Models

ModelProviderLanguagesMax Doc Tokens
rerank-english-v3.0CohereEnglish4096
rerank-multilingual-v3.0Cohere100+ languages4096
voyage-rerank-2Voyage AIEnglish16000
voyage-rerank-2-liteVoyage AIEnglish8000
jina-reranker-v2-base-multilingualJina100+ languages8192
mxbai-rerank-large-v1MixedbreadEnglish512
mxbai-rerank-base-v1MixedbreadEnglish512

SDK Examples

Python

import os
from allroutes import AllRoutesClient

client = AllRoutesClient(api_key=os.environ["ALLROUTES_API_KEY"])

results = client.rerank(
    model="rerank-english-v3.0",
    query="What is the capital of France?",
    documents=[
        "Paris is the capital and most populous city of France.",
        "Berlin is the capital of Germany.",
        "The Eiffel Tower is in Paris.",
    ],
    top_n=2,
)

for r in results["results"]:
    print(f"#{r['index']} score={r['relevance_score']:.3f}: {r['document']['text']}")

Node.js

const results = await client.rerank.create({
  model: "rerank-english-v3.0",
  query: "What is the capital of France?",
  documents: [
    "Paris is the capital and most populous city of France.",
    "Berlin is the capital of Germany.",
    "The Eiffel Tower is in Paris.",
  ],
  top_n: 2,
});

for (const r of results.results) {
  console.log(`#${r.index} score=${r.relevance_score.toFixed(3)}: ${r.document.text}`);
}

Two-Stage Retrieval Pattern

The canonical RAG flow combines Embeddings (recall) with reranking (precision):

# Stage 1: vector search returns 50 candidates
candidates = vector_store.search(query, k=50)

# Stage 2: rerank to top 5
reranked = client.rerank(
    model="rerank-english-v3.0",
    query=query,
    documents=[c.text for c in candidates],
    top_n=5,
)

top_docs = [candidates[r["index"]] for r in reranked["results"]]

A reranker typically lifts top-5 precision by 15-30 points over pure embedding similarity, especially for queries that share keywords with non-relevant documents.

Pricing

Rerankers bill in search units, not tokens. One search unit equals one (query, document) pair. Costs vary by provider:

ModelCost per 1K search units
Cohere v3$2.00
Voyage Rerank 2$0.05
Jina v2$0.02
Mixedbread large$0.10

Use BYOK for 0% commission on top of these provider rates.

Error Codes

StatusDescription
400Empty documents array, query too long, or invalid model
401Missing or invalid API key
402Insufficient credits
413Document exceeds the model's token limit
429Rate limit exceeded

Troubleshooting

  • All scores are low (< 0.1) -- the query and documents are likely in different languages. Switch to a multilingual model.
  • Top result looks wrong -- check that you didn't truncate documents below their semantic core. Try voyage-rerank-2 for longer contexts.
  • Slow responses -- Cohere multilingual is the slowest; Mixedbread is fastest. Cache aggressively when query patterns repeat.

See Also

  • Embeddings -- the first stage of two-stage retrieval
  • Models API -- list available reranker models with pricing
  • Caching -- exact-match cache covers reranks too
  • Cost Optimization -- right-size your reranker for your latency budget