Skip to content

Commit 82f8f68

Browse files
Merge pull request #3 from lockllm/compression
Compression Featuree
2 parents d0f42af + af68aca commit 82f8f68

File tree

10 files changed

+475
-25
lines changed

10 files changed

+475
-25
lines changed

CHANGELOG.md

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,83 @@
11
# Changelog
22

3+
## [1.3.0] - 2026-02-27
4+
5+
### Added
6+
7+
#### Prompt Compression
8+
Reduce token usage and costs by compressing prompts before sending them to AI providers. Three compression methods are available:
9+
10+
- **`toon`** (Free) - Converts JSON data to a compact notation format, achieving 30-60% token savings on structured data. Only activates when the prompt starts with `{` or `[` (pure JSON). Non-JSON input is returned unchanged.
11+
- **`compact`** ($0.0001/use) - Advanced compression that intelligently reduces prompt length while preserving meaning. Works on any text type. Supports configurable compression rate (0.3-0.7, default 0.5).
12+
- **`combined`** ($0.0001/use) - Applies TOON first, then runs Compact on the result for maximum token reduction. For non-JSON input, behaves identically to `compact`. Best when you want maximum compression.
13+
14+
Prompt compression is opt-in and disabled by default. Security scanning always runs on the original text before compression is applied.
15+
16+
**Proxy mode:**
17+
```typescript
18+
// TOON - compress structured JSON prompts (free)
19+
const openai = createOpenAI({
20+
apiKey: process.env.LOCKLLM_API_KEY,
21+
proxyOptions: {
22+
compressionAction: 'toon'
23+
}
24+
});
25+
26+
// Compact - compress any text with configurable rate
27+
const openai2 = createOpenAI({
28+
apiKey: process.env.LOCKLLM_API_KEY,
29+
proxyOptions: {
30+
compressionAction: 'compact',
31+
compressionRate: 0.4 // Lower = more aggressive compression (0.3-0.7, default: 0.5)
32+
}
33+
});
34+
35+
// Combined - TOON then Compact for maximum compression
36+
const openai3 = createOpenAI({
37+
apiKey: process.env.LOCKLLM_API_KEY,
38+
proxyOptions: {
39+
compressionAction: 'combined',
40+
compressionRate: 0.5
41+
}
42+
});
43+
```
44+
45+
**Scan API:**
46+
```typescript
47+
const result = await lockllm.scan(
48+
{ input: '{"users": [{"name": "Alice"}, {"name": "Bob"}]}' },
49+
{ compressionAction: 'combined', compressionRate: 0.5 }
50+
);
51+
52+
if (result.compression_result) {
53+
console.log(result.compression_result.method); // 'combined'
54+
console.log(result.compression_result.compressed_input); // Compressed text
55+
console.log(result.compression_result.compression_ratio); // e.g., 0.35
56+
}
57+
```
58+
59+
#### Compression Response Metadata
60+
Proxy responses now include compression metadata in response headers:
61+
- `X-LockLLM-Compression-Method` - Compression method used (`toon`, `compact`, or `combined`)
62+
- `X-LockLLM-Compression-Applied` - Whether compression was applied (`true` or `false`)
63+
- `X-LockLLM-Compression-Ratio` - Ratio of compressed to original length (lower = better)
64+
65+
Parse these with `parseProxyMetadata()`:
66+
```typescript
67+
const metadata = parseProxyMetadata(response.headers);
68+
console.log(metadata.compression);
69+
// { method: 'combined', applied: true, ratio: 0.35 }
70+
```
71+
72+
### Notes
73+
- Prompt compression is opt-in. Existing integrations continue to work without changes.
74+
- All new types (`CompressionAction`, `CompressionResult`) are fully exported for TypeScript users.
75+
- Security scanning always runs on the original (uncompressed) text for maximum protection.
76+
- TOON compression is free. Compact and Combined cost $0.0001 per request.
77+
- Compression results are cached for 30 minutes to avoid redundant processing.
78+
79+
---
80+
381
## [1.2.0] - 2026-02-21
482

583
### Added
@@ -134,14 +212,14 @@ const openai = createOpenAI({
134212
scanMode: 'combined',
135213
scanAction: 'block',
136214
policyAction: 'block',
137-
routeAction: 'auto', // Enable intelligent routing
215+
routeAction: 'auto', // Enable smart routing
138216
cacheResponse: true, // Enable response caching
139217
cacheTTL: 3600 // Cache for 1 hour
140218
}
141219
});
142220
```
143221

144-
#### Intelligent Routing
222+
#### Smart Routing
145223
Let LockLLM automatically select the best model for each request based on task type and complexity. Set `routeAction: 'auto'` to enable, or `routeAction: 'custom'` to use your own routing rules from the dashboard.
146224

147225
#### Response Caching

README.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ LockLLM provides production-ready AI security that integrates seamlessly into yo
8181
| **Custom Endpoints** | Configure custom URLs for any provider (self-hosted, Azure, private clouds) |
8282
| **Custom Content Policies** | Define your own content rules in the dashboard and enforce them automatically across all providers |
8383
| **AI Abuse Detection** | Detect bot-generated content, repetition attacks, and resource exhaustion from your end-users |
84-
| **Intelligent Routing** | Automatically select the optimal model for each request based on task type and complexity to save costs |
84+
| **Smart Routing** | Automatically select the optimal model for each request based on task type and complexity to save costs |
8585
| **PII Detection & Redaction** | Detect and automatically redact emails, phone numbers, SSNs, credit cards, and other personal information before they reach AI providers |
86+
| **Prompt Compression** | Reduce token usage with TOON (JSON-to-compact-notation, free), Compact (advanced compression, $0.0001/use), or Combined (TOON then Compact for maximum reduction, $0.0001/use) methods |
8687
| **Response Caching** | Cache identical LLM responses to reduce costs and latency on repeated queries |
8788
| **Enterprise Privacy** | Provider keys encrypted at rest, prompts never stored |
8889
| **Production Ready** | Battle-tested with automatic retries, timeouts, and error handling |
@@ -625,6 +626,8 @@ interface ScanOptions {
625626
policyAction?: 'block' | 'allow_with_warning'; // Custom policy behavior
626627
abuseAction?: 'block' | 'allow_with_warning'; // Abuse detection (opt-in)
627628
piiAction?: 'strip' | 'block' | 'allow_with_warning'; // PII detection (opt-in)
629+
compressionAction?: 'toon' | 'compact' | 'combined'; // Prompt compression (opt-in)
630+
compressionRate?: number; // Compact/combined compression rate (0.3-0.7)
628631
}
629632
```
630633

@@ -657,10 +660,20 @@ interface ScanResponse {
657660
scan_warning?: ScanWarning;
658661
// Present when abuse detection is enabled and abuse found
659662
abuse_warnings?: AbuseWarning;
660-
// Present when intelligent routing is enabled
663+
// Present when smart routing is enabled
661664
routing?: { task_type: string; complexity: number; selected_model?: string; };
662665
// Present when PII detection is enabled
663666
pii_result?: PIIResult;
667+
// Present when prompt compression is enabled
668+
compression_result?: CompressionResult;
669+
}
670+
671+
interface CompressionResult {
672+
method: 'toon' | 'compact' | 'combined'; // Compression method used
673+
compressed_input: string; // The compressed text
674+
original_length: number; // Original text length
675+
compressed_length: number; // Compressed text length
676+
compression_ratio: number; // Ratio (compressed/original, lower = better)
664677
}
665678

666679
interface PIIResult {
@@ -694,6 +707,8 @@ interface GenericClientConfig {
694707
policyAction?: 'block' | 'allow_with_warning';
695708
abuseAction?: 'block' | 'allow_with_warning' | null;
696709
piiAction?: 'strip' | 'block' | 'allow_with_warning' | null;
710+
compressionAction?: 'toon' | 'compact' | 'combined' | null;
711+
compressionRate?: number;
697712
routeAction?: 'disabled' | 'auto' | 'custom';
698713
sensitivity?: 'low' | 'medium' | 'high';
699714
cacheResponse?: boolean;
@@ -747,9 +762,10 @@ const headers = buildLockLLMHeaders({
747762
policyAction: 'allow_with_warning',
748763
abuseAction: 'block',
749764
piiAction: 'strip',
765+
compressionAction: 'toon',
750766
routeAction: 'auto'
751767
});
752-
// Returns: { 'x-lockllm-scan-mode': 'combined', 'x-lockllm-pii-action': 'strip', ... }
768+
// Returns: { 'x-lockllm-scan-mode': 'combined', 'x-lockllm-compression': 'toon', ... }
753769
```
754770

755771
**Parse proxy response metadata:**
@@ -764,6 +780,7 @@ console.log(metadata.scan_mode); // 'combined'
764780
console.log(metadata.cache_status); // 'HIT' or 'MISS'
765781
console.log(metadata.routing); // { task_type, complexity, selected_model, ... }
766782
console.log(metadata.pii_detected); // { detected, entity_types, entity_count, action }
783+
console.log(metadata.compression); // { method, applied, ratio }
767784
```
768785

769786
## Error Types
@@ -872,16 +889,16 @@ LockLLM uses a 10-tier progressive system based on monthly usage. Higher tiers u
872889

873890
| Tier | Max RPM | Monthly Spending Requirement |
874891
|------|---------|----------------------------|
875-
| **Tier 1** (Free) | 30 RPM | $0 |
876-
| **Tier 2** | 50 RPM | $10/month |
877-
| **Tier 3** | 100 RPM | $50/month |
878-
| **Tier 4** | 200 RPM | $100/month |
879-
| **Tier 5** | 500 RPM | $250/month |
880-
| **Tier 6** | 1,000 RPM | $500/month |
881-
| **Tier 7** | 2,000 RPM | $1,000/month |
882-
| **Tier 8** | 5,000 RPM | $3,000/month |
883-
| **Tier 9** | 10,000 RPM | $5,000/month |
884-
| **Tier 10** | 20,000 RPM | $10,000/month |
892+
| **Tier 1** (Free) | 300 RPM | $0 |
893+
| **Tier 2** | 500 RPM | $10/month |
894+
| **Tier 3** | 1,000 RPM | $50/month |
895+
| **Tier 4** | 2,000 RPM | $100/month |
896+
| **Tier 5** | 5,000 RPM | $250/month |
897+
| **Tier 6** | 10,000 RPM | $500/month |
898+
| **Tier 7** | 20,000 RPM | $1,000/month |
899+
| **Tier 8** | 50,000 RPM | $3,000/month |
900+
| **Tier 9** | 100,000 RPM | $5,000/month |
901+
| **Tier 10** | 200,000 RPM | $10,000/month |
885902

886903
See [pricing](https://www.lockllm.com/pricing) for full tier details and free monthly credits.
887904

@@ -938,7 +955,8 @@ const result = await lockllm.scan(
938955
scanAction: 'block', // Block core injection attacks
939956
policyAction: 'allow_with_warning', // Allow but warn on policy violations
940957
abuseAction: 'block', // Enable abuse detection (opt-in)
941-
piiAction: 'strip' // Redact PII from input (opt-in)
958+
piiAction: 'strip', // Redact PII from input (opt-in)
959+
compressionAction: 'combined' // Compress prompts (opt-in: 'toon' | 'compact' | 'combined')
942960
}
943961
);
944962

@@ -951,7 +969,9 @@ const openai = createOpenAI({
951969
policyAction: 'block', // Block policy violations
952970
abuseAction: 'allow_with_warning', // Detect abuse, don't block
953971
piiAction: 'strip', // Automatically redact PII
954-
routeAction: 'auto' // Enable intelligent routing
972+
compressionAction: 'compact', // Compress prompts (free: 'toon', paid: 'compact' | 'combined')
973+
compressionRate: 0.5, // Compression rate 0.3-0.7 (compact/combined only)
974+
routeAction: 'auto' // Enable smart routing
955975
}
956976
});
957977
```
@@ -971,14 +991,17 @@ const openai = createOpenAI({
971991
- `policyAction` - Controls custom policy violations: `'block'` | `'allow_with_warning'`
972992
- `abuseAction` - Controls abuse detection (opt-in): `'block'` | `'allow_with_warning'` | `null`
973993
- `piiAction` - Controls PII detection (opt-in): `'strip'` | `'block'` | `'allow_with_warning'` | `null`
974-
- `routeAction` - Controls intelligent routing: `'disabled'` | `'auto'` | `'custom'`
994+
- `compressionAction` - Controls prompt compression (opt-in): `'toon'` | `'compact'` | `'combined'` | `null`
995+
- `compressionRate` - Compression rate for compact/combined method: `0.3` - `0.7` (default: `0.5`)
996+
- `routeAction` - Controls smart routing: `'disabled'` | `'auto'` | `'custom'`
975997

976998
**Default Behavior (no headers):**
977999
- Scan Mode: `combined` (check both core + policies)
9781000
- Scan Action: `allow_with_warning` (detect but don't block)
9791001
- Policy Action: `allow_with_warning` (detect but don't block)
9801002
- Abuse Action: `null` (disabled, opt-in only)
9811003
- PII Action: `null` (disabled, opt-in only)
1004+
- Compression Action: `null` (disabled, opt-in only)
9821005
- Route Action: `disabled` (no routing)
9831006

9841007
See [examples/advanced-options.ts](examples/advanced-options.ts) for complete examples.

examples/advanced-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ async function proxyWithAdvancedOptions() {
8787
scanAction: 'block', // Block injection attacks
8888
policyAction: 'block', // Block policy violations
8989
abuseAction: 'allow_with_warning', // Detect abuse but don't block
90-
routeAction: 'auto', // Enable intelligent routing
90+
routeAction: 'auto', // Enable smart routing
9191
},
9292
});
9393

@@ -139,7 +139,7 @@ async function defaultBehavior() {
139139
* - Scan Action: allow_with_warning (detect threats but don't block)
140140
* - Policy Action: allow_with_warning (detect violations but don't block)
141141
* - Abuse Action: null (abuse detection disabled, opt-in only)
142-
* - Route Action: disabled (no intelligent routing)
142+
* - Route Action: disabled (no smart routing)
143143
*/
144144

145145
try {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lockllm/sdk",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Enterprise-grade AI security SDK providing real-time protection against prompt injection, jailbreaks, and adversarial attacks. Drop-in replacement for OpenAI, Anthropic, and 17+ providers with zero code changes. Includes REST API, proxy mode, browser extension, and webhook support. Free BYOK model with unlimited scanning.",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type {
3434
ScanAction,
3535
RouteAction,
3636
PIIAction,
37+
CompressionAction,
3738
ProxyRequestOptions,
3839
ProxyResponseMetadata,
3940
} from './types/common';
@@ -47,6 +48,7 @@ export type {
4748
ScanWarning,
4849
AbuseWarning,
4950
PIIResult,
51+
CompressionResult,
5052
} from './types/scan';
5153

5254
export type {

src/scan.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ export class ScanClient {
9494
headers['x-lockllm-pii-action'] = options.piiAction;
9595
}
9696

97+
// Compression action: opt-in prompt compression (null/undefined means disabled)
98+
if (options?.compressionAction !== undefined && options?.compressionAction !== null) {
99+
headers['x-lockllm-compression'] = options.compressionAction;
100+
}
101+
102+
// Compression rate for compact method
103+
if (options?.compressionRate !== undefined) {
104+
headers['x-lockllm-compression-rate'] = String(options.compressionRate);
105+
}
106+
97107
// Build request body
98108
const body: Record<string, any> = {
99109
input: request.input,

src/types/common.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export type RouteAction = 'disabled' | 'auto' | 'custom';
6464
/** PII detection action (opt-in) */
6565
export type PIIAction = 'strip' | 'block' | 'allow_with_warning';
6666

67+
/** Prompt compression method (opt-in) */
68+
export type CompressionAction = 'toon' | 'compact' | 'combined';
69+
6770
/** Proxy request options with advanced headers */
6871
export interface ProxyRequestOptions extends RequestOptions {
6972
/** Scan mode (default: combined) - Check both core security and custom policies */
@@ -74,7 +77,7 @@ export interface ProxyRequestOptions extends RequestOptions {
7477
policyAction?: ScanAction;
7578
/** Abuse detection action (opt-in, default: null) - When null, abuse detection is disabled */
7679
abuseAction?: ScanAction | null;
77-
/** Routing action (default: disabled) - No intelligent routing unless explicitly enabled */
80+
/** Routing action (default: disabled) - No smart routing unless explicitly enabled */
7881
routeAction?: RouteAction;
7982
/** PII detection action (opt-in, default: null) - When null, PII detection is disabled */
8083
piiAction?: PIIAction | null;
@@ -84,6 +87,12 @@ export interface ProxyRequestOptions extends RequestOptions {
8487
cacheResponse?: boolean;
8588
/** Cache TTL in seconds (default: 3600) */
8689
cacheTTL?: number;
90+
/** Prompt compression method (opt-in, default: null) - When null, compression is disabled.
91+
* "toon" converts JSON to compact notation (free). "compact" uses advanced compression ($0.0001/use).
92+
* "combined" applies TOON first then Compact for maximum compression ($0.0001/use). */
93+
compressionAction?: CompressionAction | null;
94+
/** Compression rate for compact method (0.3-0.7, default: 0.5) - Lower = more compression */
95+
compressionRate?: number;
8796
}
8897

8998
/** Response metadata from proxy */
@@ -171,4 +180,10 @@ export interface ProxyResponseMetadata {
171180
policy_detail?: any;
172181
/** Decoded abuse detail (from base64 header) */
173182
abuse_detail?: any;
183+
/** Compression metadata */
184+
compression?: {
185+
method: string;
186+
applied: boolean;
187+
ratio: number;
188+
};
174189
}

src/types/scan.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import type { ScanResult } from './errors';
6-
import type { PIIAction } from './common';
6+
import type { PIIAction, CompressionAction } from './common';
77

88
export type Sensitivity = 'low' | 'medium' | 'high';
99

@@ -24,6 +24,20 @@ export interface ScanRequest {
2424
chunk?: boolean;
2525
}
2626

27+
/** Compression result */
28+
export interface CompressionResult {
29+
/** Compression method used */
30+
method: 'toon' | 'compact' | 'combined';
31+
/** Compressed text */
32+
compressed_input: string;
33+
/** Original text length */
34+
original_length: number;
35+
/** Compressed text length */
36+
compressed_length: number;
37+
/** Compression ratio (compressed/original, lower = better) */
38+
compression_ratio: number;
39+
}
40+
2741
/** PII detection result */
2842
export interface PIIResult {
2943
/** Whether PII was detected */
@@ -46,6 +60,12 @@ export interface ScanOptions {
4660
abuseAction?: ScanAction | null;
4761
/** PII detection action (opt-in, default: null) - When null, PII detection is disabled */
4862
piiAction?: PIIAction | null;
63+
/** Prompt compression method (opt-in, default: null) - When null, compression is disabled.
64+
* "toon" converts JSON to compact notation (free). "compact" uses advanced compression ($0.0001/use).
65+
* "combined" applies TOON first then Compact for maximum compression ($0.0001/use). */
66+
compressionAction?: CompressionAction | null;
67+
/** Compression rate for compact method (0.3-0.7, default: 0.5) - Lower = more compression */
68+
compressionRate?: number;
4969
/** Custom headers to include in the request */
5070
headers?: Record<string, string>;
5171
/** Request timeout in milliseconds */
@@ -159,4 +179,6 @@ export interface ScanResponse {
159179
};
160180
/** PII detection result (present when PII detection is enabled) */
161181
pii_result?: PIIResult;
182+
/** Compression result (present when compression is enabled) */
183+
compression_result?: CompressionResult;
162184
}

0 commit comments

Comments
 (0)