{"slug": "build-a-tiny-citation-gate-before-trusting-rag-answers", "title": "Build a Tiny Citation Gate Before Trusting RAG Answers", "summary": "A developer built a deterministic citation gate to verify whether cited text contains enough evidence for a claim in RAG systems. The gate checks for missing sources and required terms without semantic reasoning, serving as a fast first test before more complex verification. The approach prioritizes inspectability and precision-recall tradeoffs over full semantic understanding.", "body_md": "A RAG answer can cite a real document and still make an unsupported claim. Retrieval answers “which text was nearby?” Citation verification asks a different question: “does the cited text contain enough evidence for this sentence?”\n\nStart with a deterministic gate before adding another model.\n\n``` python\nfrom dataclasses import dataclass\n\n@dataclass\nclass Claim:\n    text: str\n    citation_ids: list[str]\n    required_terms: set[str]\n\ndef verify(claim: Claim, sources: dict[str, str]) -> dict:\n    missing = [cid for cid in claim.citation_ids if cid not in sources]\n    evidence = \" \".join(sources.get(cid, \"\") for cid in claim.citation_ids).lower()\n    absent = sorted(term for term in claim.required_terms if term.lower() not in evidence)\n    return {\n        \"claim\": claim.text,\n        \"valid_source_ids\": not missing,\n        \"missing_sources\": missing,\n        \"missing_terms\": absent,\n        \"supported\": not missing and not absent,\n    }\n\nsources = {\n    \"doc-1\": \"The service retains audit logs for 30 days.\",\n    \"doc-2\": \"Enterprise plans can export logs as JSON.\"\n}\n\nclaim = Claim(\n    \"All plans retain exportable audit logs for 90 days.\",\n    [\"doc-1\", \"doc-2\"],\n    {\"all plans\", \"90 days\"},\n)\n\nprint(verify(claim, sources))\n```\n\nExpected result:\n\n```\n... 'missing_terms': ['90 days', 'all plans'], 'supported': False}\n```\n\nThis is intentionally not semantic reasoning. It catches missing references and obvious term mismatches while remaining easy to inspect. That makes it a useful first test and a poor final verifier.\n\n`30`\n\nand `thirty`\n\ncan be compared.Measure precision and recall separately. A gate that blocks every answer has perfect recall for bad answers and no product value. A gate that approves everything is fast but meaningless.\n\nThe public [MonkeyCode repository](https://github.com/chaitin/MonkeyCode) describes AI task and project-requirement workflows. Citation gates can be useful anywhere an assistant summarizes repository requirements or task evidence, but this exercise does not test MonkeyCode or describe its implementation.\n\nDisclosure: I contribute to the MonkeyCode project. The repository description is public; this Python lesson is independent.\n\nAfter this exercise, the key lesson should be clear: a citation is an address, not proof. Verification needs its own explicit step.", "url": "https://wpnews.pro/news/build-a-tiny-citation-gate-before-trusting-rag-answers", "canonical_source": "https://dev.to/magickong/build-a-tiny-citation-gate-before-trusting-rag-answers-2hop", "published_at": "2026-07-12 15:12:20+00:00", "updated_at": "2026-07-12 15:15:13.703613+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-safety", "ai-research", "developer-tools"], "entities": ["MonkeyCode"], "alternates": {"html": "https://wpnews.pro/news/build-a-tiny-citation-gate-before-trusting-rag-answers", "markdown": "https://wpnews.pro/news/build-a-tiny-citation-gate-before-trusting-rag-answers.md", "text": "https://wpnews.pro/news/build-a-tiny-citation-gate-before-trusting-rag-answers.txt", "jsonld": "https://wpnews.pro/news/build-a-tiny-citation-gate-before-trusting-rag-answers.jsonld"}}