Skip to main content

Knowledge base retrieval

The gateway can turn a plain chat completion into a retrieval augmented one. When a request names one or more vector stores, the gateway searches them with the user's latest message, adds the matching chunks to the prompt, and then calls the model. The application sends a normal chat request and gets a grounded answer, without running a retrieval pipeline of its own.

Scenario​

An HR team wants its chat assistant to answer policy questions from the handbook instead of from the model's general knowledge. The admin registers the handbook's vector store on the gateway and attaches it to the team's key. From then on every question the team's application sends is answered with passages from the handbook, and the Logs page shows which passages the model saw.

Steps​

  1. Register the vector store. In the Admin UI open Vector Stores (/ui/vector-stores), click + Add Vector Store, pick the Provider, enter the Vector Store ID and the provider fields, give it a Vector Store Name and click Create. To build a store from documents instead, use the Create Vector Store tab, see Vector Stores page.

  2. Grant the store to the key. Open Virtual Keys (/ui/api-keys), open the key, click Settings, pick the store under Vector Stores and save. The grant only allows the key to use the store. Nothing is retrieved until a request names the store, so each request still sends vector_store_ids (or a file_search tool) as in the next step.

  3. Ask a question.

    curl -X POST http://localhost:4000/v1/chat/completions \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{
    "model": "claude-sonnet-5-5",
    "messages": [{"role": "user", "content": "What is our parental leave policy?"}],
    "vector_store_ids": ["vs_hr_policies"]
    }'
  4. See what the model saw. Open Logs (/ui/logs), open the request and read the Vector Store Requests section of the detail drawer, which lists the query, the store and the search results that were added to the prompt.

Expected result​

The response is a normal chat completion whose answer quotes the handbook. The log entry shows the retrieved chunks with their scores, and the cost of the search and of the embedding call made to search are recorded under the same request id as the completion.

Registering vector stores in config​

vector_store_registry:
- vector_store_name: hr-policies
spanway_params:
vector_store_id: vs_hr_policies
custom_llm_provider: openai
api_key: os.environ/OPENAI_API_KEY
vector_store_description: HR policies and handbook

See create a vector store and search a vector store for the supported providers and the search API.

Attaching vector stores to keys and teams​

Grant vector stores to a key or team in its object_permission.vector_stores to control which stores it may use. A grant is permission only. It does not attach the stores to requests, so the client still names the stores in vector_store_ids or a file_search tool on each request. With general_settings.vector_store_deny_by_default: true, a key can only use vector stores it has been granted.

Ingesting documents​

The RAG ingest endpoint uploads a file or URL, extracts text, chunks it, embeds it and writes it to a vector store in one call. The RAG query endpoint combines retrieval, optional reranking and generation, with more control over retrieval than the chat completion shortcut on this page.

Errors​

StatusWhat it meansFix
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.
400 The request could not be grounded in every configured vector store. <n> vector store search(es) failed: ..., code vector_store_search_failedvector_store_search_failure_mode is error and one store's search failed.Fix the store, or set the mode back to annotate to answer without that store.
403 Access denied: You do not have permission to access this vector storeThe registered store belongs to another team.Use a key of the owning team.
Answer ignores the documents and the log shows no vector store requestThe id in vector_store_ids is not registered on the gateway. Chat completions skip unregistered ids, so no retrieval happens and no error is returned.Register it on the Vector Stores page or in vector_store_registry.
Answer ignores the documentsThe search returned no chunks, for example because the store is empty or the embedding model differs from the one that built the index.Run a test search on the Test Vector Store tab of the Vector Stores page.
401 or 403 from the provider in the logThe store's credential is wrong.Fix the credential on the store.

Logs​

LevelLineMeaningAction
WARNINGVector store search failed for vector_store_id=<id>, continuing without its context: <error>One store's search failed, so the model answered without that store's chunks.Check the store's credential and the provider. Set vector_store_search_failure_mode to fail the request instead.
WARNINGUnsupported vector_store_search_failure_mode=<value>, falling back to <default>. Supported modes: ...The setting has a value the gateway does not know.Use one of the listed modes.
ERRORError in VectorStorePreCallHook for vector_store_ids=<ids>: <error>Retrieval failed as a whole, so the request went to the model without any documents.Read the error, usually the store's registration or credential.
ERRORError adding search results to response: <error> or ... to streaming chunk: <error>The answer was sent, but without the list of chunks it used.None unless it repeats.

The other vector store lines are in the log reference.