AZ Abuzar Zulfiqar

Writing · 2026-11-23

Building a RAG System That Says “I Don't Know”: Self-Correcting RAG with Llama 3.1 and LangGraph

By Abuzar Zulfiqar · 2026-11-23

Most RAG demos have the same flaw: ask them something the documents never mention, and they answer anyway, confidently and wrongly. In a demo that is a curiosity. In production it is how a support bot invents a refund policy.

I built Llama Agentic RAG around the opposite rule: an answer must be supported by cited sources, or the system says "I don't know." It runs fully locally on Llama 3.1 8B through Ollama, which fits in 8 GB of VRAM, so private documents never leave the machine.

This post walks through the design and the choices that made it work on a small model.

The workflow

The pipeline is a LangGraph state machine with five stages:

  1. Retrieve the top-k chunks from a vector index. Every chunk has a stable id such as handbook.md#3, so citations stay traceable.
  2. Grade each chunk for relevance with the LLM before anything is generated. Off-topic context never reaches the answer.
  3. Rewrite the query once if no chunk is relevant. Vague questions often recover here; if they don't, the graph refuses.
  4. Generate an answer only from the numbered context, citing [n] after each claim.
  5. Check groundedness: a judge prompt verifies the answer against the context. An unsupported answer is replaced by a refusal.

The key idea is that hallucinations become refusals, not wrong answers. A refusal is annoying; a confident wrong answer is expensive.

Making it work on an 8B model

Large models forgive sloppy prompts. Small models don't, so a few details mattered more than any framework choice:

Measure the refusals

A RAG system that never says "I don't know" isn't production-ready, so the evaluation set deliberately includes unanswerable questions. The harness reports:

Metric What it tells you
Retrieval hit rate Did we retrieve an expected source for answerable questions?
Citation precision Do cited chunks come from expected sources?
Refusal accuracy Were unanswerable questions correctly refused?
False-refusal rate Were answerable questions wrongly refused?
Judge correctness LLM-as-judge score against a reference answer
p50 / p95 latency What does it cost the user in time?

Refusal accuracy and false-refusal rate pull against each other, which is exactly why you need both. Tighten the groundedness check and refusals get safer while false refusals rise. The numbers make that trade-off visible instead of a matter of opinion.

Treat prompt and model changes like code changes: they ship only if the evals hold.

Tests that don't need a GPU

CI runs the full graph with scripted fake LLM replies and deterministic embeddings. That covers the happy path, query rewrite, refusal, ungrounded answers, missing citations and reasoning-tag stripping, all without a model or a GPU. It is the cheapest way to keep a graph's control flow honest as it grows.

Use it from Claude with MCP

The same graph is exposed as an MCP tool, ask_documents, so Claude Desktop, Claude Code or any MCP client can query your documents and get cited answers back. The FastAPI service exposes /ask, /ingest and /health for everything else.

What's next

The roadmap is the usual path from reference to production: hybrid retrieval with BM25 and a cross-encoder reranker, a persistent vector database such as pgvector or Qdrant, tracing with Langfuse, streaming, and PDF and DOCX ingestion.

The code is MIT-licensed: github.com/abuzar18/llama-agentic-rag. If you are building RAG for a team that can't afford confident wrong answers, I'd be glad to compare notes.

Abuzar Zulfiqar is an AI/ML engineer who builds production AI agents, RAG assistants and LLM automation. More at abuzar18.github.io.