Query a Knowledge base
POST /v1/knowledge-bases/{knowledge_base_id}/query
| Property | Value |
|---|---|
| Operation | query_knowledge_base |
| Group | Knowledge bases |
| Scope | knowledge:read |
Scopes of include values | names needs knowledge:read; text needs knowledge:read |
| Route class | read |
| Idempotency | Idempotency-Key accepted: a retry with the same key replays the first answer |
| Audit | An audited content read knowledge.knowledge_base.query, when the request names an include value |
| Released in | 1.53.0 |
| Phase 0 host | Served (the hosted api pins 1.65.0) |
Runs hybrid retrieval over one version of the Knowledge base, the active one unless version names another or working_set, and answers the passages with their citations (1.53.0; knowledge-bases.md section 6). Semantic and keyword candidates, each restricted to the version's manifest and the filter, are fused by reciprocal rank fusion, then dropped below min_score, cut at top_k and packed within max_context_tokens. Without a content class in include the passages carry no text and the citations no names, and nothing is audited; naming text or names makes the query an audited content read. No version named and none active is 422 validation_failed with knowledge_base_no_active_version; a version that does not exist is 404 not_found, and a retired one 409 knowledge_base_version_retired. An archived Knowledge base still answers. From 1.54.0 the api runs no model, so it asks a worker of the ingest role for the query's vector through a query embedding task (KnowledgeQueryTask), holding the query sealed in that task for the wait only, and answers 503 dependency_unavailable when no vector comes within 5 seconds (a target) or the worker answers embedding_failed; elapsed_ms includes the wait.
Request
Set VOCAL_API to the base URL of your API and VOCAL_API_KEY to an API key with the knowledge:read scope (Authentication and keys). Replace each placeholder, such as agt_... or <name>, with your own value.
curl -X POST "$VOCAL_API/v1/knowledge-bases/kb_.../query" \
-H "Authorization: Bearer $VOCAL_API_KEY" \
-H "Idempotency-Key: $(uuidgen)" \
-H "Content-Type: application/json" \
-d '{ "query": "<query>" }'const response = await fetch(`${process.env.VOCAL_API}/v1/knowledge-bases/kb_.../query`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.VOCAL_API_KEY}`,
'Idempotency-Key': crypto.randomUUID(),
'Content-Type': 'application/json',
},
body: JSON.stringify({
"query": "<query>"
}),
});
console.log(response.status, await response.text());import os
import uuid
import requests
response = requests.post(
f"{os.environ['VOCAL_API']}/v1/knowledge-bases/kb_.../query",
headers={
"Authorization": f"Bearer {os.environ['VOCAL_API_KEY']}",
"Idempotency-Key": str(uuid.uuid4()),
},
json={
"query": "<query>",
},
)
print(response.status_code, response.text)Parameters
| Name | In | Type | Required | Description |
|---|---|---|---|---|
knowledge_base_id | path | KnowledgeBaseId | yes | The id of a Knowledge base: kb_ and the row's UUIDv7 in 26 Crockford base32 characters. Opaque and safe to log. |
include | query | array of KnowledgeContentClass | no | Content classes to add, comma-separated (text, names). Naming any makes the query an audited content read; without one the passages carry ids, ranks, scores and locators only. |
idempotency-key | header | string | no | A client-chosen key, kept 24 hours per Organization, environment and route. A replay returns the stored response. |
Request body
Content type application/json, required: KnowledgeQueryRequest.
| Field | Type | Required | Description |
|---|---|---|---|
filter | KnowledgeFilterInput | no | |
max_context_tokens | integer | no | Stops adding passages once their tokens would exceed it. |
min_score | number | no | Drops candidates whose semantic similarity is below it. |
query | string | yes | The question, 1 to 2,000 characters (a target). Content: never logged. From 1.54.0 the query route holds it only sealed in the query embedding task the worker reads (KnowledgeQueryTask), for the wait of the query (5 s, a target), and deletes it when the wait ends, whatever its outcome. |
rerank | boolean | no | Rerank through the rerank stage when a Provider resolves; skipped, and the answer says reranked: false, otherwise. |
top_k | integer | no | At most this many passages. |
version | KnowledgeVersionTarget | no | The active version when omitted. |
Responses
| Status | Content type | Shape | Error codes |
|---|---|---|---|
200 | application/json | KnowledgeQueryResult | |
400 | application/json | ErrorEnvelope | invalid_request |
401 | application/json | ErrorEnvelope | unauthenticated |
403 | application/json | ErrorEnvelope | forbidden |
404 | application/json | ErrorEnvelope | not_found |
409 | application/json | ErrorEnvelope | knowledge_base_version_retired, idempotency_in_progress, idempotency_key_reused |
422 | application/json | ErrorEnvelope | validation_failed |
429 | application/json | ErrorEnvelope | rate_limited |
500 | application/json | ErrorEnvelope | internal_error |
503 | application/json | ErrorEnvelope | dependency_unavailable |
Fields of the 200 answer
The shape is KnowledgeQueryResult.
| Field | Type | Required | Description |
|---|---|---|---|
elapsed_ms | integer | yes | The time the api took to answer, a measurement. |
knowledge_base_id | KnowledgeBaseId | yes | |
object | "knowledge_query_result" | yes | |
passages | array of KnowledgePassage | yes | Best first: at most top_k, none below min_score, and no more than fit max_context_tokens. An empty list is an answer, not a failure. |
reranked | boolean | yes | False when the fused order was kept, as when no rerank Provider resolves. |
top_score | number or null | yes | The highest score of the passages; null when there is none. |
version | integer or null | yes | The version searched; null for the working set. |