- Context is the most important building block of an LLM application. Context means external information (documents, databases, APIs, or the web) retrieved and added to the prompt so the model has knowledge-rich background to generate factually correct responses.
- When you ground an LLM’s output in retrieved evidence, you reduce hallucinations and make answers explainable and auditable.
- Relational databases (SQL queries)
- Full-text search indexes (e.g., Elasticsearch)
- Semantic search over vector embeddings stored in a vector database
- Files and documents (PDF, Word, HTML), object storage (S3), or any external API
- Real-time web search for time-sensitive queries


- Keeps answers accurate and grounded
- Reduces token usage and cost
- Limits exposure of private data to external models

- Context window limits: Every model has a finite context window—the combined size of input (prompt + context) and output. Sending a full 100-page PDF is usually impossible or inefficient.
- Relevance and privacy: Sending only the relevant subset minimizes exposure of sensitive data and makes it easier to trace the source of a fact.

- User submits a question or prompt to a chatbot or application.
- The system encodes the prompt and queries a search layer (semantic or full-text) over your corpus.
- Relevant context chunks are retrieved.
- The retrieved context is injected into the prompt and sent to the LLM.
- The LLM returns a grounded answer that can include citations, which is delivered to the user.

- Load unstructured data (HTML, PDF, DOCX, JSON, images, etc.).
- Split the data into chunks (sentences, paragraphs, or fixed-size windows of tokens). Smaller chunks improve granularity; larger chunks preserve more local context.
- Convert each chunk into an embedding vector using an embeddings model (which may be distinct from your generative LLM).
- Store the vectors and metadata (source, document id, chunk id, offsets) in a vector database (e.g., Chroma, Milvus, Weaviate, Qdrant).

- Encode the user query with the same embeddings model used for indexing.
- Perform a similarity search across your vector database to find the closest vectors (semantic matches).
- Retrieve the corresponding text chunks and metadata, then assemble the context.
- Inject the retrieved context into the prompt and send it to the LLM.
- The LLM uses the question + retrieved evidence to generate a grounded answer.


- Use the same embeddings model for indexing and querying.
- Tune chunk size to balance context preservation vs retrieval granularity.
- Store rich metadata (source URL, document id, chunk offsets) so answers can include citations.
- For highly sensitive data, use access controls or on-premise vector stores to minimize exposure.
- LangChain: an ecosystem of components for building RAG pipelines and LLM apps.
- Vector DBs: Chroma, Milvus, Weaviate, Qdrant
RAG combines semantic search with generation: the search finds evidence and the LLM composes the answer. For reliable semantic matching, always use the same embeddings model for both indexing and query encoding.