Rerank
POST /v1/rerank scores each document in a list by how relevant it is to a
query and returns them ranked, most relevant first. It is the usual second
step of retrieval-augmented generation: embeddings find a
few dozen or hundred candidates quickly, and a reranker reads the query and
each candidate together to put the best ones on top before they go into the
prompt.
Rerank models are served by Voyage AI. List them with
GET /v1/models, where they carry "kind": "rerank", and
check the model catalog for availability and pricing.
Quick start
curl -X POST "https://api.llmapi.ai/v1/rerank" \
-H "Authorization: Bearer $LLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "rerank-3",
"query": "When is the office closed?",
"documents": [
"The cafeteria serves lunch from 12:00 to 14:00.",
"The office is closed on public holidays and on 24 December.",
"Parking permits are renewed every January."
],
"top_k": 2
}'
{
"object": "list",
"data": [
{ "index": 1, "relevance_score": 0.8203125 },
{ "index": 0, "relevance_score": 0.2353515625 }
],
"model": "rerank-3",
"usage": {
"total_tokens": 61,
"cost_usd_input": 0.00000305,
"cost_usd_output": 0
}
}
data is sorted by relevance_score, highest first. index is the
document's position in the documents array you sent, so you can look the
text up on your side without asking for it back.
Request fields
| Field |
Required |
Description |
model |
Yes |
Rerank model ID. Prefix it with the provider (voyageai/rerank-3) to pin one. |
query |
Yes |
The search query, as a string. |
documents |
Yes |
The candidates to score: an array of 1 to 1,000 strings. |
top_k |
No |
Return only this many of the highest-scoring documents. A positive integer; omit it to get every document back. |
return_documents |
No |
true adds each document's text to its result as document. Defaults to false. |
truncation |
No |
true (the default) shortens a query or document that exceeds the model's limits. false rejects such a request with 400. |
Response fields
| Field |
Description |
data[].index |
Position of the document in the request's documents array. |
data[].relevance_score |
Relevance of the document to the query. Higher is more relevant. |
data[].document |
The document's text, present only when return_documents is true. |
model |
The provider's name for the model that served the request, which can differ from the ID you sent. |
usage.total_tokens |
Tokens billed for the request; see Billing. |
usage.cost_usd_* |
The request's cost at the model's price, unless your organization has turned cost reporting off. |
Limits
- Up to 1,000 documents per request.
- The query and each query-plus-document pair have per-model token limits; for the 2.5 family they are 8,000 tokens for the query and 32,000 for the query with any single document. With
truncation on, a query or document over its limit is shortened instead of rejected.
- The whole request also has a total token limit: 600,000 for the 2.5 family.
- A request Voyage rejects for a limit fails with
400 and Voyage's own message naming the limit.
Billing
A rerank request is charged per token at the model's input price. The billed
count is usage.total_tokens, which Voyage computes as the query's tokens once
for every document plus the tokens of all documents:
total_tokens = query_tokens × number_of_documents + sum(document_tokens)
A long query is therefore multiplied by the length of the list. Keep the query
short and send only the candidates worth scoring. Credits are reserved from an
estimate before the call and settled on the reported count. Requests served
with your organization's own Voyage key are not charged to your credits;
their response still reports cost_usd_* at the model's price.
Python example
import os
import requests
documents = [
"The cafeteria serves lunch from 12:00 to 14:00.",
"The office is closed on public holidays and on 24 December.",
"Parking permits are renewed every January.",
]
response = requests.post(
"https://api.llmapi.ai/v1/rerank",
headers={"Authorization": f"Bearer {os.environ['LLM_API_KEY']}"},
json={"model": "rerank-3", "query": "When is the office closed?", "documents": documents, "top_k": 2},
timeout=30,
)
response.raise_for_status()
for result in response.json()["data"]:
print(f"{result['relevance_score']:.3f} {documents[result['index']]}")
Notes
- Responses arrive in one piece. There is no streaming, no batch submission, and no provider fallback.
- The gateway checks
model, query, documents and top_k before forwarding, so a missing field, an empty or over-long document list, or a non-positive top_k returns 400 naming the field.
- A request can also fail for an unknown model, insufficient credits, a rate limit, or a provider error; provider rejections keep the provider's message.
API operation: POST /v1/rerank