Run Query
Runs the query that Create Query Token issued a token for and streams the answer as Server-Sent Events. The credential is the query token, not the knowledge base API key. The token is checked when the request is accepted.
For the two-call flow and how to read the stream, see Query the Knowledge Base.
For which errors to retry and what the error details mean, see Handle Errors.
A successful query saves its query record before sending answer.done. Repeating the call runs the query again.
curl --request POST \
--url https://{api_base_url}/knowledge-fs/query-stream \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--header 'X-Trace-ID: <x-trace-id>' \
--data '
{
"activeDocumentIds": [],
"activeEntityIds": [],
"knowledgeSpaceId": "8d2c6a1e-5b3f-4c7d-9e01-2a3b4c5d6e7f",
"mode": "fast",
"query": "What changed in the refund policy this quarter?"
}
'Authorizations
The query token returned by Create Query Token, sent as Authorization: Bearer {token}. Use it before the returned expires_at time, at most 60 seconds after issue. It authorizes only Run Query on that knowledge base.
Send X-Trace-ID on the same request, with the trace_id that Create Query Token returned.
Headers
The trace_id from the Create Query Token response, the same value as the token's trace_id claim. A value that does not match the token is rejected with 403. See Query the Knowledge Base.
Body
Send the request object from Create Query Token unchanged. Unknown fields answer 400.
Optional fields marked nullable treat null as omitted.
The normalized request must fit within 65,536 UTF-8 bytes. Exceeding this budget answers 413. See Request Size Limits.
Provide a nonblank query.
Internal ID of the knowledge base, copied from the request object of Create Query Token. Must match the token. A mismatch is rejected with 403.
1The question. Use a nonblank query. Also limited to 16,384 UTF-8 bytes.
16000\SCopied from the returned request object. These IDs do not filter or rank retrieved content.
100Copied from the returned request object. These IDs do not filter or rank retrieved content.
1001 - 200Retrieval depth, copied from the request object of Create Query Token. null is accepted and means the knowledge base's default depth.
fast, deep, research, null Session ID, copied from the request object of Create Query Token.
Response
The stream opened. Events follow as text/event-stream, whatever Accept the request sends. Before the stream opens, handle failures as HTTP errors. After it opens, handle an answer.error event as failure even though the HTTP status is 200.
Each frame has an event: line naming the event and a data: line holding one JSON object. Dispatch on the event: line; the JSON carries no event field.
Set your client's read timeout to allow for your slowest retrieval mode. The API has no read timeout of its own and sends no keep-alive events. For work that must survive a client timeout, use Create Research Task.
See Query the Knowledge Base for the streaming flow.
Events
answer.delta: one piece of the answer.delta: the text to append.traceId: the query record ID.
answer.done: the answer is complete and its query record is saved.-
finishReason:retrieval-evidencewhen chunks were found,no-retrieval-evidencewhen nothing matched. -
metadata: an open map. It carriesmodeandevidenceBundleatfast,deep, andresearchdepth,citationswithretrieval-evidence, and other keys the run produced.evidenceBundle.itemsholds the retrieved chunks with their text and scores.To inspect them later, call List Query Chunks. It returns them page by page and marks chunks whose document has since been deleted.
-
traceId: the query record ID.
-
answer.error: the query failed after the stream opened.code: the failure, one of:RETRIEVAL_EXECUTION_LEASE_LOST: the knowledge base entered deletion, or the run expired. Run the query again.KNOWLEDGE_FS_UNAVAILABLE: any other failure while generating the answer or saving the query record.
error: the message.failure: error details:category,code,message,retryPolicy,stage,traceId, and optionallyaction.stage: alwaysquery_generation.
traceId: the query record ID.
citations
One entry per retrieved chunk that supports the answer:
nodeId: the chunk ID.label:node:<nodeId>.documentAssetId: the document file the chunk came from.sectionPath: the outline path.pageNumber: page number, starting at 1, when the source provides page information. Omitted otherwise.projectionIds: IDs of the search indexes that contributed to this match. UsedocumentAssetIdto identify the source file.sources: which searches found the chunk:dense,fts,pageindex, orvisual.
An image citation can include multimodalCandidate.assetRef.analysisUnavailable.reason. When present, image analysis was skipped because of a resource limit or an image-processing failure. Related text can still contribute to the result.
evidenceBundle.items[].citations[] may also carry startOffset and endOffset. These are zero-based UTF-8 byte offsets into the parsed document text, with an inclusive start and exclusive end. Use the returned chunk text to display a citation; do not slice the original file at these offsets.
Closing sequence by outcome
- Success: zero or more
answer.delta, then oneanswer.done, then the stream closes. - Failure:
answer.errorin place ofanswer.done, then the stream closes. Pieces already delivered may precede it, and the HTTP status stays200. Treat it as final and start a new query to try again. - Client disconnect. The run is canceled and the stream ends.
Answer content: at fast and deep, one answer.delta carries a digest of the retrieved chunks (Retrieval evidence answer: followed by numbered section paths and their text). At research, the synthesized answer streams as one or more pieces. When nothing matched, the single piece is I could not find evidence for that query in the indexed retrieval projections. and finishReason is no-retrieval-evidence.
Answers are capped at 2,000 characters at every depth. Reaching that length does not tell you whether the answer was truncated.
A successful run:
event: answer.delta
data: {"delta":"Retrieval evidence answer:\n1. Policies / Refunds: Refund window extended from 14 to 30 days for annual plans.","traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"}
event: answer.done
data: {"finishReason":"retrieval-evidence","metadata":{"mode":"fast","generator":"hybrid-query","topScore":0.83,"citations":[{"documentAssetId":"b3f4c8d2-6a1e-4f7b-9c0d-2e3f4a5b6c7d","label":"node:9f2e6b41-7c3d-4a58-b0e9-1d2c3f4a5b6e","nodeId":"9f2e6b41-7c3d-4a58-b0e9-1d2c3f4a5b6e","pageNumber":4,"projectionIds":["c7a0d5b2-3e14-4f89-a6b7-0c1d2e3f4a5b"],"sectionPath":["Policies","Refunds"],"sources":["dense","fts"]}],"evidenceBundle":{"createdAt":"2026-09-06T08:31:12Z","id":"e4b2f7a0-1c3d-4e58-9f6a-0b1c2d3e4f5a","items":[{"citations":[{"documentAssetId":"b3f4c8d2-6a1e-4f7b-9c0d-2e3f4a5b6c7d","documentVersion":1,"pageNumber":4,"sectionPath":["Policies","Refunds"]}],"conflicts":[],"freshness":{"status":"fresh"},"metadata":{},"nodeId":"9f2e6b41-7c3d-4a58-b0e9-1d2c3f4a5b6e","score":0.83,"scores":{"final":0.83,"retrieval":0.71},"text":"Refund window extended from 14 to 30 days for annual plans."}],"missingEvidence":[],"query":"What changed in the refund policy this quarter?","state":"answerable","traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"}},"traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"}
Failure after the stream opened:
event: answer.error
data: {"code":"KNOWLEDGE_FS_UNAVAILABLE","error":"Agent Knowledge Base is temporarily unavailable. Try again later.","failure":{"action":"retry","category":"dependency","code":"KNOWLEDGE_FS_UNAVAILABLE","message":"Agent Knowledge Base is temporarily unavailable. Try again later.","retryPolicy":"manual","stage":"query_generation","traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"},"traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"}
Was this page helpful?
curl --request POST \
--url https://{api_base_url}/knowledge-fs/query-stream \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--header 'X-Trace-ID: <x-trace-id>' \
--data '
{
"activeDocumentIds": [],
"activeEntityIds": [],
"knowledgeSpaceId": "8d2c6a1e-5b3f-4c7d-9e01-2a3b4c5d6e7f",
"mode": "fast",
"query": "What changed in the refund policy this quarter?"
}
'