> For the complete documentation index, see [llms.txt](https://docs.nexos.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nexos.ai/gateway-api/rerank.md).

# Rerank

Rerank documents by relevance to a query.

## Rerank documents

> Rerank documents by relevance to a query.

```json
{"openapi":"3.0.0","info":{"title":"Nexos AI Public API Production","version":"1.0.0"},"tags":[{"name":"Rerank","description":"Rerank documents by relevance to a query."}],"servers":[{"url":"https://api.nexos.ai"}],"security":[{"bearerAuth":[]},{"apiKeyHeader":[]}],"components":{"securitySchemes":{"bearerAuth":{"type":"http","scheme":"bearer"},"apiKeyHeader":{"type":"apiKey","in":"header","name":"X-Api-Key","description":"Authenticate by sending your nexos API key in the `X-Api-Key` header (e.g. `X-Api-Key: user-...` for a user key or `X-Api-Key: team-...` for a team key). This is an alternative to the `Authorization: Bearer` scheme. If both `X-Nexos-Key` and `X-Api-Key` are sent, `X-Nexos-Key` takes precedence."}},"schemas":{"RerankRequest":{"type":"object","properties":{"model":{"type":"string","description":"Reranking model identifier."},"query":{"type":"string","description":"Query used to rank the documents."},"documents":{"type":"array","description":"Documents to rank by relevance to the query.","minItems":1,"items":{"oneOf":[{"type":"string"},{"type":"object","required":["text"],"properties":{"text":{"type":"string","description":"Document text."}},"additionalProperties":true}]}},"top_n":{"type":"integer","description":"Maximum number of results to return.","minimum":1},"return_documents":{"type":"boolean","description":"Whether to include document text in the results.","default":false},"max_tokens_per_doc":{"type":"integer","description":"Maximum number of tokens per document. Supported by Cohere.","minimum":1},"priority":{"type":"integer","description":"Provider request-priority hint. Supported by Cohere."},"rank_fields":{"type":"array","description":"Object fields used for ranking. Supported by Pinecone.","items":{"type":"string"}},"truncation":{"type":"boolean","description":"Whether to truncate oversized documents. Supported by Voyage."},"parameters":{"type":"object","description":"Additional provider-specific parameters.","additionalProperties":true}},"required":["model","query","documents"]},"RerankResponseResultDocument":{"type":"object","description":"Returned document content and provider-specific metadata.","required":["text"],"properties":{"text":{"type":"string","description":"Document text."}},"additionalProperties":true},"RerankResponseMeta":{"type":"object","description":"Provider response metadata.","properties":{"billed_units":{"type":"object","description":"Units billed by the provider.","properties":{"search_units":{"type":"number","format":"double","description":"Search units billed for the request."}}},"api_version":{"type":"object","description":"API version reported by the provider.","additionalProperties":true}},"additionalProperties":true},"RerankResponseUsage":{"type":"object","description":"Provider-specific usage information.","properties":{"billed_units":{"type":"object","description":"Units billed by the provider, when reported at the top level.","properties":{"search_units":{"type":"number","format":"double","description":"Search units billed for the request."}}},"prompt_tokens":{"type":"integer","description":"The number of tokens used in the prompt"},"completion_tokens":{"type":"integer","description":"The number of completion tokens reported by the provider"},"total_tokens":{"type":"integer","description":"The total number of tokens used"},"prompt_tokens_details":{"type":"object","description":"Breakdown of the tokens used in the prompt","properties":{"cached_tokens":{"type":"integer","description":"The number of cached tokens in the prompt"},"cache_write_tokens":{"type":"integer","description":"The number of prompt tokens written to the cache"}}},"nexos_credits_cost":{"type":"number","format":"double","description":"Cost of the request in nexos credits."}},"additionalProperties":true}},"responses":{"RerankResponse":{"description":"Represents a reranking response returned by the selected model.","content":{"application/json":{"schema":{"type":"object","properties":{"id":{"type":"string","description":"Request identifier."},"object":{"type":"string","description":"Response object type."},"model":{"type":"string","description":"The reranking model used."},"provider":{"type":"string","description":"Provider that handled the request."},"results":{"type":"array","description":"Documents sorted by relevance.","items":{"type":"object","properties":{"index":{"type":"integer","description":"Original index of the document."},"relevance_score":{"type":"number","format":"double","description":"Provider-dependent relevance score."},"document":{"$ref":"#/components/schemas/RerankResponseResultDocument"}}}},"meta":{"$ref":"#/components/schemas/RerankResponseMeta"},"usage":{"$ref":"#/components/schemas/RerankResponseUsage"}}}}}},"OutOfCredits":{"description":"The company has run out of AI credits. Returned when the company's budget limit has been reached.\n","content":{"application/json":{"schema":{"type":"object","required":["error"],"properties":{"error":{"type":"object","required":["message"],"properties":{"message":{"type":"string"}}}}}}}}}},"paths":{"/v1/rerank":{"post":{"operationId":"post-rerank-v1","summary":"Rerank documents","tags":["Rerank"],"description":"Rerank documents by relevance to a query.","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/RerankRequest"}}}},"responses":{"200":{"$ref":"#/components/responses/RerankResponse"},"400":{"description":"Invalid request data."},"402":{"$ref":"#/components/responses/OutOfCredits"},"500":{"description":"Server error."}}}}}}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.nexos.ai/gateway-api/rerank.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
