Building Custom Retrieval Pipelines
This lesson explains when requirements such as unusual formats, per-user permissions, specialised ranking or existing search infrastructure call for a custom retrieval pipeline instead of a managed knowledge base.
Learning outcomes
After completing this lesson, you will be able to:
- Design the ingestion side of a custom pipeline.
- Design the query side with authorisation and filtering.
- Decide between managed and custom retrieval.
Ingestion side
A custom ingestion pipeline gives full control over how documents become searchable. It extracts content, using Amazon Textract for scanned documents and custom parsers for proprietary formats; cleans the text and splits it into chunks; and generates embeddings with Amazon Bedrock, on demand or with batch inference for large backfills, or with a SageMaker AI endpoint hosting a custom embedding model.
It then writes the vectors, text and metadata, including access control lists that determine who may see each chunk, to Amazon OpenSearch Service, pgvector or Amazon S3 Vectors. AWS Step Functions orchestrates the steps with retries, and S3 events or Amazon EventBridge trigger the pipeline when documents change.
A custom ingestion pipeline
Query side
The query path, often a Lambda function or container behind API Gateway, authenticates the user, embeds the question, runs a vector or hybrid query with permission filters derived from the user's groups, calls the Rerank API, builds the prompt with citations and calls the model through the Converse API, optionally with a guardrail. Bedrock Flows can chain prompts, knowledge bases, Lambda functions and conditions visually as an alternative to custom code.
The following diagram shows where a permission field is added to each document chunk and where it is enforced, so that lawyers retrieve only documents from their own matters.

Managed or custom
Amazon Bedrock Knowledge Bases handle ingestion, chunking, embedding, storage and retrieval with little code, and they should be the default. A custom pipeline is justified when a requirement goes beyond what the managed service offers, for example document formats it cannot parse, access control at the level of individual chunks, a vector store or embedding model it does not support, or retrieval logic such as multi-step reranking. The cost is building and operating every stage. The following table summarises when a custom pipeline is justified.
Managed or custom?
Document-level security
A law firm's assistant must never show a lawyer content from matters they are not assigned to. Matter assignments change daily in an identity system.
- During ingestion, each chunk is stored in OpenSearch with an
allowed_groupsfield from the document management system. - At query time, the service reads the lawyer's current groups and adds a terms filter on
allowed_groupsto every hybrid query. - Group changes take effect immediately, because filtering happens at query time rather than in the index.
Relying on the model to refuse confidential content would be unsafe, because retrieved text is already in the prompt. Filtering before retrieval guarantees the model never sees unauthorised content.
Where must document permissions be enforced in a custom retrieval pipeline?
Show answer
In filters in the retrieval query, never in the prompt.
Exam Tips
Remember for the exam
- Custom pipelines for proprietary formats, ACLs or existing search platforms.
- Enforce document permissions with filters in the retrieval query, not in the prompt.
- Use the same embedding model for ingestion and queries.
- Bedrock Flows chains prompts, knowledge bases and Lambda without custom orchestration code.
- Prefer managed knowledge bases when they meet the requirements.