Project P05
Tool-using agent with RAG on local models
How can we build an agent that searches, reasons, and acts with tools using only models that fit on an 8 GB GPU, and how can we demonstrate that it works and is secure?
- stages
- 9
- duration
- 16 h
- concepts
- 26
- available in English
- 9 / 9
- completed by you
0 of 9
Where it fits
Overview
An entirely local LLM systems engineering project. Serve a quantized LLM with structured output, build a research corpus from the curriculum's papers and your notes with a verified evaluation set, and implement BM25, dense retrieval, an HNSW index, hybrid fusion, and reranking from scratch. Build an agent on top with tool calls, isolated code execution, and context management, then evaluate it on a custom benchmark of verifiable tasks. The project concludes with retriever fine-tuning and a red-teaming exercise involving indirect prompt injection.
Why it matters
This is both a personal research assistant and the architecture behind many real products. Building it with local models requires understanding each component, including quantization, context, retrieval, and the tool protocol. Rigorous evaluation and protection against prompt injection connect directly to application security.
Stages 9
- 01Serving a local LLM and enforcing structured outputChoose an open-license instruct model with 3 to 8 billion parameters, serve it quantized in GGUF through a local chat API, and measure prefill and decoding latency as context changes. Calculate the VRAM budget and implement grammar-constrained structured output, comparing valid JSON rates with and without constraints.
- 02Corpus, chunking and evaluation setBefore building the retriever, build a way to measure it. Ingest a research corpus of papers cited in the syllabus and your notes, chunk it using several strategies, and use the local LLM to generate questions with reference passages for manual verification.
- 03Lexical, dense and hybrid retrievalImplement BM25 from scratch with an inverted index, encode the corpus using a local GPU embedding model, construct an HNSW index and compare it with exact search, and combine both approaches with reciprocal rank fusion. Measure all variants with recall@k and nDCG@10 on the development set.
- 04Reranking, cited generation and faithfulnessAdd a local cross-encoder to rerank candidates, design a generation prompt requiring citations and abstention, and evaluate answer correctness against the reference and faithfulness to context with an LLM judge calibrated against manual annotations.
- 05Tools, calling protocol and sandboxed executionDesign the agent's tools for corpus search, reading a chunk, arXiv search, and Python execution with JSON schemas, argument validation, and informative errors. Isolate code execution with time, memory, working-directory, and network limits and test it with hostile code.
- 06The agent loop, context and tracingWrite an agent loop without frameworks: reasoning, calls, observations, stopping conditions, and step, token, and time budgets. Add context management with summaries of old observations and note memory, plus structured traces for replaying and debugging every run.
- 07Custom benchmark and agent evaluationBuild a benchmark of 60 to 100 automatically verifiable numerical, citation, or code tasks over the corpus, run several configurations with repetitions, and compare them using paired intervals and tests. Classify failed traces in an error taxonomy to guide improvements.
- 08Retriever fine-tuning and multi-hop queriesUse the error taxonomy to improve retrieval: fine-tune the embedding model on GPU with contrastive loss and hard negatives mined from the index, and add hypothetical-document query rewriting and iterative retrieval for multi-hop questions. Validate each improvement on the frozen test set with paired tests.
- 09Agent security: indirect prompt injection and red teamingAttack the agent itself using corpus documents and tool results containing malicious instructions that attempt to exfiltrate notes, run unrequested code, or divert the answer. Build a reproducible attack battery, measure success rate, and compare defenses by design, including privilege separation, a dual-LLM pattern, and control of data flow to side-effecting tools, with prompt-based defenses.
Final deliverable
Personal research assistant
A local agent that answers questions about the curriculum and papers with verifiable citations, runs calculations in the sandbox, passes its own benchmark with published metrics, and withstands the evaluation suite with its built-in defenses enabled.
Check what you already know
- Budget VRAM for a quantized LLM and its KV cache and enforce outputs that follow a JSON schema
- Build a retrieval evaluation set with verified reference passages
- Implement BM25, dense retrieval, and reciprocal rank fusion and measure recall@k and nDCG
- Evaluate the faithfulness of generated responses that cite retrieved passages
- Design an agent loop with tools, budget limits, and complete traces
- Measure the success rate of indirect prompt injections and the effect of privilege-separation defenses