Skip to main content

Rerank

/v1/rerank takes a query and a list of documents and returns the documents ordered by relevance with a score for each. Rerankers improve retrieval pipelines by re-scoring the candidates a vector search returned, so the model gets the few passages that matter instead of the twenty that merely match keywords. The gateway serves one request format for every rerank provider and prices calls per query or per token as the price map records.

Scenario​

A documentation assistant searches a vector store and gets twenty candidate passages back. Before building the prompt it sends the question and the candidates to a rerank model through the gateway and keeps the top three. The answers get better and the prompt gets shorter, and the rerank calls are logged and costed under the assistant's key next to its searches and completions.

Steps​

  1. Register a rerank model. In the Admin UI open Models + Endpoints (/ui/models-and-endpoints), click Add Model, pick the Provider (for example Cohere), choose the model, set the Public Model Name to rerank-v3.5 and set Mode to Rerank - /rerank so health checks send a rerank request, then click Add Model.

    model_list:
    - model_name: rerank-v3.5
    spanway_params:
    model: cohere/rerank-v3.5
    api_key: os.environ/COHERE_API_KEY
    model_info:
    mode: rerank
  2. Create a key in Virtual Keys (/ui/api-keys) and pick the model under Models.

  3. Rerank a few documents.

    curl -X POST http://localhost:4000/v1/rerank \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "rerank-v3.5",
    "query": "How do I rotate a virtual key?",
    "documents": [
    "Keys are rotated with POST /key/regenerate.",
    "Budgets reset on the schedule in budget_duration.",
    "Teams group users and keys."
    ],
    "top_n": 2,
    "return_documents": true
    }'
  4. Check the log. Open Logs (/ui/logs) and find the request with its cost.

Expected result​

{
"id": "rr-6a1f9b2c",
"results": [
{"index": 0, "relevance_score": 0.98, "document": {"text": "Keys are rotated with POST /key/regenerate."}},
{"index": 2, "relevance_score": 0.12, "document": {"text": "Teams group users and keys."}}
],
"meta": {"billed_units": {"search_units": 1}}
}

Request fields​

FieldMeaning
queryThe search query.
documentsStrings, or objects with a text field.
top_nNumber of results to return.
return_documentsInclude document text in results.
rank_fields, max_chunks_per_docProvider options forwarded where supported.

/v2/rerank and /rerank are the same endpoint. Rerank calls are cached when rerank is in the cache's supported_call_types. Rerank models are available from Cohere, Jina AI, Voyage, Bedrock, Azure AI, Together AI, Infinity, Hugging Face and others, listed below.

In retrieval pipelines​

The RAG query endpoint can rerank vector store results before generation with a rerank block naming one of these models, so you do not have to call this endpoint yourself.

Errors​

StatusWhat it meansFix
400 BadRequestErrorquery or documents is missing, documents is empty, or top_n is larger than the provider allows.Send a query and at least one document.
401 AuthenticationErrorThe key is missing, wrong or expired.Use a live key.
403 PermissionDeniedErrorThe key may not use the model.Add the model to the key.
404 NotFoundErrorNo deployment carries this model name.Check Models + Endpoints.
429 RateLimitErrorBudget or rate limit reached, or the provider is throttling.Raise the limit or add a deployment.
Cost shows 0The model has no per-query or per-token price.Set a price on the deployment with custom pricing.

Providers​

The gateway's endpoint support matrix marks 11 provider integrations as supporting this endpoint:

AIHubMix (aihubmix), AWS - Bedrock, Cohere, Fireworks AI, Hosted VLLM, Huggingface, Qianwen AI Platform, QwenCloud, Scaleway, VLLM, VoyageAI by MongoDB.