templates/template-chat-with-pdf/README.md
An AI-powered PDF assistant that lets you chat with any PDF document. Ask questions about the content, get answers with page citations, and optionally generate quizzes to test your comprehension. Uses RAG (Retrieval-Augmented Generation) to find relevant passages and provide accurate, sourced answers. Built with Mastra.
Putting an entire PDF in a context window hits token limits, wastes money on irrelevant tokens, and causes context drift—where the model loses focus as it wades through pages of text that don't matter. RAG solves this: the PDF gets chunked into vector embeddings, and when you ask a question, only the most relevant chunks are included in the prompt. This keeps your costs down, avoids context limits, and gives the model focused context for better answers.
This template shows how Mastra's RAG capabilities, vector storage, and agent workflows fit together—and gives you a starting point to build your own document chat experience.
<video controls width="640" height="360" src="https://res.cloudinary.com/mastra-assets/video/upload/v1772538110/template-chat-with-pdf_uamb2s.mp4"></video>
This demo runs in Mastra Studio, but you can connect this agent to your React, Next.js, or Vue app using the Mastra Client SDK or agentic UI libraries like AI SDK UI, CopilotKit, or Assistant UI.
npx create-mastra@latest --template chat-with-pdf to scaffold the project locally..env.example to .env and fill in your OpenAI API key.npm run dev and open localhost:4111 to try it out.Open Studio and start chatting with the PDF agent. Provide a PDF URL to index it, then ask questions about the content or request a quiz on specific pages or topics. The agent searches the indexed content, answers questions with page citations, and can generate comprehension quizzes from actual passages.
Need a PDF to try? Grab our free book, Principles of Building AI Agents.
Swap in a different embedding model, adjust the chunking strategy, or wire the agent into your app using the Mastra Client SDK. You can also swap out LibSQL for another vector database — Mastra supports many options including Pinecone, Qdrant, Chroma, pgvector, and more. The agent, tools, and workflow are all in src/ — edit them directly to fit your use case.
Mastra templates are ready-to-use projects that show off what you can build — clone one, poke around, and make it yours. They live in the Mastra monorepo and are automatically synced to standalone repositories for easier cloning.
Want to contribute? See CONTRIBUTING.md.