llm-entropy-filter 1.1.1 β†’ 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,352 +1,297 @@
1
- # llm-entropy-filter
2
-
3
- [![npm version](https://img.shields.io/npm/v/llm-entropy-filter.svg)](https://www.npmjs.com/package/llm-entropy-filter)
4
- [![license](https://img.shields.io/npm/l/llm-entropy-filter.svg)](LICENSE)
5
-
6
- Minimal, fast **entropy + intent gate** for LLM inputs.
7
-
8
- `llm-entropy-filter` is a deterministic, local middleware layer that filters high-entropy / low-signal inputs before they reach expensive LLM inference.
9
-
10
- It transforms your LLM from a generic processor into a **premium signal resource**.
11
-
12
- ---
13
-
14
- # πŸš€ Why this exists
15
-
16
- LLMs are powerful but:
17
-
18
- - Expensive per token
19
- - Latency-heavy (seconds vs milliseconds)
20
- - Vulnerable to spam, coercion, broken causality, and noise
21
-
22
- Most systems solve this with *more processing*.
23
-
24
- `llm-entropy-filter` solves it with **criterion before processing**.
25
-
26
- ---
27
-
28
- # 🧠 Architecture
29
-
30
- The system operates in two deterministic local layers:
31
-
32
- ## Layer 1 β€” Hard Triggers (Deterministic Signals)
33
-
34
- Immediate structural patterns:
35
-
36
- - Shouting (ALL CAPS)
37
- - Urgency markers
38
- - Money / % signals
39
- - Spam phrasing
40
- - Conspiracy vagueness
41
- - Broken causality structures
42
- - Repetition anomalies
43
-
44
- These are language-light, low-cost, and capture obvious noise.
45
-
46
- ## Layer 2 β€” Thematic Scoring (Signal Accumulation)
47
-
48
- If no hard block occurs, the input is evaluated by topic clusters:
49
-
50
- - Marketing spam
51
- - Conspiracy framing
52
- - Coercive tone
53
- - Pseudo-scientific structure
54
- - Relativism / truth dilution
55
- - Semantic incoherence
56
-
57
- Each topic contributes to an `entropy_score`.
58
-
59
- Final verdict:
60
-
61
- ALLOW | WARN | BLOCK
62
-
63
-
64
- Returned with:
65
-
66
- ```json
67
- {
68
- "action": "BLOCK",
69
- "entropy_score": 0.7,
70
- "flags": [...],
71
- "intention": "...",
72
- "confidence": 0.85,
73
- "rationale": "..."
74
- }
75
-
76
-
77
- No network calls. No embeddings. No remote inference.
78
-
79
- ## Rulesets
80
-
81
- This project ships with preset rule packs:
82
-
83
- - `default` (balanced)
84
- - `strict` (aggressive blocking)
85
- - `support` (fewer false positives)
86
- - `public-api` (hardened for open endpoints)
87
-
88
- Rulesets live in `rulesets/` and define:
89
- - thresholds (WARN/BLOCK)
90
- - hard triggers
91
- - topic scoring weights
92
-
93
- ## Integrations (copy/paste)
94
-
95
- This repo includes ready-to-use adapters under `integrations/`:
96
-
97
- - `integrations/express.mjs` β€” Express middleware gate (ALLOW/WARN/BLOCK)
98
- - `integrations/fastify.mjs` β€” Fastify plugin gate
99
- - `integrations/vercel-ai-sdk.mjs` β€” pre-gate wrapper for `streamText()` / `generateText()`
100
- - `integrations/langchain.mjs` β€” pre-gate + optional Runnable wrapper for LangChain
101
-
102
- These integrations do **not** change core behavior. They only call `gate()` and route based on the verdict.
103
-
104
-
105
- πŸ“¦ Installation
106
- npm i llm-entropy-filter
107
-
108
- ⚑ Quickstart
109
- import { gate } from "llm-entropy-filter";
110
-
111
- const result = gate("‘‘COMPRA YA!! Oferta limitada 90% OFF $$$");
112
-
113
- console.log(result);
114
-
115
- πŸ–₯ Demo Server
116
-
117
- The demo server wraps the local gate.
118
-
119
- Start
120
- npm run serve
121
-
122
-
123
- (Ensure your package.json includes: "serve": "node demo/server.mjs")
124
-
125
- Health
126
- curl http://127.0.0.1:3000/health
127
-
128
- Local gate
129
- curl -X POST http://127.0.0.1:3000/analyze \
130
- -H "Content-Type: application/json" \
131
- -d '{"text":"FREE iPhone!!! Click now!!!"}'
132
-
133
- Optional LLM Triad (Demo Only)
134
- export OPENAI_API_KEY="YOUR_KEY"
135
- export OPENAI_MODEL="gpt-4.1-mini"
136
-
137
- curl -X POST http://127.0.0.1:3000/triad \
138
- -H "Content-Type: application/json" \
139
- -d '{"text":"Vivimos en una simulaciΓ³n y todos lo esconden."}'
140
-
141
-
142
- If OPENAI_API_KEY is not set, /triad returns 503.
143
-
144
- ⚑ Performance (Measured)
145
-
146
- Environment:
147
- GitHub Codespaces (Linux container), Node 24.x
148
-
149
- Local Gate β€” /analyze
150
-
151
- Avg latency: 5.28 ms
152
-
153
- p50: 4 ms
154
-
155
- p99: 16 ms
156
-
157
- Throughput: ~5,118 req/sec
158
-
159
- 0 errors
160
-
161
- LLM Roundtrip β€” /triad
162
-
163
- Avg latency: 5,321 ms
164
-
165
- p50: 5,030 ms
166
-
167
- Throughput: ~0.34 req/sec
168
-
169
- 2 timeouts in 30s test
170
-
171
- Note: These represent different pipeline layers (local deterministic vs external LLM API). The architectural gain comes from avoiding unnecessary LLM calls.
172
-
173
- πŸ“‰ Economic Impact (Projection)
174
- Assumptions
175
-
176
- 300 tokens per request (150 in / 150 out)
177
-
178
- gpt-4o-mini pricing baseline
179
-
180
- 30% traffic filtered locally
181
-
182
- Effect
183
-
184
- If 1M requests are received:
185
-
186
- 300,000 requests never hit the LLM
187
-
188
- 30% token cost avoided
189
-
190
- 30% rate-limit headroom gained
191
-
192
- 30% reduction in latency pressure
193
-
194
- Savings scale linearly with volume and exponentially with higher-cost models.
195
-
196
- Formula:
197
-
198
- Savings =
199
- (Filtered_Requests / Total_Requests)
200
- Γ— Avg_Tokens_Per_Request
201
- Γ— Token_Price
202
-
203
- πŸ›‘ Stability & Hallucination Mitigation
204
-
205
- High-entropy inputs increase:
206
-
207
- Off-topic generation
208
-
209
- Reasoning drift
210
-
211
- Prompt injection exposure
212
-
213
- Token expansion loops
214
-
215
- By constraining input entropy before inference,
216
- the downstream model operates in a narrower semantic bandwidth.
217
-
218
- This improves stability without imposing moral or ideological constraints.
219
-
220
- πŸ§ͺ Dataset Benchmark
221
-
222
- Included:
223
-
224
- bench/sms_spam.csv
225
-
226
-
227
- Run:
228
-
229
- node bench/sms_spam_bench.mjs bench/sms_spam.csv
230
-
231
-
232
- Generates:
233
-
234
- Precision / recall
235
-
236
- Confusion matrix
237
-
238
- Top flags
239
-
240
- JSON + Markdown reports
241
-
242
- 🎯 Design Goals
243
-
244
- Deterministic
245
-
246
- Transparent
247
-
248
- Fast
249
-
250
- Composable
251
-
252
- Observable
253
-
254
- Economically rational
255
-
256
- πŸ—Ί Roadmap
257
-
258
- Multilingual rulesets
259
-
260
- Configurable rule packs
261
-
262
- Express / Fastify middleware exports
263
-
264
- Suggested rewrite mode
265
-
266
- Production case studies
267
-
268
- πŸ‘€ Attribution
269
-
270
- Developed and maintained by Ernesto Rosati.
271
-
272
- If this library creates value for your organization,
273
- consider collaboration or sponsorship.
274
-
275
- πŸ“œ License
276
-
277
- Apache-2.0
278
- Copyright (c) 2026 Ernesto Rosati
279
-
280
- Use cases & integrations
281
- ## βœ… Where this fits in real systems
282
-
283
- `llm-entropy-filter` is designed to sit **before** expensive inference. Common placements:
284
-
285
- ### 1) Public chat apps (startups)
286
- Use as a first-line gate to block obvious spam/coercion before the LLM:
287
- - faster UX for rejected traffic (<10ms)
288
- - reduced token spend
289
- - reduced prompt-abuse surface
290
-
291
- ### 2) Rate-limit protection
292
- Acts as a semantic pre-filter that reduces:
293
- - quota exhaustion
294
- - burst abuse
295
- - coordinated spam floods
296
-
297
- It creates headroom by rejecting high-entropy traffic locally.
298
-
299
- ### 3) RAG pipelines (pre-retrieval gate)
300
- Before retrieval:
301
- - block low-signal queries that would waste retrieval + reranking
302
- - normalize/clean input to improve recall precision
303
- - prevent adversarial queries from polluting retrieval traces
304
-
305
- ### 4) Multi-agent systems
306
- In agent loops:
307
- - prevent β€œreasoning drift” from noisy inputs
308
- - keep agents from spending cycles on incoherent or adversarial prompts
309
- - add structured telemetry for agent decisions (`flags`, `intention`, `entropy_score`)
310
-
311
- ### 5) Tooling & SDK pre-gates (LangChain / Vercel AI SDK)
312
- Drop in as a deterministic guard:
313
- - before `callLLM()`
314
- - before `streamText()`
315
- - before tool selection / agent routing
316
-
317
- The output can be used as:
318
- - a routing signal (ALLOW/WARN/BLOCK)
319
- - a logging payload for audits and dashboards
320
-
321
- β€œWhat’s missing to be production-ready”
322
- ## Production readiness checklist
323
-
324
- The core gate is stable, but β€œproduction-ready” requires:
325
-
326
- ### 1) Configurable rulesets
327
- - `default` (balanced)
328
- - `strict` (aggressive spam/coercion blocking)
329
- - `support` (customer support / fewer false positives)
330
- - `public-api` (open endpoints / hardened)
331
-
332
- ### 2) Reproducible metrics (precision / recall)
333
- Bench scripts should emit:
334
- - precision/recall/F1
335
- - confusion matrix
336
- - false-positive rate on normal conversations
337
- - top flags per dataset
338
-
339
- ### 3) Copy-paste integrations
340
- Provide ready-to-use adapters:
341
- - Express middleware
342
- - Fastify plugin
343
- - Next.js / Vercel edge wrapper
344
- - β€œpre-gate” helpers for LangChain-style pipelines
345
-
346
- ### 4) One real production example
347
- A minimal public case study:
348
- - traffic volume
349
- - % blocked
350
- - cost avoided
351
- - rate-limit incidents reduced
352
- - latency improvement for blocked traffic
1
+ llm-entropy-filter
2
+
3
+ Deterministic linguistic entropy gate for LLM inputs.
4
+
5
+ llm-entropy-filter is a lightweight, configurable middleware that evaluates text using linguistic and logical entropy signals before it reaches an LLM.
6
+
7
+ It is not an AI model.
8
+ It is a deterministic decision layer.
9
+
10
+ Why This Exists
11
+
12
+ Modern LLM systems face:
13
+
14
+ Spam floods
15
+
16
+ Phishing attempts
17
+
18
+ Fraud requests
19
+
20
+ Prompt injection
21
+
22
+ Manipulative urgency
23
+
24
+ Entropic noise
25
+
26
+ Most systems try to solve this reactively inside the model.
27
+
28
+ This project solves it before the model.
29
+
30
+ Order before generation.
31
+ Criteria before probability.
32
+
33
+ Core Principles
34
+
35
+ Deterministic (no randomness)
36
+
37
+ Linguistic + logical analysis
38
+
39
+ Configurable via rulesets
40
+
41
+ Fully reproducible scoring
42
+
43
+ No external API calls
44
+
45
+ Middleware-ready
46
+
47
+ Architecture Overview
48
+ Text
49
+ ↓
50
+ Normalization
51
+ ↓
52
+ Hard Triggers
53
+ ↓
54
+ Topic Signals
55
+ ↓
56
+ Entropy Scoring
57
+ ↓
58
+ Policy Overrides
59
+ ↓
60
+ Threshold Decision
61
+ ↓
62
+ ALLOW | WARN | BLOCK
63
+
64
+ Installation
65
+ npm install llm-entropy-filter
66
+
67
+ Quick Usage
68
+ import { gate } from "llm-entropy-filter";
69
+ import ruleset from "./rulesets/public-api.js";
70
+
71
+ const result = gate("FREE prize winner click now claim $100!!!", {
72
+ ruleset
73
+ });
74
+
75
+ console.log(result);
76
+
77
+
78
+ Example output:
79
+
80
+ {
81
+ "action": "BLOCK",
82
+ "entropy_score": 0.85,
83
+ "flags": ["spam_sales", "money_signal", "shouting"],
84
+ "intention": "marketing_spam",
85
+ "confidence": 0.85
86
+ }
87
+
88
+ Actions
89
+ Action Meaning
90
+ ALLOW Low entropy, safe to process
91
+ WARN Suspicious signals detected
92
+ BLOCK High entropy or deterministic policy match
93
+ Rulesets
94
+
95
+ Behavior is controlled entirely by rulesets.
96
+
97
+ Each ruleset defines:
98
+
99
+ thresholds
100
+
101
+ normalization
102
+
103
+ hard triggers
104
+
105
+ topic signals
106
+
107
+ policy overrides
108
+
109
+ Example:
110
+
111
+ {
112
+ "name": "public-api",
113
+ "thresholds": { "warn": 0.4, "block": 0.6 },
114
+ "policy": {
115
+ "block_flags": ["phishing_2fa_code"],
116
+ "warn_flags": ["scam_wfh"]
117
+ }
118
+ }
119
+
120
+ Threshold Logic
121
+
122
+ Decision is score-based unless overridden by policy.
123
+
124
+ if score >= block β†’ BLOCK
125
+ else if score >= warn β†’ WARN
126
+ else β†’ ALLOW
127
+
128
+ Policy Overrides (Deterministic Control Layer)
129
+
130
+ Rulesets may define deterministic overrides:
131
+
132
+ "policy": {
133
+ "block_flags": ["phishing_2fa_code"],
134
+ "warn_flags": ["fraud_payment_request"]
135
+ }
136
+
137
+
138
+ Behavior:
139
+
140
+ If any block_flag matches β†’ BLOCK (independent of score)
141
+
142
+ If any warn_flag matches β†’ at least WARN
143
+
144
+ Otherwise β†’ threshold decision
145
+
146
+ This allows:
147
+
148
+ Fraud cluster blocking
149
+
150
+ Linguistic certainty escalation
151
+
152
+ Safe tuning without lowering global thresholds
153
+
154
+ Logical Clusters
155
+
156
+ Entropy is not triggered by single words.
157
+
158
+ It increases when patterns accumulate.
159
+
160
+ Example combinations:
161
+
162
+ spam_kw_click + spam_kw_claim + spam_kw_free
163
+
164
+ urgency + fraud_payment_request
165
+
166
+ phishing_verify_threat + money_signal
167
+
168
+ Suspicion grows with structural density.
169
+
170
+ This reflects a linguistic-logical principle, not keyword matching alone.
171
+
172
+ Deterministic Stability Guarantee
173
+
174
+ For identical input + identical ruleset:
175
+
176
+ Output is deterministic
177
+
178
+ Score is reproducible
179
+
180
+ Flags are traceable
181
+
182
+ No external calls are made
183
+
184
+ This makes the gate:
185
+
186
+ Auditable
187
+
188
+ Compliance-friendly
189
+
190
+ Safe for logging
191
+
192
+ Open-Source Scope
193
+
194
+ This core version does NOT include:
195
+
196
+ Context memory
197
+
198
+ Session tracking
199
+
200
+ Behavioral anomaly detection
201
+
202
+ Identity verification
203
+
204
+ AI secondary review
205
+
206
+ Adaptive learning
207
+
208
+ Those belong to orchestration layers or enterprise systems.
209
+
210
+ This package focuses strictly on:
211
+
212
+ Linguistic entropy filtering.
213
+
214
+ Benchmarks
215
+
216
+ Run metrics:
217
+
218
+ npm run bench:metrics:public-api
219
+ npm run bench:metrics:strict
220
+
221
+
222
+ Reports include:
223
+
224
+ Accuracy
225
+
226
+ Precision / Recall
227
+
228
+ F1 score
229
+
230
+ Confusion matrix
231
+
232
+ Per-tag accuracy
233
+
234
+ Overblock / Underblock rates
235
+
236
+ The system is fully reproducible.
237
+
238
+ Tuning Strategy
239
+
240
+ Do NOT blindly lower thresholds.
241
+
242
+ Prefer:
243
+
244
+ Add deterministic block_flags
245
+
246
+ Adjust pattern weights
247
+
248
+ Improve signal density
249
+
250
+ Then adjust thresholds if necessary
251
+
252
+ This preserves precision while increasing recall safely.
253
+
254
+ Middleware Mode
255
+
256
+ Typical production flow:
257
+
258
+ User Input
259
+ ↓
260
+ Entropy Gate
261
+ ↓
262
+ ALLOW β†’ LLM
263
+ WARN β†’ Log / Rate-limit / Secondary review
264
+ BLOCK β†’ Reject
265
+
266
+ Philosophy
267
+
268
+ This project is grounded in a simple idea:
269
+
270
+ Entropy precedes manipulation.
271
+
272
+ Linguistic disorder precedes exploitation.
273
+
274
+ A gate should not think.
275
+ It should filter.
276
+
277
+ Version
278
+
279
+ Current stable: v1.2.x
280
+
281
+ Features:
282
+
283
+ Deterministic entropy scoring
284
+
285
+ Policy override system
286
+
287
+ Configurable thresholds
288
+
289
+ Hard trigger architecture
290
+
291
+ Cluster-sensitive scoring
292
+
293
+ Zero BLOCK→ALLOW leaks in benchmark dataset
294
+
295
+ License
296
+
297
+ APACHE 2.0