AI / LlamaIndex Interview Questions
How do you persist and reload an index in LlamaIndex?
Building an index from scratch every time an application starts is wasteful, since it means re-embedding every document. LlamaIndex avoids that with StorageContext, which can save the docstore, index store, and vector store to disk and load them back later.
# Persist index.storage_context.persist(persist_dir="./storage") # Reload later from llama_index.core import StorageContext, load_index_from_storage storage_context = StorageContext.from_defaults(persist_dir="./storage") index = load_index_from_storage(storage_context)
On reload, no re-embedding happens; the previously computed vectors and Node data are read straight back from disk, so the index is ready to query immediately. If you're using an external vector store like Pinecone or Chroma instead of the default in-memory store, the vectors already live there persistently, so you mainly need to reconnect using the same StorageContext configuration rather than persisting to a local folder.
More Related questions...