Skip to main content
LlamaIndex.TS processes data through a series of transformations, from raw documents to indexed embeddings to final query responses. Understanding this flow is crucial for building effective LLM applications.

Overview

The data flow consists of two main pipelines:

Ingestion Pipeline

Document → Nodes → Embeddings → Vector Store

Query Pipeline

Query → Embedding → Retrieval → Synthesis → Response

Ingestion Pipeline

The ingestion pipeline transforms raw documents into searchable vector embeddings.

Step 1: Document Loading

Load documents
Document Structure:
From @llamaindex/core/schema/node.ts

Step 2: Node Parsing (Chunking)

Documents are split into smaller chunks called Nodes:
Node parsing
Why chunk documents?
LLMs have maximum context windows (e.g., 4K, 8K, 128K tokens). Chunking ensures content fits within these limits.
Smaller chunks often represent more coherent semantic units, improving retrieval accuracy.
Fine-grained chunks allow more precise retrieval of relevant information.
Node Structure:
BaseNode from @llamaindex/core/schema

Step 3: Embedding Generation

Each node is converted to a vector embedding:
Generate embeddings
How it works:
From packages/llamaindex/src/indices/vectorStore/index.ts
Embeddings are vector representations of text that capture semantic meaning. Similar texts have similar embeddings.

Step 4: Vector Store Insertion

Nodes with embeddings are stored in a vector database:
Store in vector database
What gets stored:
  • Node ID
  • Embedding vector
  • Node content (if storesText: true)
  • Metadata for filtering

Complete Ingestion Example

Full ingestion pipeline

Advanced: IngestionPipeline

For more control, use the IngestionPipeline:
Custom ingestion pipeline
Pipeline features:

Query/Retrieval Pipeline

The query pipeline retrieves relevant nodes and synthesizes responses.

Step 1: Query Embedding

Query embedding
Always use the same embedding model for indexing and querying to ensure compatibility.
Retrieve similar nodes
From VectorIndexRetriever:
packages/llamaindex/src/indices/vectorStore/index.ts

Step 3: Response Synthesis

Retrieved nodes are sent to the LLM to generate a response:
Query engine
What happens:
  1. Query is embedded
  2. Top-K similar nodes are retrieved
  3. Nodes are formatted into a context
  4. LLM generates response using context + query

Step 4: Post-Processing (Optional)

Apply reranking or filtering:
Reranking

Complete Query Example

Full query pipeline

Chat Engine Flow

Chat engines maintain conversation history:
Chat engine
Chat flow:

Streaming Responses

Stream responses for better UX:
Streaming

Data Flow Optimization

Chunk Size

Smaller chunks = more precise retrieval but more chunks to searchLarger chunks = more context but less precision

Top-K

Higher K = more context but slower and more expensiveLower K = faster but might miss relevant info

Embedding Model

Better embeddings = better retrieval accuracyLarger dimensions = more accurate but slower

Reranking

Post-retrieval reranking improves precisionUse similarity cutoff to filter low-quality matches

Best Practices

1

Consistent Embeddings

Use the same embedding model for both indexing and querying
2

Optimal Chunk Size

Test different chunk sizes (512, 1024, 2048) for your use case
3

Metadata Filtering

Use metadata to filter retrieval results (e.g., by date, source, category)
4

Monitor Performance

Track retrieval quality with evaluation metrics
5

Cache Embeddings

Use IngestionPipeline caching to avoid re-embedding unchanged documents

Next Steps

Vector Indices

Deep dive into VectorStoreIndex configuration and usage

Query Engines

Learn about different query engine types and customization

Ingestion Pipeline

Advanced ingestion pipeline patterns and transformations