Chat with PDF using LlamaIndex and LlamaParse
Parse PDFs with LlamaParse, index them with LlamaIndex and ask questions with OpenAI, with page citations and a one-click Railway template.
Large language models don't know what's in your documents. Retrieval-augmented generation (RAG) fixes that at question time: the app finds the passages that relate to your question and sends them to the model along with it. For PDFs, the quality of the answer depends heavily on a step that's easy to overlook, which is getting clean text out of the file in the first place.
This post builds a "chat with PDF" app with three pieces: LlamaParse to parse the PDF, LlamaIndex to index it, and OpenAI to embed the text and write the answers. It's the chat-with-pdf app in my llama-index repo.
Why Parse PDFs with LlamaParse
Libraries like pypdf read a PDF's text layer, which works well for plain, single-column documents. Tables, multi-column layouts and charts often come out as jumbled lines, and scanned pages have no text layer at all. LlamaParse is a hosted parser that converts each page to markdown, keeping tables as tables, and its higher tiers handle scans and complex layouts.
You choose a tier per document, and each costs a different number of credits per page:
| Tier | Credits per page | Suits |
|---|---|---|
| Fast | 1 | Plain, single-column text |
| Cost Effective | 3 | Mostly text with simple tables (the app's default) |
| Agentic | 10 | Scanned pages, multi-column layouts, real tables, charts |
| Agentic Plus | Highest | Dense financial reports and complex tables |
A free LlamaCloud account includes 10,000 credits a month, enough for about 3,300 pages at the Cost Effective tier. Re-parsing the same file within 48 hours is free, because LlamaParse caches results.
Parse a PDF into Pages
The app sends the PDF to LlamaParse through the llama-cloud SDK and asks for markdown. It turns each successfully parsed page into a LlamaIndex Document with its page number as metadata, which helps with page citations later:
def parse_pdf(data: bytes, name: str, api_key: str, tier: str) -> list[Document]:
"""Parse a PDF with LlamaParse and return one LlamaIndex Document per page."""
with LlamaCloud(api_key=api_key) as client:
result = client.parsing.parse(
tier=tier,
version="latest",
upload_file=(name, data, "application/pdf"),
page_ranges={"max_pages": MAX_PAGES},
expand=["markdown"],
timeout=PARSE_TIMEOUT,
)
pages = result.markdown.pages if result.markdown else []
return [
Document(text=page.markdown, metadata={"page": page.page_number})
for page in pages
if page.success and page.markdown.strip()
]The app parses the first 50 pages by default and gives up after five minutes, so a large or slow document fails with a clear message instead of hanging.
Index and Query with LlamaIndex
LlamaIndex embeds the pages with OpenAI's text-embedding-3-small model and keeps them in an in-memory vector index. Each question retrieves the five most similar chunks, and the tree_summarize response mode combines them into one answer:
st.session_state.index = VectorStoreIndex.from_documents(
st.session_state.documents,
embed_model=OpenAIEmbedding(model=EMBED_MODEL, api_key=openai_api_key),
)
query_engine = st.session_state.index.as_query_engine(
llm=OpenAI(model=model, api_key=openai_api_key),
similarity_top_k=TOP_K,
response_mode="tree_summarize",
)
response = query_engine.query(query)The parsed pages and the index are kept in the Streamlit session, so follow-up questions about the same PDF don't parse or embed it again. Nothing is written to disk, and the index disappears when the session ends.
Deploy the App on Railway
My LlamaIndex Apps template on Railway deploys this app alongside a URL summarizer. There are no variables to set: you enter your OpenAI and LlamaCloud API keys in the app's sidebar, and they stay in your browser session.
Chat with a PDF
Add your OpenAI and LlamaCloud API keys in the sidebar, pick a LlamaParse tier, upload a PDF of up to 20 MB and ask a question. The answer shows the pages it drew on, and the Retrieved passages panel shows the text behind it, so you can check the answer against the document. I uploaded Alphabet's Q1 2024 earnings release and asked about quarterly revenue.

The default model is gpt-6-luna, and you can change it in the sidebar or with the OPENAI_MODEL variable. The model field only accepts names that LlamaIndex's OpenAI integration recognises, so a newly released model may need a library update before it works.
Frequently Asked Questions
Is LlamaParse free? A free LlamaCloud account includes 10,000 credits a month. At 3 credits per page on the Cost Effective tier, that's about 3,300 pages.
Can I chat with a scanned PDF? Yes, with LlamaParse's Agentic or Agentic Plus tier, which handle scanned pages. The Fast tier only reads text that's already in the file.
Why doesn't the answer cover the end of my document? The app parses the first 50 pages by default. Set MAX_PAGES to a higher number if you deploy it yourself.