Retrieval Contract (Backend <-> Agent Engine)¶
This note is the canonical source of truth for retrieval semantics shared by backend and agent engine.
It covers:
- the execution-scoped retrieval request contract
- normalized retrieval result fields
- scoring and fusion semantics
- ownership boundaries across backend, agent engine, and provider adapters
It does not cover frontend display rules or KB-admin UI behavior.
Canonical Request¶
Retrieval is optional and execution-scoped.
Canonical request fields:
index: active vector index or KB-backed provider index namequery: retrieval query texttop_k: requested result countfilters: provider-agnostic exact-match filter bagsearch_method:semantic | keyword | hybridquery_preprocessing:none | normalizehybrid_alpha: optional hybrid semantic weight
Defaults:
top_k = 5search_method = semanticquery_preprocessing = nonehybrid_alpha = 0.5only whensearch_method = hybrid
Canonical example:
{
"retrieval": {
"index": "kb_product_docs",
"query": "How does retrieval work?",
"top_k": 5,
"filters": {},
"search_method": "hybrid",
"query_preprocessing": "normalize",
"hybrid_alpha": 0.5
}
}
Canonical Result Shape¶
Agent engine emits normalized retrieval call metadata plus per-result canonical relevance fields.
Canonical retrieval call metadata:
indexquerytop_ksearch_methodquery_preprocessinghybrid_alpha?result_count
Canonical retrieval result item:
idtextmetadatarelevance_scorerelevance_kindrelevance_components?scorescore_kind
relevance_kind is one of:
similaritykeyword_scorehybrid_score
relevance_components, when present, contains:
semantic_score?keyword_score?
Canonical result example:
{
"retrieval_calls": [
{
"index": "kb_product_docs",
"query": "how does retrieval work",
"top_k": 5,
"search_method": "hybrid",
"query_preprocessing": "normalize",
"hybrid_alpha": 0.5,
"result_count": 1,
"results": [
{
"id": "doc-1",
"text": "Hybrid retrieval blends semantic recall with lexical precision.",
"metadata": {
"title": "Architecture Overview",
"uri": "https://example.com/architecture"
},
"score": 0.94,
"score_kind": "hybrid_score",
"relevance_score": 0.94,
"relevance_kind": "hybrid_score",
"relevance_components": {
"semantic_score": 0.91,
"keyword_score": 0.97
}
}
]
}
]
}
Built-In Chunk Metadata¶
Knowledge-base ingestion stores these built-in metadata fields on every indexed chunk:
knowledge_base_iddocument_idchunk_indextitlesource_typesource_nameurisource_idsource_pathmanaged_by_source
PDF ingestion also stores page_number on each chunk when the original page is known. page_number is chunk-level metadata and is safe to use for source references and retrieval filters. It is not inferred from page_count; page_count remains document-level metadata when the parser can determine it.
Vector-store schemas must reserve these built-in fields, including page_number, so custom schema properties should not duplicate them.
Knowledge-chat reference projection aggregates pages from page_number, pages, or page_start / page_end. For source-managed local-directory documents, backend projection may include file_url, an authenticated /v1/playgrounds/.../source-file route. The frontend must use that backend URL instead of exposing local file:// paths.
Scoring Semantics¶
semantic- uses the active
embeddingsbinding plus the activevector_storebinding - canonical
relevance_scoreis normalized similarity -
canonical
relevance_kindissimilarity -
keyword - uses the active
vector_storebinding without embeddings - canonical
relevance_scoreis the provider keyword score used for ranking -
canonical
relevance_kindiskeyword_score -
hybrid - runs semantic and keyword branches in backend/agent-engine retrieval logic, not in the provider contract
- canonical
relevance_scoreis the fused hybrid score - canonical
relevance_kindishybrid_score relevance_components.semantic_scoreandrelevance_components.keyword_scoreare the normalized branch scores actually used in fusion
Hybrid fusion rules:
- preprocess the query once using
query_preprocessing - over-fetch both branches with
min(max(top_k * 3, 10), 50) - normalize semantic branch as similarity
- normalize keyword branch per result set with min-max scaling
- if all keyword scores are equal and keyword results exist, assign
1.0to all keyword matches - fuse with
hybrid_score = alpha * semantic + (1 - alpha) * keyword
Ownership Boundaries¶
- Backend owns:
- product/public retrieval request shaping
- active KB resolution and deployment-runtime selection
- forwarding canonical retrieval requests to agent engine
-
projecting retrieval results into knowledge-chat and playground-facing source payloads
-
Agent engine owns:
- execution-time retrieval normalization
- semantic / keyword / hybrid branch execution
- hybrid fusion
-
canonical
retrieval_calls[*]result emission -
Provider adapters own:
- translating canonical retrieval requests into provider-native query calls
- returning provider-native
score/score_kind - they do not define canonical hybrid behavior
Provider Boundary and Non-Goals¶
- Provider-native hybrid search is not part of the canonical contract.
- Frontend score labels, snippets, numbering, and source-card presentation are not part of this note.
- Backend KB retrieval QA pages and knowledge chat may project retrieval results differently for UX, but they should preserve canonical retrieval semantics from this contract.