Skip to main content

Security & Safety Guardrails

In financial and legal RAG systems, accuracy is mandatory. A wrong answer is worse than no answer. My architecture implements a "Safety-First" approach with three distinct layers.


1. PII Shield (Privacy First)

Before the user's query ever reaches an LLM (OpenRouter/Qwen), it passes through a regex-based PII Shield. This masks sensitive Indian identifiers to ensure GDPR-grade privacy.

  • Masked Entities: Aadhaar Numbers, PAN Cards, Mobile Numbers, Email Addresses, IFSC codes.
  • Why Regex? It's local, 0ms latency, and 100% deterministic. No private data ever leaves the server.

2. Hallucination Guard (LLM-as-Judge)

The most critical node in the LangGraph StateGraph is Node 7: Hallucination Guard. It acts as a validator that verifies the relationship between the Retrieved Context and the Generated Answer.


3. Resilience: Circuit Breakers

To prevent cascading failures when external APIs (OpenRouter, Jina, Pinecone) are slow or down, I use pybreaker.

StateBehavior
ClosedNormal operation. Requests flow through.
Open3 consecutive failures detected. All requests fail fast (0ms) to save resources.
Half-OpenAfter 30s, one test request is allowed. If it succeeds, the circuit closes.
Engineering Takeaway

"By combining deterministic PII masking, agentic hallucination checks, and industrial-strength circuit breakers, the system achieves 'Financial Grade' reliability on a zero-cost infrastructure."