Dev.to AI 🤖 Ai 👁 0 📖 3 min read

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

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 po

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:

  • One-word verdicts. Llama 3.1 8B is unreliable with complex JSON schemas, so the grading and groundedness prompts ask for yes or no, and parsing is tolerant of extra words.
  • Strip reasoning tags. If you swap in a reasoning model, its <think> blocks are removed before parsing.
  • Citations are enforced, not requested. An answer without valid [n] citations is treated as ungrounded, even if it sounds right.
  • Config over code. Every model, path and threshold is an environment variable, so trying another Ollama model is a one-line change.

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.

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

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.