Table of Contents
Introduction: The Trust Gap in Generative AI
What is Grounding in LLMs?
Why Grounding Matters in Enterprise Applications
The Grounding Architecture: Source Attribution at Every Step
Technology Stack Overview
Step-by-Step Implementation: Backend Development
Defining the Grounding-Aware State Schema
Building the Source-Retrieval Agent
Implementing the Grounded Generation Agent with Citation Enforcement
Creating the Citation Verification Agent
Designing the Confidence Scoring Agent
Constructing the LangGraph Workflow with Memory
Frontend Implementation: Grounding Transparency Interface
Real-Time Use Case: Financial Analysis Assistant
Conclusion: From Black Box to Transparent AI
Introduction
Large Language Models are remarkably fluent but fundamentally untrustworthy they can generate plausible-sounding text that bears no relation to reality. In enterprise contexts, this is unacceptable. A financial analyst cannot act on a revenue figure without knowing its source. A doctor cannot prescribe treatment based on a medical claim without verification. This is where grounding becomes critical. Grounding transforms LLMs from creative generators into verifiable information systems by anchoring every claim to specific, traceable sources. This article presents an enterprise-grade multi-agent LangGraph system that implements rigorous grounding with source attribution, citation verification, and confidence scoring—ensuring every output is not just plausible, but provable.
What is Grounding in LLMs?
Grounding is the practice of constraining LLM generation to information explicitly present in provided source materials, with mandatory citation of those sources. Unlike general RAG (which may retrieve but not verify), grounding enforces:
Source Attribution: Every factual claim must reference its specific source document or chunk
Citation Verification: Cited sources must actually contain the claimed information
Confidence Scoring: Each claim receives a confidence score based on source quality and relevance
Transparency: Users can inspect the exact passages supporting each claim
Grounding is not optional in regulated industries—it's a compliance requirement. Financial reports must cite SEC filings. Legal briefs must reference case law. Medical advice must point to clinical guidelines.
The Grounding Architecture
Our system implements grounding through a four-stage pipeline:
Stage 1 — Source Retrieval: Fetch relevant documents with metadata tracking (source ID, page number, timestamp, authority level)
Stage 2 — Grounded Generation: Generate responses with strict citation requirements—every claim must include a source reference in a structured format
Stage 3 — Citation Verification: Independently verify that each cited source actually supports the claim made near it
Stage 4 — Confidence Scoring: Compute an overall grounding confidence score based on citation accuracy, source authority, and claim-source alignment
The system maintains persistent memory of grounding quality metrics to continuously improve retrieval and generation strategies.
Technology Tags
Python, LangGraph, LangChain, FastAPI, React, PostgreSQL, pgvector, Redis, Pydantic, OpenAI API, Sentence Transformers, Docker, TypeScript, TailwindCSS, RAGAS, DeepEval
Step-by-Step Implementation
1. Grounding-Aware State Schema
from typing import List, Dict, Any, TypedDict, Optional, Literal
from langgraph.graph import StateGraph, END
from langchain_core.messages import HumanMessage, AIMessage
import redis
import json
from datetime import datetime
class GroundedClaim(TypedDict):
claim_text: str
source_id: str
source_excerpt: str
confidence: float
verified: bool
class GroundingState(TypedDict):
messages: List
conversation_id: str
query: str
# Retrieval
retrieved_sources: List[Dict]
# Generation
grounded_response: str
extracted_claims: List[GroundedClaim]
# Verification
citation_accuracy: float
source_authority_score: float
overall_grounding_confidence: float
verification_details: str
# Memory
historical_grounding_quality: float
grounding_log: List[Dict]
2. Source Retrieval Agent
from langchain_community.vectorstores import PGVector
from langchain_huggingface import HuggingFaceEmbeddings
class SourceRetrievalAgent:
"""Retrieves sources with rich metadata for grounding"""
def __init__(self, vector_store: PGVector):
self.vector_store = vector_store
def retrieve(self, state: GroundingState) -> GroundingState:
query = state["query"]
docs = self.vector_store.similarity_search_with_score(query, k=8)
sources = []
for i, (doc, score) in enumerate(docs):
sources.append({
"id": f"src_{i}",
"content": doc.page_content,
"metadata": {
"source_name": doc.metadata.get("source", "unknown"),
"page": doc.metadata.get("page", "N/A"),
"timestamp": doc.metadata.get("timestamp", ""),
"authority_level": doc.metadata.get("authority", "standard"),
"document_type": doc.metadata.get("type", "general")
},
"relevance_score": score
})
state["retrieved_sources"] = sources
return state
3. Grounded Generation Agent with Citation Enforcement
from langchain_openai import ChatOpenAI
class GroundedGenerationAgent:
"""Generates responses with mandatory source citations"""
def __init__(self):
self.llm = ChatOpenAI(model="gpt-4", temperature=0.0)
def generate(self, state: GroundingState) -> GroundingState:
sources_text = "\n".join([
f"[{s['id']}] ({s['metadata']['source_name']}, p.{s['metadata']['page']}): {s['content']}"
for s in state["retrieved_sources"]
])
prompt = f"""You are a precision research assistant. Answer the question using ONLY the provided sources.
STRICT RULES:
1. Every factual claim MUST be followed by a citation in format: [src_X]
2. If information is not in the sources, explicitly state: "Not found in provided sources"
3. Never infer, assume, or fabricate information
4. Use exact numbers, dates, and terminology from sources
5. If sources conflict, present both with their citations
SOURCES:
{sources_text}
QUESTION: {state['query']}
ANSWER (with citations):"""
response = self.llm.invoke(prompt)
state["grounded_response"] = response.content
return state
4. Citation Verification Agent
class CitationVerificationAgent:
"""Verifies that citations actually support the claims"""
def __init__(self):
self.llm = ChatOpenAI(model="gpt-4", temperature=0.0)
def extract_and_verify(self, state: GroundingState) -> GroundingState:
response = state["grounded_response"]
sources = state["retrieved_sources"]
# Extract claims with citations
prompt = f"""Extract each factual claim and its citation from this text.
Return as JSON array of objects with: claim_text, source_id, context_around_citation
Text: {response}
JSON:"""
extract_resp = self.llm.invoke(prompt)
try:
import re
match = re.search(r'\[[\s\S]*\]', extract_resp.content)
claims_data = json.loads(match.group()) if match else []
except Exception:
claims_data = []
# Verify each claim
verified_claims = []
for claim_info in claims_data:
source_id = claim_info.get("source_id", "")
source = next((s for s in sources if s["id"] == source_id), None)
if source:
# Check if source actually supports the claim
verify_prompt = f"""Does this source support this claim?
Source: {source['content'][:600]}
Claim: {claim_info.get('claim_text', '')}
Respond YES or NO with brief reason."""
verify_resp = self.llm.invoke(verify_prompt)
is_verified = "yes" in verify_resp.content.lower()
# Confidence based on source authority and relevance
authority_boost = 0.2 if source["metadata"]["authority_level"] == "high" else 0.0
confidence = min(1.0, source["relevance_score"] + authority_boost)
verified_claims.append(GroundedClaim(
claim_text=claim_info.get("claim_text", ""),
source_id=source_id,
source_excerpt=source["content"][:200],
confidence=confidence,
verified=is_verified
))
state["extracted_claims"] = verified_claims
# Compute citation accuracy
if verified_claims:
verified_count = sum(1 for c in verified_claims if c["verified"])
state["citation_accuracy"] = verified_count / len(verified_claims)
else:
state["citation_accuracy"] = 0.0
return state
5. Confidence Scoring Agent
class ConfidenceScoringAgent:
"""Computes overall grounding confidence"""
def score(self, state: GroundingState) -> GroundingState:
# Source authority score (average across retrieved sources)
authority_scores = []
for src in state["retrieved_sources"]:
auth = src["metadata"]["authority_level"]
if auth == "high":
authority_scores.append(1.0)
elif auth == "medium":
authority_scores.append(0.7)
else:
authority_scores.append(0.4)
state["source_authority_score"] = sum(authority_scores) / max(len(authority_scores), 1)
# Overall grounding confidence
citation_weight = 0.6
authority_weight = 0.3
coverage_weight = 0.1 # How many claims have citations
claims_with_citations = len(state["extracted_claims"])
total_claims = max(claims_with_citations, 1)
coverage = claims_with_citations / total_claims
state["overall_grounding_confidence"] = (
citation_weight * state["citation_accuracy"] +
authority_weight * state["source_authority_score"] +
coverage_weight * coverage
)
# Generate verification details
verified = sum(1 for c in state["extracted_claims"] if c["verified"])
state["verification_details"] = (
f"Verified {verified}/{len(state['extracted_claims'])} claims. "
f"Source authority: {state['source_authority_score']:.2f}. "
f"Citation accuracy: {state['citation_accuracy']:.2f}."
)
return state

Join the conversation! Your thoughts help the community grow.