LlamaIndex Cheat Sheet
Ingest, index, and query your own data for LLM apps using LlamaIndex's data connectors, indices, retrievers, and query engines.
Load Documents and Build an Index
Read a directory of files and build a vector index in a few lines.
from llama_index.core import SimpleDirectoryReader, VectorStoreIndexdocuments = SimpleDirectoryReader("./docs").load_data()index = VectorStoreIndex.from_documents(documents)index.storage_context.persist(persist_dir="./storage")
Query the Index
Turn an index into a query engine and ask natural-language questions over your data.
query_engine = index.as_query_engine(similarity_top_k=5, response_mode="compact")response = query_engine.query("What were Q3 revenue drivers?")print(response)for node in response.source_nodes: print(node.score, node.node.metadata.get("file_name"))
Custom Node Parsing (Chunking)
Control how documents are split into nodes before embedding.
from llama_index.core.node_parser import SentenceSplittersplitter = SentenceSplitter(chunk_size=512, chunk_overlap=64)nodes = splitter.get_nodes_from_documents(documents)index = VectorStoreIndex(nodes)
Reload a Persisted Index
Restore a previously built index from disk without re-embedding everything.
from llama_index.core import StorageContext, load_index_from_storagestorage_context = StorageContext.from_defaults(persist_dir="./storage")index = load_index_from_storage(storage_context)query_engine = index.as_query_engine()
Index Types
Different index structures for different retrieval patterns.
- VectorStoreIndex- similarity search over embeddings, the most common choice
- SummaryIndex- linear scan, good for small corpora needing full-context summarization
- TreeIndex- hierarchical summarization tree for large documents
- KeywordTableIndex- keyword-based lookup, useful as a retrieval fallback
- KnowledgeGraphIndex- extracts and queries entity-relation triples
- as_chat_engine()- wraps a query engine with conversational memory
Decompose Queries Across Multiple Indexes
SubQuestionQueryEngine breaks a compound question into sub-questions routed to the right tool.
from llama_index.core.query_engine import SubQuestionQueryEnginefrom llama_index.core.tools import QueryEngineTool, ToolMetadatatools = [ QueryEngineTool( query_engine=sales_index.as_query_engine(), metadata=ToolMetadata(name="sales", description="Q3 sales data"), ), QueryEngineTool( query_engine=support_index.as_query_engine(), metadata=ToolMetadata(name="support", description="support ticket data"), ),]engine = SubQuestionQueryEngine.from_defaults(query_engine_tools=tools)response = engine.query("How did Q3 sales compare to support ticket volume?")
Rerank Results and Filter by Metadata
Overfetch with a wide top_k, then narrow by metadata filter and cross-encoder reranking.
from llama_index.core.postprocessor import SentenceTransformerRerankfrom llama_index.core.vector_stores import MetadataFilters, ExactMatchFilterreranker = SentenceTransformerRerank( model="cross-encoder/ms-marco-MiniLM-L-6-v2", top_n=3,)filters = MetadataFilters(filters=[ExactMatchFilter(key="department", value="finance")])query_engine = index.as_query_engine( similarity_top_k=20, filters=filters, node_postprocessors=[reranker],)response = query_engine.query("What was the Q3 budget variance?")
Build a Tool-Using FunctionAgent
Wrap a Python function as a tool and let a FunctionAgent decide when to call it.
from llama_index.core.agent.workflow import FunctionAgentfrom llama_index.core.tools import FunctionTooldef lookup_order(order_id: str) -> str: """Return the shipping status for an order id.""" return f"Order {order_id} shipped yesterday"agent = FunctionAgent( tools=[FunctionTool.from_defaults(fn=lookup_order)], llm=llm, system_prompt="You help customers track orders.",)response = await agent.run("Where is order 4471?")
Cached Ingestion Pipeline
Chain chunking and embedding transformations with a cache so unchanged documents skip re-embedding.
from llama_index.core.ingestion import IngestionPipeline, IngestionCachefrom llama_index.core.node_parser import SentenceSplitterfrom llama_index.embeddings.openai import OpenAIEmbeddingpipeline = IngestionPipeline( transformations=[ SentenceSplitter(chunk_size=512, chunk_overlap=64), OpenAIEmbedding(), ], cache=IngestionCache(), vector_store=vector_store,)nodes = pipeline.run(documents=documents, show_progress=True)pipeline.persist("./pipeline_storage")
Response Modes & Retrieval Fusion
Options that change how retrieved context is synthesized into an answer.
- response_mode="tree_summarize"- hierarchically combines chunk answers, best for broad summarize-everything queries
- response_mode="refine"- iteratively refines one running answer chunk-by-chunk; most accurate, slowest
- response_mode="compact"- default mode; packs as much context as fits per LLM call before synthesizing
- chat_mode="condense_plus_context"- rewrites follow-up questions using chat history before retrieving
- QueryFusionRetriever- reciprocal-rank-fuses results from multiple retrievers (e.g. vector + BM25)
- Settings.llm / Settings.embed_model- global defaults so you don't have to thread llm= through every call
Persist your storage_context after every index build — re-embedding a large corpus on every process restart is the single most common source of wasted API spend in LlamaIndex apps.