Skip to main content
Build retrieval in two observable parts: find relevant source material, then ask the model to answer from it. Inspecting only the final prose hides whether a failure came from ingestion, search, or generation. For a complete project with installation and a run command, follow the knowledge tutorial. These independent host functions require matching core, a configured text model, and an embedding provider. Remote embedding providers make network requests even when the vector store is in memory.

Retrieve first, then answer

This function owns an in-memory knowledge base and an Agent. It loads two fictional store policies, retrieves evidence, and returns both the answer and source IDs. The supplied model and embedder must already be configured by your application.
Ask “How long do I have to return an unopened item?” Expect the retrieved IDs to include returns and the answer to cite that ID with the 30-day policy. The prompt requests grounding; verify it with application evaluations rather than treating a citation as proof. Change one policy and repeat the question. Also ask about a fact absent from both documents: the answer should acknowledge missing evidence. This small fixture rebuilds its index for each invocation; a service should ingest separately and reuse its owned index. Host factory: pass an initialized knowledge base populated with documents visible to this caller. This function borrows it. The host closes the Agent after its runs and the knowledge base after all consumers finish.
Observe search_policy in the run’s tool events and inspect its result. Giving the model a search tool does not guarantee it will call it. Choose retrieval-before-generation when your application requires evidence to be present on every request.

Choose the storage and retrieval path

Metadata filters are part of retrieval configuration, not a substitute for authorizing document ingestion and access. Keep source IDs stable and retain the source revision used by an answer.

More patterns

For ingestion, chunking, vector adapters, and hybrid-search configuration, use the knowledge guides. For a source-oriented harness, run the research project. Add evaluations for retrieval recall, unsupported questions, citation accuracy, and tenant-specific evidence.