Skip to main content

RAG query

POST /v1/rag/query answers a question with retrieval augmented generation in a single request. The gateway takes the last user message as the query, searches a vector store, optionally reranks the results, adds the top chunks to the prompt, and calls the model. The response is a normal chat completion with the retrieved context recorded, so applications get grounded answers without orchestrating retrieval themselves.

Scenario​

An internal help desk bot answers questions from the IT knowledge base. Each question goes to the RAG query endpoint with the knowledge base store, a rerank model to pick the best three passages, and the chat model that writes the answer. The bot reads the answer from choices[0].message.content, and the IT team reviews in the Logs page which passages the model used when an answer looks wrong.

Steps​

  1. Register the models and the store. In the Admin UI open Models + Endpoints (/ui/models-and-endpoints) and add the chat model and, optionally, a rerank model (Mode Rerank - /rerank). Load documents into the store with RAG ingest.

  2. Create a key in Virtual Keys (/ui/api-keys) that may use the chat model, the rerank model and the vector store.

  3. Ask a question.

    curl -X POST http://localhost:4000/v1/rag/query \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "claude-sonnet-5-5",
    "messages": [{"role": "user", "content": "How many weeks of parental leave do we offer?"}],
    "retrieval_config": {"vector_store_id": "vs_hr_policies", "custom_llm_provider": "openai", "top_k": 8},
    "rerank": {"enabled": true, "model": "rerank-v3.5", "top_n": 3, "return_documents": true},
    "stream": false
    }'
  4. See what the model saw. Open Logs (/ui/logs), open the request and read the Vector Store Requests section with the query and the retrieved chunks. The rerank call and the completion appear under the same request.

Expected result​

{
"id": "chatcmpl-8f6c2a1e",
"object": "chat.completion",
"created": 1760441234,
"model": "claude-sonnet-5-5",
"choices": [{"index": 0, "message": {"role": "assistant", "content": "The handbook grants sixteen weeks of paid parental leave.", "tool_calls": null, "function_call": null}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 412, "completion_tokens": 18, "total_tokens": 430}
}

With stream: true the answer arrives as chat completion chunks.

Request fields​

FieldMeaning
modelModel that generates the answer.
messagesConversation. The last user message is the retrieval query.
retrieval_configvector_store_id, custom_llm_provider, top_k, and filters or retrieval_filter in the backend's filter format.
rerankenabled, model (a rerank model on the gateway), top_n and return_documents.
streamStream the generated answer.

How the steps are costed​

The vector store search, the rerank call and the completion are each logged under the same request id with their own cost, so the spend log shows the full cost of the answer and the Logs page shows which chunks the model received.

Access control​

The key must have access to the vector store, the rerank model and the generation model. With general_settings.vector_store_deny_by_default: true, stores must be granted explicitly.

Errors​

StatusWhat it meansFix
400 BadRequestErrormodel, messages or retrieval_config is missing.Send all three fields.
400 messages must contain a user message with the question to answer.messages has no user message to use as the query.Add a user message.
401 key_vector_store_access_denied (or team_ or user_)The key, its team or its user lists vector stores and leaves this one out, or vector_store_deny_by_default is on and none of them grants it.Grant the store on the key and its team.
403 PermissionDeniedErrorThe store belongs to another team, or the key is not granted the rerank model or the chat model.Use a key of the owning team, or grant the models.
404 NotFoundErrorThe store or a model name is unknown.Check Vector Stores and Models + Endpoints.
Answer says it has no informationThe search returned no chunks.Test the store on the Test Vector Store tab and check that documents were ingested.
429 RateLimitErrorBudget or rate limit reached on one of the three calls.Raise the limit on the key.
500 RAG query failed. The gateway log has the cause.An unexpected failure inside the gateway. A provider error keeps the provider's status and message instead.Look for the RAG Query failed line in the gateway log.

Logs​

LevelLineWhat it meansAction
ERRORRAG Query failed: <cause>The query stopped on an error. The caller got the provider's status, a 400 for a missing question, or a 500.Action needed when the caller got a 500. Read the cause.