Secure, fast, and drop-in PII redaction and context preservation reverse proxy for Large Language Models.
SOC 2 Type II and HIPAA compliance for LLM streams without breaking real-time latency.
LLM-Shield-Proxy is an open-source, zero-egress AI Gateway and LLM Firewall deployed directly within your corporate VPC. It intercepts OpenAI-compatible LLM API requests, redacts Personally Identifiable Information (PII) and raw secrets before they leave your infrastructure, and deterministically re-hydrates real-time Server-Sent Events (SSE) chat responses with ultra-low stream latency.
Designed to enforce Zero Trust AI and unblock enterprise privacy compliance (SOC 2 Compliance for AI, HIPAA, HITRUST without breaking real-time streaming latency).
- Sub-Millisecond SSE Rehydration: Patent-pending sliding-window buffer reconstructs fragmented sensitive tokens across Server-Sent Events without breaking real-time UX or introducing network lag (<4.3 ยตs overhead per chunk).
- Zero-Egress Synthetic Masking: Advanced Data Loss Prevention (DLP) for LLMs using format-preserving substitution (Regex + Shannon Entropy + ONNX NER) ensuring PII never traverses the public internet.
-
Zero-Data Stateless Cryptography: Ephemeral TTL vaults and AES-256-GCM envelope encryption guarantee zero long-term data liability (operating in an ultra-low footprint of
<=55MB RAM). -
Role-Based Policy-as-Code & Hot-Reloading: Zero-downtime YAML file watcher (
policies.yaml) dynamically mapsvirtual_key_ididentities to granular security roles, custom PII profiles, and thread-safe$O(1)$ setting overrides. -
Universal Decision Trace Exporter: Every PII redaction and agent RBAC decision is cryptographically sealed in a local WORM-compliant Merkle Tree. Export tamper-evident NIST OSCAL artifacts and OpenTelemetry
gen_ai.*spans directly to your GRC platform (Vanta/Drata) or SIEM (Datadog) for strict SOC 2 Compliance for AI, ISO 42001 AI Management System forensics, and comprehensive LLM Security Posture Management (LLM SPM). -
Streaming Tool-Call Interception & Agent Governance: Intercepts real-time LLM function calls (e.g.,
exec_sql,shell_exec) mid-stream using a zero-allocation JSON parser, enforcing fail-closed tool access controls backed by Redis policy stores to prevent agent drift. -
Service Mesh Native gRPC Sidecar: Stream buffers directly over Unix Domain Sockets (UDS) via Envoy's
ext_procfor zero HTTP network hops, paired with a zero-dependency Kubernetes Mutating Webhook. -
ReDoS-Immune C++ DFA Engine: Pre-compiled Deterministic Finite Automatons (
google-re2) guarantee linear execution time against adversarial regex payloads. - Universal Zero-SDK Translators: Drop-in compatibility for existing OpenAI SDKs with automatic edge-translation to Anthropic, Gemini, and vLLM schemas.
This repository provides the reference proxy architecture and benchmark suite for resolving SSE stream fragmentation in enterprise sandboxes, as proposed in:
- Upstream Proposal: NVIDIA/OpenShell #2763
- Preprint Publication: DOI: 10.5281/zenodo.21955770
Because LLM-Shield-Proxy natively mimics the OpenAI specification, you do not need to rewrite your application code. You simply change the base_url in your SDK or the endpoint in your curl command. The proxy intercepts the payload, redacts it, and translates the schema to the correct upstream provider automatically.
Option A: cURL
# โ Before: Sending raw PHI directly to OpenAI
curl https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer sk-openai-key" \
-d '{"messages": [{"role": "user", "content": "My SSN is 000-00-0000"}]}'
# โ
After: Sending payload through LLM-Shield (Zero Egress)
curl http://localhost:8000/v1/chat/completions \
-H "Authorization: Bearer shield-virtual-key" \
-d '{"messages": [{"role": "user", "content": "My SSN is 000-00-0000"}]}'Option B: Python SDK (1-Line Change)
from openai import OpenAI
client = OpenAI(
api_key="your-openai-api-key",
base_url="http://localhost:8000/v1", # Point to LLM-Shield-Proxy
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Contact Sarah Connor at sarah@example.com or 555-0199."}],
stream=True,
)
for chunk in response:
print(chunk.choices[0].delta.content or "", end="", flush=True)Spin up the zero-egress proxy in seconds.
Option A: Run the Live Streaming Demo (Docker Compose)
# 1. Spin up the proxy container in background
docker compose up -d
# 2. Verify health probe
curl http://localhost:8000/healthz
# 3. Run the live demo script
python examples/demo.pyOption B: Standalone Container (Production Base)
docker run -d -p 8000:8000 \
-e OPENAI_API_KEY="sk-your-openai-api-key" \
-e HOST="0.0.0.0" \
-e PORT=8000 \
--name llm-shield-proxy \
ghcr.io/ninadphalak/llm-shield-proxy:latestLLM-Shield-Proxy is heavily modular. You can configure the engine based on your specific compliance ROI and memory constraints:
| Installation Tier | Command | Capabilities Included | Use Case / Trade-off |
|---|---|---|---|
| Standard Mode (Microsecond Proxy) |
pip install llm-shield-proxy |
Tier 1 (Regex) & Tier 2 (Shannon Entropy) | Best for DevOps & Secrets: Operates with ultra-low memory (<60MB RAM) and maximum throughput. Coverage: 100% deterministic catch rate for structured compliance data (SSNs, Emails, IP/MAC) and high-entropy cryptographic secrets (API Keys, Hex tokens). Misses conversational/free-text names. |
| Full NLP Mode (Contextual NER) |
pip install "llm-shield-proxy[ner]" |
Adds Tier 3 (ONNX Runtime NER) | Best for HIPAA/GDPR: Adds a quantized BERT-NER model via ONNX runtime to extract conversational PII (Patient Names, Organizations) from free-text. Coverage: >95% F1 Recall for contextual entities on standard benchmark datasets, matching the accuracy of enterprise cloud NLP APIs (AWS Comprehend, Google Cloud DLP, Microsoft Presidio) at 10x lower memory. Trade-off: Requires an additional ~45MBโ65MB of RAM for the quantized ONNX model weights and inference session. |
Enabling Tier 3 ONNX NER: When installed with
[ner], enable deep neural entity extraction by settingENABLE_TIER3_ONNX_NER=truein your.envor environment variables (and optionally pointONNX_MODEL_PATHto custom model weights). If disabled or not installed, the engine automatically and gracefully bypasses Tier 3 with zero startup overhead.
LLM-Shield-Proxy is not locked into a single NER model. Enterprise architectures can plug in any domain-specific Hugging Face transformer exported to ONNX by pointing ONNX_MODEL_PATH (along with its tokenizer.json):
- Healthcare & HIPAA: Load quantized BioBERT, ClinicalBERT, or Med-BERT models to redact clinical patient notes and medical records.
- Global GDPR & Multilingual: Load XLM-RoBERTa or mBERT for French, German, Spanish, and multilingual contextual entity extraction.
- Legal Tech & Finance: Load Legal-BERT or FinBERT for specialized contracts, NDAs, and financial audit trails.
- Zero Overhead When Disabled: If
ENABLE_TIER3_ONNX_NER=false, the ONNX runtime is completely bypassed, maintaining the ultra-low<60MBRAM and<6 ยตsfootprint.
Enterprise compliance often requires scanning for proprietary internal formats (e.g., custom employee IDs, internal project codenames, or proprietary billing tokens). LLM-Shield-Proxy allows you to inject custom regex rules that are evaluated alongside Tier 1 without risking catastrophic ReDoS (Regular Expression Denial of Service).
To inject custom regexes, mount a custom_regex.yaml file into the proxy and point CUSTOM_REGEX_PATH to it.
Security & ReDoS Immunity:
Naive reverse proxies can crash when evaluated against poorly written backtracking regexes like (a+)+$. To prevent this, LLM-Shield-Proxy leverages the google-re2 C++ engine for all BYOR custom patterns. It parses your YAML configuration via Pydantic during the FastAPI lifespan startup event, and compiles all patterns using re2, mathematically guaranteeing O(N) execution time regardless of how complex your regex or how adversarial the streaming payload is. This ensures complete immunity against ReDoS attacks without sacrificing the microsecond latency overhead.
# custom_regex.yaml
custom_patterns:
- name: INTERNAL_EMPLOYEE_ID
pattern: "(?i)EMP-[A-Z]{3}-\\d{5}"
description: "Matches internal Acme Corp employee IDs"# Start the proxy server locally on port 8000
llm-shield-proxy --host 0.0.0.0 --port 8000 --workers 1| Existing Legacy Proxies | LLM-Shield-Proxy |
|---|---|
| Destroys Real-Time SSE Streaming: Buffers entire responses before scanning, causing multi-second UI latency stalls. | Ultra-Low Latency Streaming: Redacts and re-hydrates delta-by-delta as SSE packets stream. |
| Heavy Memory Footprint: Requires 1GBโ2GB RAM for heavy spaCy or PyTorch NLP libraries. | Ultra-Lightweight <60MB RAM: Runs on a microsecond compiled regex + Shannon entropy + synthetic generator engine. |
| Data Liability: Stores user PII in long-term databases. | Zero Long-Term Storage (Zero-Data Mode): Self-destructing TTL session vault built for zero data liability. Operates in strict "Zero-Data Mode"โno prompts, PII, or context windows are ever written to persistent disk or external storage. |
| Complex Cloud Egress: Routes data to 3rd-party SaaS inspection APIs. | 100% Zero-Egress VPC: All scanning happens locally inside your secure corporate boundary. |
Designed specifically for highly regulated enterprise environments, strict Zero Trust AI network architectures, and security-first engineering teams implementing LLM Security Posture Management (LLM SPM).
- Keeps data in your VPC: The shield runs 100% inside your corporate boundary without transmitting unredacted data to external third parties.
- Zero-Data Storage: Sensitive prompts are never persisted. The proxy utilizes self-destructing in-memory vaults with deterministic TTL eviction.
- Continuous Stability: Validated under high-concurrency stress testing to maintain consistent throughput and sub-millisecond latency.
- Transparent Rule Engine: Combines transparent, deterministic pattern matching with Shannon entropy and local ONNX neural entity recognition.
It's a crowded space. Here is exactly why you should deploy LLM-Shield-Proxy instead of the alternatives:
- Microsoft Presidio / spaCy: Legacy libraries that consume 1GB+ of RAM and block your event loop with 50-150ms of latency per request. (Because nothing says "real-time AI" like pausing the universe for regex). LLM-Shield-Proxy uses a flat <60 MB footprint with <6 ยตs latency overhead.
- Cloud AI Safety APIs (Azure/AWS): Checking for PII by sending raw data out of your VPC defeats the purpose. With LLM-Shield-Proxy, the data never leaves your infrastructure unredacted.
- Standard Regex Gateways: They break on asynchronous Server-Sent Events (SSE). If a sensitive token is split across two streaming packets, standard gateways let it leak. LLM-Shield-Proxy uses a sliding-window lookahead buffer to seamlessly hold split tokens without breaking stream formatting.
- LiteLLM / LangChain: LLM-Shield-Proxy is not a model router or orchestration framework. It works alongside them. Put LLM-Shield-Proxy in front of your orchestrator to guarantee data masking before routing.
LLM-Shield-Proxy is not a model router. It is designed to deploy as a transparent edge proxy directly in front of industry-standard orchestration tools. It stacks with your existing AI routing infrastructure, requires zero code changes, and is compatible out-of-the-box with:
- Orchestration Frameworks: LangChain, LlamaIndex, Semantic Kernel, AutoGen, CrewAI.
- AI Gateways & Routers: LiteLLM, Cloudflare AI Gateway, Kong AI Gateway, Portkey. (Note: You can seamlessly stack LLM-Shield-Proxy in front of LiteLLM to combine multi-model routing with strict zero-egress PII redaction and AES-256-GCM encryption).
- Local & Open-Source Inference: vLLM, Ollama, NVIDIA NIM, Hugging Face TGI.
- Upstream Providers: OpenAI, Anthropic, Google Gemini, DeepSeek, Mistral.
Drop LLM-Shield-Proxy directly in front of them to guarantee deterministic, SOC 2-compliant data masking before the payload ever reaches the orchestrator.
LLM-Shield-Proxy supports two configurable tokenization strategies out of the box:
| Mode | Configuration | Description | Best For |
|---|---|---|---|
| Synthetic Swapping (Default) | ENABLE_SYNTHETIC_SWAPPING=true |
Deterministically substitutes PII with realistic, unbracketed entities (e.g., Maya, Springfield) to eliminate Byte-Pair Encoding (BPE) token bloat and preserve LLM attention weight distributions. |
Modern LLMs, cost & latency optimization |
| Structural Tagging | ENABLE_SYNTHETIC_SWAPPING=false |
Substitutes PII with explicit bracketed type tags (e.g., [PERSON_1], [EMAIL_1]). |
Legacy compliance pipelines, deterministic regex auditing |
โถ Click to view Structural Tagging Demo (Bracketed Tag Stream)
Demonstration of microsecond streaming rehydration using explicit bracketed tags ([PERSON_1], [EMAIL_1]).
%%{init: {'themeVariables': {'edgeLabelBackground': '#ffffff'}}}%%
flowchart TD
classDef client fill:#e0f2fe,stroke:#0284c7,stroke-width:2px,color:#0369a1,font-weight:bold;
classDef proxyEngine fill:#f8fafc,stroke:#475569,stroke-width:2px,color:#0f172a,font-weight:bold;
classDef piiSecurity fill:#fef2f2,stroke:#ef4444,stroke-width:2px,color:#991b1b,font-weight:bold;
classDef vault fill:#fffbebe,stroke:#f59e0b,stroke-width:2px,color:#92400e,font-weight:bold;
classDef upstream fill:#f3e8ff,stroke:#9333ea,stroke-width:2px,color:#6b21a8,font-weight:bold;
UserApp["๐ค Client Application\n(OpenAI / LangChain SDK)"]:::client
subgraph SecurityMoat ["๐ก๏ธ LLM-Shield-Proxy VPC Security Gateway"]
direction TD
InboundAuth["๐ Inbound Auth & Virtual Key Swapping\n(Constant-Time Verification)"]:::proxyEngine
subgraph CascadeEngine ["๐ 3-Tier Multi-Modal & CJK Redaction Engine"]
Tier1["Tier 1: Pre-compiled DFA Regex\n(<0.03ms Pattern Matching)"]:::piiSecurity
Tier2["Tier 2: Shannon Entropy Secret Filter\n(Base64 >= 4.5, Hex >= 3.4 bits/char)"]:::piiSecurity
Tier3["Tier 3: Contextual ONNX NER Pipeline\n(Script-Aware CJK & Multi-Modal Unwrapping)"]:::piiSecurity
Tier1 --> Tier2 --> Tier3
end
VaultStore[("๐ AES-256-GCM Vault Store\n(Session-Scoped TTL Eviction)")]:::vault
LookaheadBuffer["โฑ๏ธ Sliding-Window Streaming Buffer\n(Chunk-Split & Slowloris Protection)"]:::proxyEngine
Rehydrator["๐ Real-Time SSE Re-hydrator\n(Synthetic Entity / Tag De-masking)"]:::proxyEngine
end
UpstreamLLM["โ๏ธ Upstream LLM Provider\n(OpenAI / Anthropic / Gemini / vLLM)"]:::upstream
%% Inbound Request Flow
UserApp -- "<b>1. Inbound Request (Raw PII / Secrets)</b>" --> InboundAuth
InboundAuth -- "<b>2. Authenticated Payload</b>" --> Tier1
Tier3 -- "<b>3. Encrypt & Store Token Mappings</b>" --> VaultStore
Tier3 -- "<b>4. Sanitized Zero-PII Payload</b>" --> UpstreamLLM
%% Outbound Response Flow
UpstreamLLM -. "<b>5. Raw SSE Stream Deltas</b>" .-> LookaheadBuffer
LookaheadBuffer -- "<b>6. Prefix-Safe Rehydration</b>" --> Rehydrator
Rehydrator <--> VaultStore
Rehydrator -. "<b>7. Sanitized Real-Time Stream</b>" .-> UserApp
style SecurityMoat fill:#f8fafc,stroke:#0284c7,stroke-width:2px,stroke-dasharray: 5 5,color:#0f172a
style CascadeEngine fill:#ffffff,stroke:#cbd5e1,stroke-width:1px,color:#263238,font-weight:bold
linkStyle default stroke:#0f172a,stroke-width:2px;
- Intercept: Your application sends a standard OpenAI / LangChain payload to
localhost:8000. - Cascade Redaction: The proxy intercepts the JSON and routes text through the 3-Tier detection cascade (Regex -> Shannon Entropy -> ONNX NER).
- Vault Storage: The original sensitive data is mapped to a deterministic tag (or synthetic entity) and stored locally in a TTL-backed session vault.
- Clean Egress: A 100% sanitized payload is forwarded to OpenAI. OpenAI never sees your raw sensitive data.
- SSE Stream Intercept: OpenAI streams the response back chunk-by-chunk via Server-Sent Events (SSE).
- Prefix-Aware Buffer: Because tokens can be split across SSE chunks, the sliding-window buffer retains trailing prefix overlap up to
L = max(0, max_token_length - 1). - Re-hydration: Once a tag or synthetic word is fully assembled, the proxy swaps the real data back from the local vault and streams the un-redacted text to the user's application in real-time.
LLM-Shield-Proxy delivers enterprise privacy and zero-trust security through highly optimized architectural breakthroughs.
View the Complete Architecture Deep Dive ๐๏ธ: For an exhaustive breakdown of the streaming lexer, memory mechanics, and service mesh integrations, please refer to the detailed architecture documentation.
Rust-backed orjson engine parses fragmented Server-Sent Events with mathematical overlap bounding, enabling high-throughput without Python GIL saturation and capping memory at <60MB.
All identifiers and custom dictionaries are pre-compiled into Deterministic Finite Automatons (DFAs) in C++, guaranteeing linear execution time to physically immunize the proxy against Regex Denial of Service (ReDoS).
Vectorized O(N) math loop evaluating H(S) bit density to instantly intercept unstructured 64-char cryptographic keys and substitute them with Faker-based synthetic equivalents in <6 ยตs.
Zero-Data proxying via stateless AES-256-GCM envelope encryption (data encrypted inside the LLM prompt) or ephemeral Redis TTL vaults with Deterministic HMAC masking.
Deployed natively as a Kubernetes sidecar microservice, integrating directly into Envoy Proxy's envoy.ext_proc. Buffer chunks stream directly over Unix Domain Sockets (UDS) via gRPC, ensuring zero HTTP network hops.
"Zero-SDK" translation layer dynamically maps standard OpenAI schema structures into Anthropic Claude schemas at the network edge, avoiding downstream application code rewrites.
Isolates CJK ideographs from Latin alphabets to prevent catastrophic sub-word collisions when streaming unspaced logographic languages (Chinese, Japanese, or Korean text).
Neutralizes invisible Unicode characters, BiDi overrides, and NFKC exploits. Traversal of nested payloads and tool_calls is hard-capped against stack-overflow JSON bombs.
Actively tracks autonomous LLM tool_calls array depths to halt runaway AutoGen/CrewAI loops and enforces Redis evalsha token-bucket rate limits (6000 RPM / 200 Burst).
LLM-Shield-Proxy is validated against an exhaustive suite of 127 automated unit, integration, and adversarial fuzzing tests.
Below is a high-level summary of our defense architecture. For the complete 18-vector Threat Matrix, detailed implementation specifications, and vulnerability coverage, view our Deep Dive Security & Threat Model Documentation.
LLM-Shield-Proxy is engineered specifically to help enterprises utilize Generative AI without violating data privacy regulations like HIPAA or failing SOC 2 audits.
Below is a summary of our compliance mappings. For the exhaustive deep-dive mapping, view our Enterprise Compliance Documentation.
If you are deploying LLM-Shield to satisfy a compliance audit, map the proxy's features directly to your Trust Services Criteria. See our complete Auditor Evidence Mapping. Includes documentation for the Universal Decision Trace Exporter and Kubernetes-Native GRC Dispatcher.
| Compliance Domain | Supported Features & Capabilities |
|---|---|
| ๐ฅ HIPAA Transmission Security | Local O(1) Redaction, Tier-2 Shannon Entropy + Faker synthetic substituting. No raw PHI traverses public internet to third-party APIs. |
| ๐ก๏ธ SOC 2 Audit Controls | WORM-Compliant Merkle Attestation & SHA-256 Hash Chaining. Emits tamper-evident structured logs with strict RFC 6902 differential patching. |
| โ๏ธ Legal & Egress Provenance | Cryptographic Proof of Non-Egress Merkle Attestation. Dynamic Canary Watermarking for insider leak forensics. |
| ๐ Data Integrity & Storage | Zero long-term storage. In-Band Stateless AES-256-GCM masking or ephemeral Redis TTL Vault mapping with Deterministic HMAC masking. |
Based on extreme stress testing, the Proxy scales highly efficiently across multi-core architectures. The proxy engine is fully asynchronous and achieves its highest throughput on Linux environments utilizing epoll.
- Rule of Thumb: Provision 1 CPU core for every 1,800 expected peak concurrent users.
- Mid-Tier (16 Cores): ~28,800 Concurrent Users. (Recommended: AWS c6i.4xlarge, GCP c2-standard-16, or Azure Standard_F16s_v2)
- High-Tier (32 Cores): ~57,600 Concurrent Users. (Recommended: AWS c6i.8xlarge, GCP c2-standard-32, or Azure Standard_F32s_v2)
- Memory (RAM) Footprint: The proxy is strictly CPU-bound. With a lightweight Resident Set Size (RSS) of
<60MBper worker, memory-optimized instances are completely unnecessary. Standard compute-optimized instances provide vastly more RAM than the proxy will ever consume.
Note
Windows Deployment Note (SO_REUSEPORT): While the proxy runs efficiently on Windows, scaling to extreme high-concurrency with multiple workers is constrained by the Windows TCP stack. Windows does not natively support the SO_REUSEPORT socket option. Under massive load, this can result in less efficient connection routing across Uvicorn workers. For maximum enterprise production scale, Linux deployments are generally recommended. In rigorous load tests, a single Python core on Windows tops out around ~800 to 900 concurrent streaming users before encountering accept() backlog saturation (ConnectionRefusedError).
LLM-Shield-Proxy is engineered for sub-millisecond overhead and ultra-lightweight resource usage. Numbers from the automated benchmark suite (python benchmark.py):
=================================================================
LLM-Shield-Proxy Enterprise Latency & Proof Benchmark
=================================================================
1. ISOLATED SHANNON ENTROPY SECRET SCANNER (<6 ยตs Proof):
-----------------------------------------------------------------
โข Mean Latency: 2.60 ยตs
โข Median (p50): 2.60 ยตs
โข 95th Percentile:2.70 ยตs
โข 99th Percentile:3.30 ยตs
[VERIFIED] Shannon Entropy executes in <6 ยตs: True
2. MASSIVE PAYLOAD REDACTION (10,000 Words / 50 Adversarial Secrets):
-----------------------------------------------------------------
โข Mean Latency: 25.96 ms
โข Median (p50): 25.80 ms
โข 95th Percentile:26.73 ms
โข 99th Percentile:32.08 ms
3. RESIDENT MEMORY BASELINE:
-----------------------------------------------------------------
โข Active RSS Footprint: 55.31 MB (<60 MB Target: True)
=================================================================
ALL AUDIT BENCHMARKS COMPLETED AND VERIFIED
=================================================================
| Metric | Average Latency | Median Latency | Footprint / Notes |
|---|---|---|---|
| Tier 1 Regex Overhead | 0.0379 ms |
0.0366 ms (36.60 ยตs) |
Microsecond pattern scan |
| Tier 2 (Shannon Entropy) Overhead | 0.0026 ms |
0.0026 ms (2.60 ยตs) |
Math-bound loop execution |
| Tier 3 (ONNX NER) Overhead | ~12.50 ms |
~11.80 ms |
Inference on 50-token chunk (Optional NLP Mode) |
| Total SSE Stream Overhead | 0.0043 ms |
0.0042 ms (4.23 ยตs) |
Added latency per SSE delta chunk |
| AES-256-GCM Encrypt + Decrypt | 0.0017 ms |
0.0017 ms (1.76 ยตs) |
Authenticated vault cipher cycle |
| Process RAM Footprint | - | - | <60 MB Resident Set Size (55.31 MB verified) |
To achieve microsecond latencies, LLM-Shield-Proxy bypasses heavy legacy NLP frameworks in favor of aggressive low-level algorithmic optimizations:
- O(N) Vectorized Shannon Entropy: Tier 2 evaluates raw unformatted secrets (API keys, Hex) using a highly optimized frequency
Counterand math-bound loop, avoiding heavy regex backtracking. It executes in<6 ยตs. - DFA Pre-compiled Regex Caching: Tier 1 identifiers are compiled into deterministic finite automatons (DFA) at startup, ensuring constant-time structural matching.
- Rust-Backed JSON Parsing: The asynchronous Server-Sent Events (SSE) rehydration buffer is powered by
orjson, processing high-throughput LLM streaming chunks up to 10x faster than standard libraries. - Lazy-Loaded ONNX Neural Pipeline: The Tier 3 Named Entity Recognition (NER) pipeline is strictly lazy-loaded. If disabled, it gracefully bypasses neural inference with zero startup overhead or memory bloat.
- Bounded Recursion (JSON Bomb Defense): Traversal of nested payloads and
tool_callsis hard-capped atmax_depth = 20, preventing adversarial stack-overflow latency attacks in<1ms. - Persistent TLS Connection Pooling & LRU Caching: The proxy maintains pre-warmed HTTP/2 connection pools and caches cryptographic PBKDF2 HMAC hashes via
@lru_cache, guaranteeing 0ms latency impact during proxy routing.
Engineered on an asynchronous, non-blocking event loop with HTTP/2 persistent connection pooling, LLM-Shield-Proxy scales effortlessly under high enterprise load:
- Concurrent Streaming Capacity: Verified stable under 1,800+ simultaneous persistent SSE streams per container worker (core) with zero packet desynchronization.
- Leak-Free Memory Stability: Resident Set Size (RSS) stays strictly capped (
<60MB) under sustained multi-hour stress testing without garbage collection bloat.
To run the automated benchmark and stress test suites locally:
# Automated latency & unit benchmarks
python benchmark.py
# Locust concurrent stream stress suite
locust -f load_test.py --headless -u 500 -r 50 --run-time 10m --host http://localhost:8000Building a microsecond-latency reverse proxy requires low-level architectural optimizations:
-
Custom SSE Sliding-Window vs. Off-the-Shelf Parsers: Standard HTTP/SSE libraries buffer data line-by-line, which fails when sensitive entities (such as SSNs) are fragmented across consecutive
data:delta chunks. LLM-Shield-Proxy implements a custom async generator buffer retaining prefix overlap (L = max_token_length - 1), guaranteeing 100% interception of fragmented packets without stream stalling. -
ONNX Runtime vs. PyTorch: Heavy ML frameworks like PyTorch or spaCy consume 1GB+ of RAM and incur significant initialization latency. By quantizing the Tier 3 BERT-NER model and executing via C++ ONNX Runtime, the proxy maintains a
<60MBfootprint and starts instantly. -
Rust-Backed
orjsonvs. Standardjson: Standardjsonparsing introduces CPU overhead during high-concurrency streaming.orjsonexecutes deserialization in native code without GIL contention, delivering up to 10x faster parsing on large payloads. -
Information Theory (Shannon Entropy) vs. Brute-Force Regex: Massive regex dictionaries degrade performance through backtracking and miss unstructured credentials. The proxy couples structural regex with an
$O(N)$ Shannon Entropy scanner, isolating high-density cryptographic secrets in<6 ยตs.
Please be aware of the following current limitations:
- Text Only: The proxy does not currently scan or redact text embedded inside base64 image payloads (e.g., OpenAI Vision models).
- Supported Languages: Multilingual support requires providing your own ONNX model via BYOM (
ONNX_MODEL_PATH). By default, the proxy falls back to an English-optimized NLP model. - Non-Standard Streaming: Designed for standard Server-Sent Events (SSE). Custom or proprietary streaming protocols may bypass the sliding-window buffer.
Run the full automated test suite using pytest:
# Run all unit and integration tests
py -m pytest -v
# Run specific modules
py -m pytest tests/test_streaming.py -v
py -m pytest tests/test_pii_engine.py -v
py -m pytest tests/test_security_hardening.py -vDesigned for zero-friction adoption by DevOps, Site Reliability Engineers (SREs), and Network Administrators:
Built-in liveness, readiness, and metrics endpoints explicitly support enterprise orchestrators:
- Kubernetes / Swarm Probes: Requests to
/healthzand/livezreturn an immediateHTTP 200 OKliveness probe. Requests to/readyzverify Redis connectivity and proxy health. - Prometheus Metrics: Native instrumentation at
/metricswith optional Bearer token authentication. - Frontend / Browser Integration: Native support for CORS
OPTIONSpreflight requests, returning standard CORS headers andHTTP 204 No Contentto unblock secure frontend applications without triggering auth failures.
$ curl -X GET "http://localhost:8000/health"
# Output: {"status":"ok","service":"llm-shield-proxy","version":"1.2.14"}
$ curl -X GET "http://localhost:8000/readyz"
# Output: {"status":"ready","service":"llm-shield-proxy","version":"1.2.14","redis_connected":false}
curl -X OPTIONS http://localhost:8000/v1/chat/completions
# Returns 204 No Content with Access-Control-Allow-* headers100% compliant with 12-factor app standards. All upstream target routing, keys, thresholds, and pool sizes are managed via validated pydantic-settings:
| Environment Variable | Type | Default | Description |
|---|---|---|---|
HOST |
str |
0.0.0.0 |
Socket host to bind |
PORT |
int |
8000 |
Socket port to bind |
UPSTREAM_BASE_URL |
str |
https://api.openai.com |
Target upstream LLM provider base URL |
OPENAI_API_KEY |
str |
None |
Centralized enterprise OpenAI API key |
REDIS_URL |
str |
None |
Redis connection URL for distributed vault state |
TELEMETRY_ENABLED |
bool |
False |
Enable OpenTelemetry tracing and audit logging to OTLP collector |
Note: For a full list of all configuration flags and advanced feature toggles, refer to the Deployment Guide.
LLM-Shield-Proxy runs completely stateless by default. For high-volume enterprise deployments, instances scale horizontally behind edge proxies (NGINX, Traefik, AWS ALB):
# Spin up 5 load-balanced instances of the proxy
docker compose up -d --scale llm-shield-proxy=5When configured with REDIS_URL, session vaults are shared across all proxy replicas via redis.asyncio, ensuring seamless session isolation across multi-instance clusters.
Every published release includes automated SHA-256 checksums (checksums.txt) and GPG detached signatures (checksums.txt.asc) signed by maintainer Ninad Phalak. You can verify checksums and cryptographic authenticity before deployment using:
# 1. Verify SHA-256 Checksums (Linux / macOS):
sha256sum -c checksums.txt
# On Windows (PowerShell):
Get-FileHash llm-shield-proxy-source-v1.2.14.zip -Algorithm SHA256
# 2. Verify Cryptographic GPG Signature:
gpg --verify checksums.txt.asc checksums.txtAbstracts configuration fatigue away from the global environment variables by mounting a policies.yaml file to dynamically map virtual_key_id client identities to distinct security roles. The engine supports zero-downtime hot-reloading updates for live, enterprise-grade RBAC without dropping active proxy streams.
- Universal Dynamic Overrides: Allows per-tenant contextual isolation of any proxy setting (e.g., rate limits, strictness, timeouts) seamlessly natively via policies.yaml.
I am committed to maintaining LLM-Shield-Proxy as the fastest ultra-low latency redaction engine for LLMs. I am actively looking for open-source contributors and collaborators to help execute the following technical roadmap. If you submit a PR, I will personally review and merge your architecture contributions:
- Cythonize the Sliding-Window Buffer: Compile the pure-Python async generator (
streaming.py) into a C-extension binary to aggressively drive down tail latencies for high-throughput enterprise deployments.
If you want to contribute to enterprise AI security, check out CONTRIBUTING.md and claim an issue (e.g., Help Cythonize the proxy! #15)!
If your organization is evaluating, benchmarking, or deploying LLM-Shield-Proxy to unblock LLM streaming and meet strict compliance requirements (like SOC 2/HIPAA), I encourage you to engage with the community:
- Architecture Discussions: Open a GitHub Discussion to share your feedback on high-throughput deployments, custom proxy pipelines, or benchmark results.
- Enterprise Case Studies: If your startup or enterprise is using the proxy in production, let me know! I highlight production architectures and feature enterprise teams in my community benchmarks.
- Bug Reports & Features: Submit technical issues or feature requests via the GitHub Issue tracker.
LLM-Shield-Proxy is actively gathering feedback from CISOs, DevOps engineers, and Cybersecurity professionals to shape the open-source compliance roadmap.
- ARCHITECTURE.md - Engine & Data Plane
- Format-Preserving Synthetic Masking & Entropy (Shannon entropy with faker Tier 2)
- In-Band Stateless Cryptographic Masking
- Multi-Provider Translators (e.g. Zero-SDK OpenAI-to-Anthropic request transformation and SSE stream normalization)
- Anthropic Adapter Implementation
- Zero-Allocation Streaming JSON Lexer
- Provider Failover Routing (Explicit header-driven rerouting to secondary mirrors without model downgrades)
- Antifragile Exponential Retries (Native asyncio jitter catching network timeouts and 429/50x errors)
- POLICIES.md - Role-Based Policy-as-Code (RBAC)
- SECURITY.md - Threat Model & Defenses
- Composite Agent Loop Circuit Breaker
- Stateless Redis TTL Vault & Deterministic HMAC Masking
- Granular Entity Policy Scopes (O(1) in-memory tenant profile mapping)
- Centralized Enterprise Secrets & mTLS (Native HashiCorp Vault)
- Cryptographic Canary Prompt Tripwires (Inbound honeytokens and outbound Generator Exit socket drops)
- Entity-Weighted Blast Radius Limits (Redis Token-Bucket circuit breakers for bulk data exfiltration)
- COMPLIANCE.md - Audit, Forensics & Legal
- Cryptographic SHA-256 Hash Chaining
- Dynamic Canary Watermarking & Steganography (Leak Forensics)
- Cryptographic Proof of Non-Egress Merkle Attestation
- WORM-Compliant Merkle Attestation & Audit Logging
- FIPS 140-3 KAT, RFC 6902 Differential Audit Logging
- LLM FinOps Chargeback Meter (Asynchronous Prometheus metrics for multi-tenant chargebacks)
- Universal Decision Trace Exporter (NIST OSCAL artifacts and OpenTelemetry spans)
- DEPLOYMENT.md - Infrastructure & Resiliency
- Service Mesh Native Interface
- Zero-Overhead OpenTelemetry Tracing (W3C traceparent propagation via background thread)
- Service Mesh Native gRPC ext_proc Integration (Zero HTTP network hops)
- Traffic Engineering & Resiliency (Redis evalsha Token-Bucket Rate Limiter, Kubernetes 25s SIGTERM draining)
- Zero-Dependency Kubernetes Mutating Webhook
- Deep Component Health Probes and Prometheus Alert Rules
LLM-Shield-Proxy is an original engineering work authored and maintained by Ninad Phalak.
- Open-Source License: The core engine, proxy middleware, and streaming buffers are licensed under the Apache 2.0 License (see LICENSE for details).
- Patent Status: Core architectural mechanismsโspecifically including the asynchronous Server-Sent Event (SSE) sliding-window lookahead buffer and the memory-bounded two-tier inference routing cascadeโare protected under U.S. Patent Pending status (App. No. 64/126,730).
If you reference this architecture, benchmark methodology, or sliding-window buffer implementation, please cite:
Phalak, N. (2026). Quantifying Latency and Token Overhead in Real-Time LLM Stream Sanitization: A Tiered Detection Approach (Version 1.0.0). Zenodo. https://doi.org/10.5281/zenodo.21955770
@misc{phalak2026quantifying,
author = {Phalak, Ninad},
title = {Quantifying Latency and Token Overhead in Real-Time LLM Stream Sanitization: A Tiered Detection Approach},
month = aug,
year = 2026,
publisher = {Zenodo},
doi = {10.5281/zenodo.21955770},
url = {https://doi.org/10.5281/zenodo.21955770}
}
