-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduction-ready.yaml
More file actions
641 lines (525 loc) · 19.3 KB
/
production-ready.yaml
File metadata and controls
641 lines (525 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# CrashLens Policy: Production-Ready Combined
# Comprehensive rules for production LLM cost governance
# yaml-language-server: $schema=../crashlens/config/policy-schema.json
# Estimated Savings: 40-60% overall
metadata:
name: "Production-Ready Combined Policy"
description: "Comprehensive cost governance combining critical rules from all policy templates"
version: "1.0.0"
author: "CrashLens Team"
last_updated: "2025-11-09"
estimated_savings: "40-60%"
tags:
- production
- comprehensive
- cost-governance
- best-practices
documentation: "https://crashlens.dev/docs/policies/production-ready"
# References to other policy templates
includes:
- model-overkill-detection (top critical rules)
- retry-loop-prevention (top critical rules)
- budget-protection (all rules)
- fallback-storm-detection (top critical rules)
- prompt-optimization (top critical rules)
- context-window-optimization (critical warnings)
# Configuration variables (customize per environment)
variables:
# Model overkill thresholds
simple_task_threshold: 50 # tokens
# Retry limits
max_retries_per_minute: 5
min_retry_interval_seconds: 1
max_retry_limit: 10
# Budget limits (adjust per environment)
max_cost_per_call: 0.50 # $0.50
hourly_spend_limit: 100.00 # $100/hour
daily_spend_limit: 1000.00 # $1000/day
# Fallback limits
max_fallback_chain_length: 3
# Context limits
repeated_context_threshold: 20
rules:
# ============================================================================
# CRITICAL PRIORITY (FAIL) - Must fix immediately
# ============================================================================
# === BUDGET PROTECTION (4 rules) ===
- id: single_call_too_expensive
description: "Single API call costs > $0.50"
match:
cost: "> 0.50"
action: fail
severity: critical
suggestion: |
🚨 CRITICAL: Single call cost > $0.50!
**Immediate Actions**:
1. Review token count (likely > 30K tokens)
2. Break into smaller requests
3. Use cheaper models for preprocessing
4. Check for retry aggregation errors
**Learn More**: https://crashlens.dev/docs/cost-per-call-optimization
- id: hourly_spend_threshold
description: "Hourly spend exceeds $100 (spending spike)"
match:
sum_cost_1h: "> 100"
action: fail
severity: critical
suggestion: |
🚨 Hourly spending spike: $100+!
**Immediate Actions**:
1. Check for retry storms
2. Identify top spenders
3. Enable rate limiting
4. Consider emergency circuit breaker
**Learn More**: https://crashlens.dev/docs/incident-response
- id: daily_budget_exceeded
description: "Daily spend exceeds $1000"
match:
sum_cost_24h: "> 1000"
action: fail
severity: critical
suggestion: |
🚨 Daily budget exceeded!
**Immediate Actions**:
1. Set hard budget limits in OpenAI dashboard
2. Enable progressive rate limiting
3. Switch to cheaper models
4. Review high-cost endpoints
**Learn More**: https://crashlens.dev/docs/budget-management
- id: exponential_backoff_failure
description: "5+ retry attempts in < 60 seconds (backoff broken)"
match:
retry_count: "> 5"
time_window_seconds: "< 60"
action: fail
severity: critical
suggestion: |
🚨 Exponential backoff failure!
**Immediate Actions**:
1. Implement exponential backoff with jitter
2. Add circuit breaker (5 failures = 60s cooldown)
3. Set max retry limit (3-5 attempts)
**Implementation**:
```python
from tenacity import retry, wait_exponential_jitter, stop_after_attempt
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential_jitter(initial=1, max=60)
)
def call_llm_with_retry(prompt):
return openai.ChatCompletion.create(...)
```
**Learn More**: https://crashlens.dev/docs/retry-best-practices
- id: infinite_retry_detected
description: "More than 10 retry attempts (infinite loop)"
match:
retry_count: "> 10"
action: fail
severity: critical
suggestion: |
🚨 Infinite retry loop detected!
**Immediate Actions**:
1. Set MAX_RETRIES = 3 (hard limit)
2. Implement circuit breaker
3. Add dead letter queue for persistent failures
4. Monitor retry counts in production
**Learn More**: https://crashlens.dev/docs/circuit-breaker-pattern
- id: retry_storm_detected
description: "10+ traces retrying simultaneously (API outage)"
match:
distinct_trace_count: "> 10"
all_retrying: true
time_window_seconds: "< 60"
action: fail
severity: critical
suggestion: |
🌪️ Retry storm detected!
**Immediate Actions**:
1. Implement global circuit breaker
2. Add API health checks
3. Enable graceful degradation
4. Alert ops team
**Learn More**: https://crashlens.dev/docs/retry-storms
- id: repeated_fallback_failure
description: "Same fallback pattern failing 10+ times"
match:
fallback_pattern_count: "> 10"
all_attempts_failed: true
action: fail
severity: critical
suggestion: |
🚨 Fallback strategy broken!
**Immediate Actions**:
1. Analyze failure logs for systemic errors
2. Implement fail-fast for non-retryable errors
3. Add dead letter queue
4. Enable graceful degradation
**Learn More**: https://crashlens.dev/docs/handling-systemic-failures
# === MODEL OVERKILL (3 critical rules) ===
- id: gpt4_for_simple_tasks
description: "GPT-4 used for simple tasks (< 50 tokens)"
match:
model: "gpt-4"
usage.prompt_tokens: "< 50"
action: fail
severity: high
suggestion: |
💡 Model overkill detected!
Switch to gpt-4o-mini (200x cheaper) or gpt-3.5-turbo for simple tasks.
**Potential Savings**: 90-95%
**Example Fix**:
```python
# Before: GPT-4 ($0.03/1K)
model = "gpt-4"
# After: GPT-4o-mini ($0.00015/1K) - 200x cheaper!
model = "gpt-4o-mini"
```
**Learn More**: https://crashlens.dev/docs/model-selection-guide
- id: claude_opus_overkill
description: "Claude Opus for tasks < 100 tokens"
match:
model: "claude-3-opus-20240229"
usage.total_tokens: "< 100"
action: fail
severity: high
suggestion: |
🚨 Expensive model for tiny task!
Switch to Claude Haiku (60x cheaper) or Sonnet (5x cheaper).
**Cost Comparison** (per million tokens):
- Claude Opus: $15/$75
- Claude Sonnet: $3/$15 (5x cheaper)
- Claude Haiku: $0.25/$1.25 (60x cheaper!)
**Learn More**: https://crashlens.dev/docs/claude-model-selection
- id: o1_preview_overkill
description: "O1-preview used for tasks < 100 tokens"
match:
model:
- "o1-preview"
- "o1-mini"
usage.prompt_tokens: "< 100"
action: fail
severity: critical
suggestion: |
🚨 Most expensive model for tiny task!
O1-preview is $15/$60 per million tokens (most expensive).
Switch to GPT-4o or GPT-4o-mini for 100x savings.
**When to Use O1**: Complex multi-step reasoning, PhD-level tasks
**NOT for**: Simple Q&A, classification, short responses
**Learn More**: https://openai.com/o1
# === FALLBACK ISSUES (1 critical rule) ===
- id: cascading_fallback_chain
description: "3+ models tried for same traceId"
match:
distinct_model_count: "> 3"
group_by: traceId
action: fail
severity: high
suggestion: |
⚠️ Cascading fallback chain!
Limit fallback chain to 2 models (primary + backup).
**Recommended Pattern**:
- Primary: Best quality model
- Fallback: Cheaper alternative or different provider
- No more than 2 attempts
**Learn More**: https://crashlens.dev/docs/fallback-strategy
# ============================================================================
# HIGH PRIORITY (FAIL) - Fix soon
# ============================================================================
- id: immediate_retry_loop
description: "Retries with < 1 second interval"
match:
retry_count: "> 3"
retry_interval_seconds: "< 1"
action: fail
severity: high
suggestion: |
⚠️ Immediate retry loop!
Add minimum 2-second delay between retries.
**Quick Fix**:
```python
import time
for attempt in range(3):
try:
return call_api()
except Exception as e:
if attempt < 2:
time.sleep(2 ** attempt) # 1s, 2s, 4s
else:
raise
```
**Learn More**: https://crashlens.dev/docs/retry-intervals
- id: redundant_retries_on_client_errors
description: "Retrying 4xx errors (non-retryable)"
match:
status_code:
- 400
- 401
- 403
- 404
- 422
retry_count: "> 1"
action: fail
severity: high
suggestion: |
🚫 Don't retry client errors!
**Non-Retryable**: 400, 401, 403, 404, 422
**Retryable**: 429, 500, 502, 503, 504
Fix the request instead of retrying.
**Learn More**: https://crashlens.dev/docs/error-handling
- id: cost_per_user_session
description: "Cost per session > $5"
match:
sum_cost_by_trace: "> 5"
action: warn
severity: high
suggestion: |
⚠️ Expensive user session!
**Optimization Strategies**:
1. Implement conversation summarization at 80% budget
2. Use sliding window (last 10 messages)
3. Switch to cheaper models after first 3 turns
4. Set session budget limit ($5 max)
**Learn More**: https://crashlens.dev/docs/conversation-cost-management
- id: inefficient_cost_per_token
description: "Cost per completion token > $0.01"
match:
cost_per_completion_token: "> 0.01"
action: fail
severity: high
suggestion: |
🚨 Extremely high cost per token!
Normal: $0.00006 per token (GPT-4)
Yours: > $0.01 per token (300x+ normal!)
**Check for**:
1. Retry loops (multiple attempts aggregated)
2. Long fallback chains
3. Model overkill (O1-preview)
4. Cost calculation bugs
**Learn More**: https://crashlens.dev/docs/cost-per-token-analysis
# ============================================================================
# MEDIUM PRIORITY (WARN) - Optimize when possible
# ============================================================================
- id: gpt4_for_short_completions
description: "GPT-4 generating < 10 completion tokens"
match:
model: "gpt-4"
usage.completion_tokens: "< 10"
action: warn
severity: medium
suggestion: |
⚠️ Short completion on expensive model
Simple responses rarely need GPT-4. Consider gpt-4o-mini or gpt-3.5-turbo.
**Savings**: 90%+ for yes/no answers, classifications, short summaries
**Learn More**: https://crashlens.dev/docs/when-to-use-gpt4
- id: expensive_model_for_structured_tasks
description: "Expensive models for classification/extraction"
match:
model:
- "gpt-4"
- "gpt-4-32k"
- "claude-3-opus-20240229"
prompt: "regex:(classify|extract|keywords?|categorize|label|sentiment)"
action: warn
severity: medium
suggestion: |
💡 Consider cheaper alternatives for structured tasks
**Better Options**:
- GPT-4o-mini: 200x cheaper
- GPT-3.5-turbo: 20x cheaper
- Traditional NLP: Free
**Learn More**: https://crashlens.dev/docs/structured-task-optimization
- id: fallback_to_more_expensive
description: "Fallback from cheap to expensive model"
match:
fallback_from: "gpt-3.5-turbo"
fallback_to: "gpt-4"
action: warn
severity: medium
suggestion: |
💡 Backwards fallback detected!
Fallback should go expensive → cheap (not cheap → expensive).
**Correct**: GPT-4 → GPT-3.5 (rate limit fallback)
**Incorrect**: GPT-3.5 → GPT-4 (increases cost!)
**Learn More**: https://crashlens.dev/docs/fallback-vs-escalation
- id: long_prompt_short_response
description: "Prompt > 2000 tokens, completion < 50 tokens"
match:
usage.prompt_tokens: "> 2000"
usage.completion_tokens: "< 50"
action: warn
severity: medium
suggestion: |
⚠️ Inefficient token ratio!
40:1 input/output ratio is highly inefficient.
**Optimization**:
1. Reduce few-shot examples (2-3 max)
2. Use embeddings for context selection
3. Compress system prompts
4. Enable prompt caching
**Savings**: 50-70% by optimizing prompt length
**Learn More**: https://crashlens.dev/docs/prompt-optimization
- id: repeated_prompt_content
description: "Same prompt sent 10+ times"
match:
prompt_hash_count: "> 10"
action: warn
severity: medium
suggestion: |
💡 Repeated prompt detected!
Enable caching for 90% savings.
**Solutions**:
1. OpenAI prompt caching (GPT-4 Turbo/4o - automatic)
2. Application-level caching (Redis)
3. Semantic caching (for similar prompts)
**Learn More**: https://crashlens.dev/docs/caching-strategies
- id: near_context_limit
description: "Using > 90% of context window"
match:
prompt_tokens: "> context_limit * 0.90"
action: warn
severity: medium
suggestion: |
⚠️ Near context window limit!
Risk of truncation or quality degradation.
**Solutions**:
1. Sliding window (keep last 10-20 messages)
2. Summarize old context
3. Use RAG instead of full documents
4. Switch to larger context model
**Learn More**: https://crashlens.dev/docs/context-management
- id: repeated_context_loading
description: "Same context loaded 20+ times"
match:
context_hash_count: "> 20"
action: warn
severity: medium
suggestion: |
💡 Repeated context loading!
Use conversation memory or caching for 90%+ savings.
**Solutions**:
1. OpenAI prompt caching (automatic)
2. Conversation state management
3. Vector DB for RAG
4. Redis caching layer
**Learn More**: https://crashlens.dev/docs/context-caching
# ============================================================================
# POLICY USAGE & EXAMPLES
# ============================================================================
# Basic production enforcement
# crashlens guard logs.jsonl --policy-file policies/production-ready.yaml --fail-on-violations
# CI/CD integration (GitHub Actions)
# name: LLM Cost Governance
# on: [push, pull_request]
# jobs:
# guard:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - name: Install CrashLens
# run: pip install crashlens
# - name: Check LLM costs
# run: |
# crashlens guard logs/*.jsonl \
# --policy-file policies/production-ready.yaml \
# --fail-on-violations \
# --format json \
# --output-dir reports/
# Continuous monitoring (Slack alerts)
# crashlens guard production-logs.jsonl \
# --policy-file policies/production-ready.yaml \
# --format slack \
# --slack-webhook $SLACK_WEBHOOK \
# --strip-pii
# Weekly cost review
# crashlens guard weekly-logs.jsonl \
# --policy-file policies/production-ready.yaml \
# --format markdown \
# --output-dir weekly-reports/ \
# --summary-only
# Custom environment variables
# export CRASHLENS_MAX_COST_PER_CALL=1.00 # Higher limit for dev
# export CRASHLENS_HOURLY_LIMIT=500 # $500/hour for high-traffic prod
# crashlens guard logs.jsonl --policy-file policies/production-ready.yaml
# ============================================================================
# CUSTOMIZATION EXAMPLES
# ============================================================================
# Create custom thresholds file: custom-thresholds.yaml
# variables:
# max_cost_per_call: 1.00 # Higher for dev/staging
# hourly_spend_limit: 200.00 # Adjust per environment
# daily_spend_limit: 2000.00
# max_retries_per_minute: 10 # More lenient for testing
# Use custom thresholds
# crashlens guard logs.jsonl \
# --policy-file policies/production-ready.yaml \
# --config custom-thresholds.yaml
# ============================================================================
# EXPECTED SAVINGS BREAKDOWN
# ============================================================================
# Estimated savings by category (based on typical usage patterns):
#
# 1. Model Overkill Prevention: 15-20%
# - Switch GPT-4 → GPT-4o-mini for simple tasks
# - Claude Opus → Haiku for basic operations
#
# 2. Retry Loop Elimination: 10-15%
# - Fix exponential backoff
# - Eliminate infinite retries
# - Don't retry 4xx errors
#
# 3. Budget Protection: 5-10%
# - Prevent spending spikes
# - Rate limiting
# - Cost alerts
#
# 4. Fallback Optimization: 5-10%
# - Shorter fallback chains
# - Correct fallback direction
# - Fix broken strategies
#
# 5. Prompt Optimization: 5-10%
# - Reduce token waste
# - Enable caching
# - Better context management
#
# 6. Context Window Optimization: 5-10%
# - Prevent truncation
# - Cache repeated context
# - Summarize conversations
#
# Total Expected Savings: 40-60%
# ============================================================================
# COMPLIANCE & REPORTING
# ============================================================================
# Generate compliance report
# crashlens guard logs.jsonl \
# --policy-file policies/production-ready.yaml \
# --format markdown \
# --output-dir compliance-reports/ \
# --include-suggestions
# Export metrics to Prometheus
# crashlens guard logs.jsonl \
# --policy-file policies/production-ready.yaml \
# --push-metrics \
# --pushgateway-url http://localhost:9091
# Email report to team
# crashlens guard logs.jsonl \
# --policy-file policies/production-ready.yaml \
# --format markdown \
# --email-to team@company.com \
# --email-subject "Weekly LLM Cost Review"
# ============================================================================
# RELATED DOCUMENTATION
# ============================================================================
# Individual policy templates (for focused analysis):
# - model-overkill-detection.yaml: Model selection optimization
# - retry-loop-prevention.yaml: Retry pattern fixes
# - budget-protection.yaml: Spending limit enforcement
# - fallback-storm-detection.yaml: Fallback chain optimization
# - prompt-optimization.yaml: Token efficiency
# - context-window-optimization.yaml: Context management
# Documentation links:
# - Policy syntax: https://crashlens.dev/docs/policy-syntax
# - CI/CD integration: https://crashlens.dev/docs/ci-cd-integration
# - Best practices: https://crashlens.dev/docs/best-practices
# - Troubleshooting: https://crashlens.dev/docs/troubleshooting