Lesson 5 of 7 Retrieval Augmented Generation

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.

Put in order

A custom ingestion pipeline

Exam skills 3.2.8

Arrange the stages of a custom ingestion pipeline for RAG.

Log in to try this activity Interactive activities are free with an account.

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.

Enforcing matter assignments at query time. During ingestion, each chunk is stored with an allowed_groups field from the document management system. For every query, the service reads the lawyer's current groups from the identity system and adds a terms filter on allowed_groups, so unauthorised chunks are never retrieved and group changes take effect immediately.
The scenario is illustrative.

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 knowledge base
Standard documents, fast deliveryPreferred
Proprietary formatsCustom transformation Lambda may suffice
Per-document user permissionsMetadata filters passed by the application
Existing OpenSearch search platform with custom scoringLimited
Least operational overheadPreferred
Custom pipeline
Standard documents, fast deliveryUnnecessary
Proprietary formatsFull control
Per-document user permissionsNative ACL filtering in the search query
Existing OpenSearch search platform with custom scoringPreferred
Least operational overheadMore to maintain
Sort into groups

Managed or custom?

Exam skills 3.2.8 · 2.1.5

Decide whether each requirement points to Bedrock Knowledge Bases or a custom pipeline.

Log in to try this activity Interactive activities are free with an account.
Worked example

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_groups field from the document management system.
  • At query time, the service reads the lawyer's current groups and adds a terms filter on allowed_groups to 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.

Check yourself

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.