{"slug": "building-an-agent-that-can-t-afford-to-be-wrong-quran-sanity-agent", "title": "Building an Agent That Can't Afford to Be Wrong: Quran Sanity Agent", "summary": "A developer built Quran Sanity Agent, an open-source bilingual (Arabic and English) research workspace that anchors every verse, translation, and scholarly commentary to immutable records in Sanity's Content Lake so the agent cannot generate Quranic text itself. The TypeScript monorepo combines deterministic GROQ lookups (sub-50ms) with a Sanity Context MCP endpoint over 21,398 library chunks, and the Next.js workspace verifies candidate document IDs and rejects AI notes lacking verbatim source quotes. The project is live and its code is available on GitHub.", "body_md": "*This is a submission for the [Sanity Challenge, Path One: Ship an Agent That Queries Real Content](https://dev.to/challenges/sanity-2026-09-16)*\n\nWhen I first opened the Sanity Challenge announcement, one sentence in the Path One brief immediately grabbed my attention:\n\n*\"Build anything that needs an answer it can't afford to get wrong.\"*\n\nThat one line defined this entire project.\n\nIf you ask an AI chatbot for movie recommendations or a recipe, a small hallucination is harmless. But if you ask it about the Quran, religious scripture, or classical commentary, **getting things wrong is not an option.**\n\nYet that is exactly what standard LLMs do every day:\n\nTraditional RAG (Retrieval-Augmented Generation) doesn't fix this either, because dumping raw chunks into a prompt still leaves the model free to summarize, blur, and paraphrase whatever it wants.\n\nI wanted to see what happens when you treat the problem differently: **what if the AI literally cannot generate Quranic text?** What if the agent only has access to verified, structured records in Sanity, and conflicting historical opinions are modeled cleanly as data rather than smoothed over?\n\nThat is what **Quran Sanity Agent** is built to do.\n\n**Quran Sanity Agent** is a bilingual (Arabic & English) web research workspace where every single verse, translation, and scholarly commentary is anchored to an immutable record in Sanity.\n\nThe app is live, publicly accessible, and backed by a hosted Sanity Studio:\n\n*Inspect real-time Sanity Document IDs, physical book locators, and raw JSON payloads straight from the Content Lake.*\n\nHere are four questions you can paste into the live app to see the structured pipeline in action:\n\n`2:255` (or switch the UI to Arabic and type `البقرة ٢٥٥`).` ayah-2-255` in the Evidence Drawer.`Compare interpretations of Al-Fatiha Basmalah`\n`Compare interpretations of Al-Asr`\n`What does the Quran say about justice even against oneself?`\nThe entire codebase is open-source and available on GitHub:\n\n👉 [github.com/OmarAfifi-CSE/quran-sanity-agent](https://github.com/OmarAfifi-CSE/quran-sanity-agent)\n\nThe project is structured as a TypeScript monorepo:\n\n`web/`:` studio/`:` scripts/`:\nThis project is built around the idea that **an agent is only as reliable as the structure behind its content.** \n\nHere is how Sanity handles every layer of the architecture:\n\n```\n┌──────────────────────────────────────────────────────────┐\n│                   SANITY CONTENT LAKE                    │\n│   • 114 Surahs (Surah number, names, revelation type)    │\n│   • 6,236 Ayahs (Uthmani text, translation, keywords)    │\n│   • 6 Tafsir Authorities (Scholar, death year, school)   │\n│   • 12 Audited Interpretive Claims with primary excerpts │\n└────────────┬─────────────────────────────┬───────────────┘\n             │                             │\n    Exact Lookups (GROQ)          Semantic Queries (MCP)\n             │                             │\n             ▼                             ▼\n┌──────────────────────────┐  ┌─────────────────────────────┐\n│ Deterministic Lake Fetch │  │ Sanity Context MCP Endpoint │\n│  • Sub-50ms response     │  │  • 21,398 library chunks    │\n│  • Direct schema joins   │  │  • Vector embeddings rank   │\n└────────────┬─────────────┘  └────────────┬────────────────┘\n             │                             │\n             └──────────────┬──────────────┘\n                            ▼\n┌──────────────────────────────────────────────────────────┐\n│                 NEXT.JS AGENT WORKSPACE                  │\n│   • Verifies all candidate IDs against Content Lake      │\n│   • Rejects AI notes that lack verbatim source quotes    │\n│   • Renders bilingual cards & Evidence Drawer            │\n└──────────────────────────────────────────────────────────┘\n```\n\n`studio/schemaTypes/`)\nInstead of generic blog or article schemas, Sanity Studio manages structured Quranic and classical commentary data:\n\n`surah`:` ayah`:` tafsirSource`:` athari`, `juridical`, `rational`, `linguistic`).` interpretiveClaim`:` primaryExcerpt`), the physical book locator (` sourceLocator`), the divergence classification (` contradictory`, `complementary`, `consensus`), and editorial status.` sourceEdition` & `libraryChunk`:\nOne of the most practical things about Sanity Studio is how easily you can customize the **Desk Structure** to enforce editorial discipline.\n\nIn [`studio/deskStructure.ts`], I added a custom **\"Needs source review\"** filter:\n\nIf an editor enters a new claim but forgets the primary book excerpt, the exact source URL, or the reviewer sign-off, Sanity immediately catches it and moves it into this review queue.\n\nMore importantly, the Next.js agent's GROQ queries strictly filter for `reviewStatus in [\"source_checked\", \"reviewed\"]`. If a claim hasn't passed the editorial gate, the AI agent is physically unable to see it or use it in an answer.\n\nWhile exact verse numbers (e.g. `2:255`) are resolved instantly via deterministic GROQ queries, thematic questions (e.g. *\"What does the Quran say about justice?\"*) require semantic understanding.\n\nTo handle this, I pointed **Sanity Context** at the imported library of **21,398 chunks** and enabled Content Lake embeddings:\n\n`qkca243t`\n`production`\n`https://api.sanity.io/v1/context/organizations/o831wcpb9/mcp/quran-evidence-mcp`\nBuilding this project made one thing very clear:\n\nThe solution to AI hallucination in high-stakes domains isn't \"better prompts\" or larger models. It's **structured content.**\n\nWhen you treat texts as unstructured strings dumped into a vector database, the AI is always one step away from fabricating an answer. But when you model your domain properly—distinguishing chapters from verses, authorities from editions, and consensus from disagreement—the AI stops guessing and starts acting as an interface to verified knowledge.\n\nSanity was uniquely suited for this: having schemas, the Content Lake, GROQ, and the Context MCP in one unified ecosystem meant I could build a zero-hallucination agent that remains completely transparent with every answer it gives.\n\n*Check out the live project at [quran-sanity.omar-afifi.com](https://quran-sanity.omar-afifi.com) and explore the code on [GitHub](https://github.com/OmarAfifi-CSE/quran-sanity-agent).*", "url": "https://wpnews.pro/news/building-an-agent-that-can-t-afford-to-be-wrong-quran-sanity-agent", "canonical_source": "https://dev.to/omarafifi/building-an-agent-that-cant-afford-to-be-wrong-quran-sanity-agent-1pai", "published_at": "2026-10-04 06:59:52+00:00", "updated_at": "2026-10-04 07:12:05.727376+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "large-language-models", "ai-tools", "structured-data"], "entities": ["Sanity", "Quran Sanity Agent", "GitHub", "Next.js", "Omar Afifi", "GROQ", "TypeScript", "MCP"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/building-an-agent-that-can-t-afford-to-be-wrong-quran-sanity-agent", "markdown": "https://wpnews.pro/news/building-an-agent-that-can-t-afford-to-be-wrong-quran-sanity-agent.md", "text": "https://wpnews.pro/news/building-an-agent-that-can-t-afford-to-be-wrong-quran-sanity-agent.txt", "jsonld": "https://wpnews.pro/news/building-an-agent-that-can-t-afford-to-be-wrong-quran-sanity-agent.jsonld"}}