Rerank
A rerank model takes a query and a list of documents and scores how relevant each document is to the query. Use it after a fast first search (keyword, or embeddings) to put the best results on top before you show them or pass them to a chat model.
Rerank is not an OpenAI endpoint, so the OpenAI SDKs have no method for it. Call it over plain HTTP with the same API key. The request and response follow OpenRouter's rerank API, which is Cohere's rerank shape.
Endpoint
POST https://backend.sovereigneg.com/v1/rerank
Request
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Rerank model ID, e.g. cohere/rerank-v3.5 |
query | string | Yes | The search query to rank the documents against |
documents | array | Yes | 1 to 1,000 documents. Each one is a string, or an object with text and/or image |
top_n | integer | No | Return only the N most relevant documents (at least 1). Default: all of them |
return_documents | boolean | No | Whether each result repeats its document. Supported by Cohere-style models |
max_tokens_per_doc | integer | No | Cohere models: cut each document to this many tokens |
truncation | boolean | No | Voyage models: cut inputs that are longer than the model's context |
user | string | No | Your own ID for the end user (up to 256 characters) |
Any other field is ignored and never sent to the provider.
Images. On models whose input_modalities list image, a document can be {"image": "data:image/png;base64,..."} (or carry both text and image). Images must be inline base64 data URLs; hosted image URLs are refused. Up to 20 images per request.
Example
curl https://backend.sovereigneg.com/v1/rerank \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "cohere/rerank-v3.5",
"query": "What is the capital of Egypt?",
"documents": [
"Paris is the capital of France.",
"Cairo is the capital of Egypt.",
"Madrid is the capital of Spain."
],
"top_n": 2
}'import os
import httpx
response = httpx.post(
"https://backend.sovereigneg.com/v1/rerank",
headers={"Authorization": f"Bearer {os.environ['OPENAI_API_KEY']}"},
json={
"model": "cohere/rerank-v3.5",
"query": "What is the capital of Egypt?",
"documents": [
"Paris is the capital of France.",
"Cairo is the capital of Egypt.",
"Madrid is the capital of Spain.",
],
"top_n": 2,
},
timeout=30,
)
response.raise_for_status()
for result in response.json()["results"]:
print(result["index"], round(result["relevance_score"], 3))const response = await fetch("https://backend.sovereigneg.com/v1/rerank", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "cohere/rerank-v3.5",
query: "What is the capital of Egypt?",
documents: [
"Paris is the capital of France.",
"Cairo is the capital of Egypt.",
"Madrid is the capital of Spain.",
],
top_n: 2,
}),
})
const { results } = await response.json()
for (const r of results) console.log(r.index, r.relevance_score)Response
{
"id": "req_8f2c1a7d9e4b",
"model": "cohere/rerank-v3.5",
"results": [
{ "index": 1, "relevance_score": 0.98, "document": { "text": "Cairo is the capital of Egypt." } },
{ "index": 0, "relevance_score": 0.07, "document": { "text": "Paris is the capital of France." } }
],
"usage": { "total_tokens": 42, "search_units": 1, "cost": 0.1 }
}resultsis sorted from most to least relevant.indexis the document's position in thedocumentsyou sent, so you can map each result back to your own data.relevance_scoreis the model's score. Compare scores within one response; different models use different scales.idis the same request ID as theX-Request-Idheader. Quote it when you contact support.usage.costis what this request cost, in EGP. The otherusagefields come from the model's provider:total_tokenswhen it counts tokens,search_unitsfor Cohere models.
Rerank responses are never streamed: each request returns one JSON body.
Available models
Rerank models are listed in the model catalog under Modality: Rerank, or with GET https://backend.sovereigneg.com/v1/models where modality is "rerank". Each model page shows ready-to-run code for that model, and input_modalities tells you whether it takes images.
Pricing
Each rerank request is billed once, at the model's catalog price in EGP. The catalog shows that price in the model's own unit: per 1M input tokens (the query plus the documents) for most models, or per search for Cohere models. One search is one query with up to 100 documents, and a document longer than 500 tokens together with the query counts as one document per started 500 tokens. usage.cost on the response is that request's price. Your usage page shows what was actually charged, after your spend limits.
You need credit left to call a rerank model: with none, the request is refused with 402 before anything runs.
Errors
| Status | Code | Meaning |
|---|---|---|
| 400 | model_not_supported_on_endpoint | The model is not a rerank model (or a rerank model was sent to /v1/chat/completions). The message names the right endpoint |
| 400 | model_not_found | Unknown model ID |
| 400 | empty_input | A document object has neither text nor image. param names it |
| 400 | remote_media_not_supported / invalid_image_url | An image is a hosted URL, or its data URL is not a supported image type or is over the size limit. Inline it as a base64 data URL |
| 400 | too_many_image_parts | More than 20 images in one request |
| 400 | rerank_model_unpriced | The model has no rerank price yet, so it cannot be billed. Pick a rerank model with a price in the catalog |
| 400 | provider_user_error | The model's provider refused the request as sent; the message gives its reason. Fix the request; if the message doesn't point at your request, retry later or pick another model |
| 401 | missing_auth_header / invalid_api_key / key_revoked / key_expired / invalid_token | No Authorization: Bearer header; the sk- key is unknown, revoked or expired; or the token isn't an sk- key (invalid_token) |
| 402 | quota_exceeded | No spendable credit left. Top up in Dashboard → Billing |
| 402 | insufficient_balance | Your spendable credit is less than this request may cost; the message gives both amounts. Top up, or send fewer or shorter documents |
| 402 | spending_cap_reached / project_spend_cap_exceeded | The key's or the project's monthly spend cap is reached |
| 413 | request_too_large | The request body is over 32 MiB. Send fewer or smaller documents |
| 422 | invalid_request_body | Malformed request: a missing query, no documents, more than 1,000 documents, or top_n below 1. param names the field |
| 429 | rate_limit_exceeded | Too many requests for your plan — back off and retry |
| 503 | provider_backings_unavailable / no_callable_backing | The model is temporarily unavailable — retry shortly |