AWS Builder Center
AIdeas: TruthLayer — The Real-Time AI Hallucination Firewall

AIdeas: TruthLayer — The Real-Time AI Hallucination Firewall

TruthLayer is a production-ready serverless API that catches AI hallucinations before they reach users. Built on Amazon Bedrock Titan Embeddings V2, AWS Lambda, and DynamoDB — 100% precision, zero false positives, sub-second latency. One API call integrates with any AI system. No model changes required.

Software Engineer | Full-Stack & Applied AI Systems | AWS Builder
An AI that hallucinates in a hospital could cost a life. In a law firm, a lawsuit. In a bank, millions. The question is not whether AI makes mistakes — it is whether you catch them before your users do.

App category

Workplace Efficiency
TruthLayer is infrastructure for organizations deploying AI in high-stakes environments — customer support, legal research, regulatory compliance, medical documentation. Any team using AI to assist human decision-making needs one thing: a guarantee that what the AI says actually corresponds to reality.
TruthLayer is that guarantee.

My vision

The Problem I Saw

In 2025, a law firm submitted a legal brief containing case citations that did not exist. Their AI assistant had fabricated case names, dates, and rulings with full confidence. The lawyers trusted it. The judge sanctioned them.
This was not a failure of AI technology. It was a failure of the infrastructure around AI — there was no layer between the model and the real world that simply asked: "Is this actually true?"
That is what I built.

What TruthLayer Does

TruthLayer is a production-ready verification API deployed on AWS. It sits silently between any AI model and its users, intercepting every AI response before it reaches a human and certifying whether each claim is:
StatusMeaningAction
✅ VERIFIEDSemantically and factually grounded in your source documentsSafe to display
⚠️ UNCERTAINTopically related but not fully confirmedDisplay with caveat or escalate
❌ UNSUPPORTEDNot found in any source — likely hallucinatedBlock or flag for human review
Integration takes one API call. No model fine-tuning. No prompt changes. No changes to existing AI infrastructure. One layer added — and hallucinations become visible.
🌐 Live demo: truth-layer.vercel.app 

Why this matters

The Scale of the Problem

AI hallucination is not a niche research problem. It is a production crisis happening right now at every organization that has deployed large language models in a real workflow:
IndustryReal Hallucination ScenarioCost of One Mistake
HealthcareAI recommends aspirin for a child. Source says: never give aspirin to children — risk of Reye syndrome.Patient harm, malpractice
LegalAI summarizes liability cap as $500K. Contract says $50K.$450K undetected exposure
FinanceAI states GDPR fine is 2% of revenue. Law states 4%.Non-compliant advice, regulatory fine
HRAI says the refund policy is "non-refundable." Policy is refundable.Employee lawsuits
Customer SupportAI claims product has "unlimited" storage. Plan has a 100GB cap.Customer churn, brand damage
InsuranceAI fabricates a coverage clause that doesn't exist.Multi-million dollar claims dispute
Studies consistently show 15–30% of AI-generated responses contain at least one factual error. For a company running 100,000 AI interactions per month, that is 15,000 to 30,000 potentially harmful responses — every single month — with no systematic way to catch them.

Why Existing Solutions Fail

ApproachProblem
Human reviewDoesn't scale. Defeats the purpose of AI automation.
LLM-as-JudgeAdds 3–8 seconds, costs $0.01–$0.10 per check, and LLMs confidently hallucinate about hallucinations.
Embedding-only similarityBlind to numbers, negations, and logical contradictions — precisely the most dangerous hallucination types.
The third failure is the one TruthLayer was built to solve. The embedding similarity between "GDPR fines are up to 2% of revenue" and "GDPR fines are up to 4% of revenue" is 0.97 out of 1.0. A system relying on embeddings alone would classify the wrong answer as VERIFIED. TruthLayer catches it.

How I built this

Architecture Overview

Architecture Diagram
TruthLayer is built entirely on AWS serverless infrastructure. The full verification pipeline:
1
[Client Application] ↓ POST /verify + x-api-key header[API Gateway REST API] ↓ auth validated[AWS Lambda — Verification Engine] ├── 1. Claim Extractor → parse AI response into atomic factual sentences ├── 2. Text Splitter → chunk source docs (500 chars, 50-char overlap) ├── 3. Cache Check → SHA-256 hash → look up in TruthLayerEmbeddings ├── 4. Bedrock Titan V2 → 1024-dim vectors (only on cache miss, ~150ms) ├── 5. Cosine Similarity → NumPy dot product → best source match per claim ├── 6. Entity Checker → penalties for numbers, negations, superlatives └── 7. Confidence Scorer → VERIFIED / UNCERTAIN / UNSUPPORTED ↓ async write[TruthLayerVerifications Table] ↓[Structured JSON Response → claims[], summary{}, metadata{}]

The Core Innovation: Dual-Signal Verification

Every other hallucination detection system uses one signal. TruthLayer uses two.
Signal 1 — Semantic Similarity (Amazon Bedrock Titan Embeddings V2)
Claims and source chunks are embedded into 1,024-dimensional semantic vectors. Cosine similarity finds the best-matching source chunk for each claim.
Signal 2 — Entity Contradiction Checker (Proprietary)
Semantic similarity answers "Is this claim about the same topic?" but cannot answer "Does this claim contradict the source on specific values?"
The Entity Contradiction Checker applies multiplicative penalties for three contradiction classes that embedding models fundamentally cannot detect:
Contradiction TypeExampleDetection MethodPenalty
Numerical mismatchClaim says "2% fine" / Source says "4% fine"Regex extracts all numeric tokens× 0.5
Negation mismatchClaim says "non-refundable" / Source says "refundable"Negation word pattern matching× 0.6
Superlative vs. specificClaim says "unlimited" / Source says "1,000 calls/month"Absolute term detection× 0.6
Final Score = Cosine Similarity × Alignment Penalty
A claim with 0.97 embedding similarity but a number mismatch scores 0.97 × 0.5 = 0.485 → UNSUPPORTED. The hallucination is caught.

Intelligent Claim Extraction

Not every sentence needs verification. TruthLayer filters sentences by factual content using a weighted scoring system:
SignalWeightExample
Contains number or date+0.30"Revenue reached $4.2B in Q3"
Contains proper noun+0.20"Amazon announced..."
Contains percentage+0.15"99.9% uptime SLA"
Hedge word present−0.40"might", "possibly", "approximately"
Ends with "?"EliminatedQuestions are not factual claims
Opinion marker−0.50"I think", "I believe"
A sentence scoring ≥ 0.20 is treated as a verifiable claim. Compound sentences are split into atomic claims — "Founded in 2019 with 500 employees across 12 countries" becomes three independently verified assertions.

The Embedding Cache — Speed Without Compromise

Amazon Bedrock delivers state-of-the-art embedding quality — but calling it on every request for every document chunk is slow and costly. TruthLayer's DynamoDB-backed embedding cache eliminates redundant Bedrock calls:
ScenarioWhat HappensLatency
First verification of a documentBedrock called, vectors stored in DynamoDB with 7-day TTL~150ms per chunk
All subsequent verificationsSHA-256 hash → DynamoDB get_item → vector returned~5ms
Cache hit rate on repeat content100%—
End-to-end latency (cache hit)—~750ms
End-to-end latency (cache miss)—~900ms
Cost savings vs. no cache~80% reduction in Bedrock API spend—

AWS Services — The Complete Stack

AWS ServiceRole in TruthLayerConfiguration
Amazon BedrockTitan Embeddings V2 — 1024-dim semantic vectorsamazon.titan-embed-text-v2:0, us-east-1
AWS LambdaVerification engine — 4 functionsPython 3.9, 1024MB memory, 60s timeout
Amazon API GatewayREST API — routing, CORS, auth headers5 endpoints: /verify, /documents, /analytics, /health
Amazon DynamoDBAll persistent storage4 tables, on-demand billing, encrypted at rest
AWS SAMFull Infrastructure as CodeAll infra in one template.yaml — one command to deploy
Dashboard: The real-time monitoring dashboard is built with Next.js 16 (App Router) and connects directly to the AWS backend APIs.

Cost Analysis

One of TruthLayer's design goals was to make enterprise-grade hallucination detection affordable. Here is the complete monthly cost breakdown at 50,000 verifications:
AWS ServiceMonthly UsageCost
AWS Lambda50,000 invocations, ~100GB-seconds$0.00 (Free Tier)
Amazon API Gateway50,000 REST API requests$0.00 (Free Tier)
Amazon DynamoDB6GB storage, on-demand reads/writes$0.00 (Free Tier)
Amazon Bedrock Titan V2~15M tokens at $0.0001/1K~$1.50
CloudWatchBasic monitoring and logs$0.00 (Free Tier)
Total50,000 AI verifications/month~$1.50
$1.50 per month. For 50,000 verified AI responses. That is $0.00003 per verification — three thousandths of a cent. By comparison, LLM-as-judge approaches using GPT-4 cost $0.01–$0.05 per check, making TruthLayer 300–1,600× cheaper at equivalent scale.

The Four DynamoDB Tables

TablePrimary KeyPurposeTTL
TruthLayerApiKeysapi_key_hash (SHA-256)Authentication — raw keys are never storedNone
TruthLayerDocumentsdocument_id (UUID)Uploaded source documentsNone
TruthLayerEmbeddingsdocument_id + chunk_index1024-dim vector cache7 days
TruthLayerVerificationsverification_id (UUID)Full audit trail of every request90 days

Security Architecture

API Key Security: When a key is generated, only its SHA-256 hash is stored in DynamoDB. The raw key is shown to the user once and never saved. Even with full database access, no raw key can be recovered — the same model Stripe and GitHub use.
Rate Limiting: Every request atomically increments a usage_count counter via DynamoDB conditional writes. When the counter hits the configured limit, the API returns HTTP 429 with a Retry-After header — enforced at the database level, not the application layer.
IAM Least Privilege: Each Lambda function holds only the exact DynamoDB permissions it needs. The verify function cannot write to the API keys table. If one function were compromised, the blast radius is strictly contained.
Zero External Dependencies: The entire backend runs on Python stdlib + boto3. No PyPI packages. Zero supply-chain attack surface.

Latency Breakdown

Where does the time go in a typical warm verification request?
Pipeline Stagep50p95
API Gateway routing3ms5ms
Lambda warm start< 1ms5ms
Claim extraction10ms25ms
DynamoDB cache lookup5ms10ms
Bedrock Titan V2 (cache miss)150ms200ms
NumPy cosine similarity15ms25ms
Entity contradiction check2ms5ms
Response serialization2ms5ms
Total (cache hit)~750ms~870ms
Total (cache miss)~900ms~1,100ms
The key insight: Bedrock embedding accounts for 80% of total latency. The embedding cache directly targets this bottleneck — not as a performance nice-to-have, but as the core architectural solution to making TruthLayer fast enough for production use.

Production Resilience

RiskMitigation
Bedrock latency spikeCache eliminates 80%+ of Bedrock calls — most requests unaffected
Lambda cold start (~3.1s)Lazy init outside handler + container reuse on warm requests
DynamoDB throttlingOn-demand billing + exponential backoff with jitter
Cost overrunAWS Budget alerts at $17 and $20/month
Service Degradation Levels:
LevelTriggerBehavior
0 — NormalAll systems healthyFull Bedrock embedding verification
1 — DegradedBedrock latency > 100msCache-first, reduce max claims from 10 to 5
2 — PartialBedrock unavailableFallback keyword matching with reduced_confidence flag
3 — MaintenanceCritical failureHTTP 503, queue requests for later processing

Integration Ecosystem

Official SDKs:
SDKLanguageInstall
Python SDKPython 3.9+pip install truthlayer-sdk
TypeScript SDKNode.js 18+npm install @truthlayer/sdk
Python SDK — 3 lines:
1
pythonfrom truthlayer import TruthLayertl = TruthLayer(api_key="tl_your_key")result = tl.verify(ai_response="...", source_documents=["..."])
Three Production Demos:
1. Customer Support Chatbot — Routes AI responses through TruthLayer before reaching customers. UNSUPPORTED claims are silently replaced with: "Let me confirm this — please contact support directly." Zero hallucinations reach the user.
2. Legal Contract Analyzer — Upload a contract once, store its
document_id. Every AI analysis of that contract is verified by ID — no resending 50 pages per call. Outputs a hallucination risk score and highlights which clauses are ungrounded.
3. Document Q&A System — Upload a knowledge base, ask questions, receive answers with sentence-level source attribution and claim-by-claim verification status.
LangChain Integration: A native LangChain wrapper in integrations/langchain/ runs TruthLayer verification on any chain output — compatible with agents, RAG pipelines, and conversational chains.

The Real-Time Dashboard

Real-time monitoring dashboard verifications tracked live
Built on Next.js 16 App Router, the dashboard at truth-layer.vercel.app/dashboard  provides:
  • Verification analytics — verified / uncertain / unsupported counts with trend graphs
  • Claim-level results — each sentence color-coded 🟢 🟡 🔴
  • Source attribution — for every verified claim, the exact source sentence that supports it
  • API key management — generate, view usage, revoke
  • Cache performance — hit rate and latency with vs. without cache

API Reference

EndpointMethodAuthPurpose
POST /verifyPOST✅ RequiredVerify AI response against source documents
POST /documentsPOST✅ RequiredUpload source document, receive document_id
GET /documentsGET✅ RequiredList all uploaded documents
GET /analyticsGET✅ RequiredVerification statistics
GET /healthGET❌ PublicHealth check
Request:
1
json{ "ai_response": "Aspirin is safe for children with fever.", "source_documents": ["Aspirin should NOT be given to children — risk of Reye syndrome."]}
Response:
1
json{ "claims": [ { "text": "Aspirin is safe for children with fever.", "status": "UNSUPPORTED", "confidence": 28.4, "matched_source": "Aspirin should NOT be given to children..." } ], "summary": { "verified": 0, "uncertain": 0, "unsupported": 1 }, "metadata": { "latency_ms": 892, "cache_hits": 0, "cache_misses": 2 }}
The negation ("is safe" vs "should NOT") reduced the score from 0.91 to 0.284 — correctly blocking a response that could cause serious harm to a child.

Demo

TruthLayer Demo Video

The video shows:
  1. Landing page — interactive try-it-now demo with a deliberate hallucination
  2. Number mismatch: GDPR 2% vs 4% — how the entity checker catches it
  3. Negation mismatch: aspirin/children — UNSUPPORTED result
  4. Dashboard: verified / uncertain / unsupported analytics in real time
  5. API key generation: SHA-256 security model
  6. First API call: generate key → POST /verify → structured JSON response
Try it yourself in 60 seconds:
  1. Go to truth-layer.vercel.app 
  2. Click "Get API Key" → key generated instantly
  3. Paste any AI response + any source document → see claim-by-claim verification
  4. Or call the API directly: POST https://qoa10ns4c5.execute-api.us-east-1.amazonaws.com/prod/verify

The TruthLayer landing page — live at truth-layer.vercel.app

Three steps: Send AI output → Dual-Signal Analysis → Trust Verdict

Interactive live demo — real API call, real results, not a mockup

From install to first verification in under 2 minutes

Generate a free API key to start verifying AI outputs. 1,000 requests/month included.

Six enterprise-grade capabilities — Sub-second latency, 100% precision, smart caching, entity checker, one-line integration, real-time dashboard

What I learned

1. Embeddings are brilliant — and dangerously incomplete
"$2 million" and "$20 million" produce a cosine similarity of 0.97. They mean nearly the same thing semantically — a financial amount — while being completely different factually. Embedding models are not designed to catch this. They capture meaning, not precision. A complete verification system needs both signals: semantic relevance and factual precision. Most existing tools use only the first.
2. The cache is as important as the algorithm
Early TruthLayer prototypes hit Bedrock on every request. A 3-document verification took 3–4 seconds. After the DynamoDB embedding cache, the same verification dropped to 750ms. This was not an optimization — it was the difference between a usable API and an unusable one. Documents don't change. Their embeddings shouldn't be recomputed every time. Caching is infrastructure design.
3. Serverless + IaC = a solo developer can build production systems
TruthLayer runs four Lambda functions, four DynamoDB tables, and an API Gateway with authentication and rate limiting — maintained by one student. The entire infrastructure lives in one template.yaml SAM file. Adding a table takes ten lines. Rolling back a deployment takes one command. Without Infrastructure as Code, this project would not exist.
4. 100% precision is the correct optimization target for safety systems
TruthLayer's benchmarks show 100% precision — zero false positives across 18 test cases. Every claim classified VERIFIED was genuinely supported. This was a deliberate design choice. The cost of a false positive (classifying a hallucination as VERIFIED) is always higher than the cost of a false negative (flagging a correct claim for review). In safety-critical systems, precision is not just a metric. It is the entire point.
5. Data stays in AWS — and that changes the enterprise conversation
Building on Bedrock rather than OpenAI was a deliberate choice. Healthcare, legal, and financial organizations have strict data governance requirements. Sending patient records or legal contracts to an external API through the open internet is often prohibited. Bedrock keeps everything within the AWS ecosystem. Compliance requirements are met by default. Enterprise readiness should not require extra configuration.
6. The right tools change what you can build alone
Using Kiro showed me how much faster development becomes when your IDE understands the infrastructure you're deploying to. Instead of looking up CloudFormation docs constantly, I stayed focused on the actual algorithm. The right tools don't just save time — they change what you can build alone.
7. The real problem is trust, not technology
The hardest thing about this project was not the algorithm — it was articulating why hallucination detection infrastructure is necessary when AI models keep improving. The answer: improvement is not elimination. AI models will never achieve perfect factual accuracy at inference time without real-time access to a verified ground truth. TruthLayer provides that ground truth. And that problem will exist as long as AI generates language.

Performance Benchmarks

(Live production API — AWS us-east-1, March 2026)
MetricValue
Precision100% — zero false positives across 18 benchmark cases
False positive rate0%
Unit tests passing87 / 87
Cache hit rate (repeat content)100%
Warm latency — cache hit~750ms
Warm latency — cache miss~900ms
Cold start~3.1s (first request after idle only)
Embedding dimensions1,024 (Titan Embeddings V2)

What's next

TruthLayer v1 is live and processing real verification requests today. The roadmap is built around three phases: first eliminating remaining latency, then scaling to enterprise data volumes, then expanding to regulated industry compliance requirements.
TimeframeFeatureBusiness Reason
Near-term (v2)Streaming verification — verify claims as LLM generates tokensReduces perceived latency to zero for streaming AI apps
Near-term (v2)FAISS approximate nearest neighbor for 1,000+ chunk documentsEnables enterprise knowledge bases with millions of chunks
Near-term (v2)Multi-language supportAPJC market expansion — Indian, Japanese, Korean enterprise customers
Mid-term (v3)SOC2 / HIPAA compliance audit trail exportRequired for healthcare and financial services enterprise contracts
Mid-term (v3)Industry-specific verification thresholdsHealthcare: ≥0.90 VERIFIED. Customer support: ≥0.75. Not one size fits all.
Mid-term (v3)Per-tenant rate limiting and usage dashboardsEnterprise billing model — each customer gets isolated quotas and reporting
Long-termOpenSearch / pgvector integrationReplace DynamoDB embeddings table for sub-10ms retrieval at billion-vector scale
Long-termSLA monitoring dashboard (p99, uptime, error rate)Enterprise customers require contractual uptime guarantees
Long-termMarketplace listing (AWS Marketplace)Pay-as-you-go commercial model, direct AWS customer reach
The long-term vision: TruthLayer becomes the standard hallucination verification layer that every enterprise AI deployment includes by default — the way every API includes authentication. Not an optional add-on. Infrastructure.

The invisible trust layer that makes AI safe for the people who depend on it.
🌐 Live: truth-layer.vercel.app  📦 Python SDK: pip install truthlayer-sdk
Built solo — one student, three weeks, zero infrastructure team.
Stack: Amazon Bedrock · AWS Lambda · Amazon API Gateway · Amazon DynamoDB · AWS SAM · Next.js 16 · Python 3.9 · Kiro IDE
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article