Query a knowledge base
Retrieve from a knowledge base — the way to test what an agent will see.
Retrieve from a knowledge base — the way to test what an agent will see.
An agent using this knowledge base runs the same retrieval on every customer turn and prepends the matched text to its prompt, so what comes back here is what the agent gets. If this returns nothing, the agent will answer from its prompt alone and the call will sound completely normal — silent misses are exactly what this endpoint exists to make visible.
total_candidates counts what was retrieved BEFORE reranking, so with
rerank: true it is larger than returned. score is
1/(1+distance), not a cosine similarity: it is useful for ordering, not
as an absolute confidence.
The knowledge base is the one in the path, resolved against your organisation: this endpoint accepts no collection list, so it cannot be pointed at another organisation's knowledge base.
Rate limit: 1000 requests per minute per organization (bucket default). Exceeding it returns 429 with Retry-After; the X-RateLimit-* response headers report your remaining allowance on every call.
v2/knowledge-bases/{collection}/queryAuthorization
AuthorizationRequiredBearer <token>
Your Graine API key. Create one in the dashboard under Developers, or via POST /v2/api-keys. Send it as Authorization: Bearer <key>.
In: header
Request Body
application/jsonRequiredqueryRequiredQuery
The text to retrieve against.
1Maximum length: 4000max_resultsMax Results
Chunks to retrieve before any reranking.
10Minimum: 1Maximum: 50similarity_thresholdSimilarity Threshold
Minimum score to keep. Scores are 1/(1+distance), so 0.0 (the default) keeps everything and values above ~0.5 are aggressive.
0Minimum: 0Maximum: 1rerankRerank
Re-order candidates with a cross-encoder. Slower, more precise.
falsererank_candidatesRerank Candidates
Candidates fed to the reranker when rerank is true.
20Minimum: 1Maximum: 100rerank_finalRerank Final
Chunks kept after reranking when rerank is true.
5Minimum: 1Maximum: 50Path Parameters
collectionRequiredCollection
The knowledge base name.
Query Parameters
organization_idOrganization Id
Must match the organisation the API key belongs to.
Response Body
200
The matched chunks, most relevant first.
collectionRequiredCollection
The knowledge base that was queried.
documentsRequiredDocuments
Matched chunks, most relevant first.
returnedRequiredReturned
Number of chunks in 'documents'.
total_candidatesRequiredTotal Candidates
Candidates retrieved BEFORE reranking. With rerank enabled this is larger than 'returned'; it is not a corpus-wide match count.
query_time_msQuery Time Ms | null
Upstream retrieval time.
400
1000 — malformed input (bad knowledge base name, unreadable or empty file).
errorRequiredError
Stable integer code from the /v2 error table. Branch on this.
messageRequiredMessage
One human-readable sentence. Wording may change; the code will not.
401
1100 — missing, unknown or inactive API key. 1101 — a browser session token was presented.
errorRequiredError
Stable integer code from the /v2 error table. Branch on this.
messageRequiredMessage
One human-readable sentence. Wording may change; the code will not.
403
1102 — the key is valid but lacks the scope this endpoint requires, or names another organisation. GET /v2/scopes reports which scopes the key holds.
errorRequiredError
Stable integer code from the /v2 error table. Branch on this.
messageRequiredMessage
One human-readable sentence. Wording may change; the code will not.
404
1200 — no such knowledge base in this organisation. Cross-tenant reads answer 404, never 403.
errorRequiredError
Stable integer code from the /v2 error table. Branch on this.
messageRequiredMessage
One human-readable sentence. Wording may change; the code will not.
422
1001 — the body or query failed validation; the message names the first offending field.
errorRequiredinteger
Stable integer code from the error table. Branch on this, not on the message.
messageRequiredstring
One human-readable sentence. Wording may change; the code will not.
429
1300 — per-organisation rate limit exceeded. Carries Retry-After.
errorRequiredError
Stable integer code from the /v2 error table. Branch on this.
messageRequiredMessage
One human-readable sentence. Wording may change; the code will not.
503
1501 — the knowledge service is unreachable or returned a 5xx.
errorRequiredError
Stable integer code from the /v2 error table. Branch on this.
messageRequiredMessage
One human-readable sentence. Wording may change; the code will not.
504
1502 — the knowledge service did not respond in time. A slow ingest may still complete.
errorRequiredError
Stable integer code from the /v2 error table. Branch on this.
messageRequiredMessage
One human-readable sentence. Wording may change; the code will not.
The matched chunks, most relevant first.

