Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/visible/cruel/llms.txt

Use this file to discover all available pages before exploring further.

Cruel provides five ready-to-use chaos presets that simulate different levels of AI infrastructure stress. Use these to quickly test your application’s resilience.

Available Presets

import { presets } from 'cruel/ai-sdk'

// Five levels of chaos
presets.realistic     // Light chaos for development
presets.unstable      // Medium chaos for CI/staging
presets.harsh         // Heavy chaos for stress testing
presets.nightmare     // Extreme chaos for resilience testing
presets.apocalypse    // Maximum chaos for chaos engineering

Preset Reference

realistic

Light chaos that simulates normal production conditions.
const realistic: CruelChaosOptions = {
  rateLimit: 0.02,              // 2% rate limits
  overloaded: 0.01,             // 1% overload
  delay: [50, 200],             // 50-200ms latency
  slowTokens: [20, 80],         // 20-80ms per token
}
rateLimit
0.02
2% chance of rate limit errors
overloaded
0.01
1% chance of model overload
delay
[50, 200]
50-200ms latency before responses
slowTokens
[20, 80]
20-80ms delay between streamed tokens
Use for:
  • Development testing
  • Smoke tests
  • Verifying basic error handling

unstable

Moderate chaos for staging environments.
const unstable: CruelChaosOptions = {
  rateLimit: 0.1,               // 10% rate limits
  overloaded: 0.05,             // 5% overload
  streamCut: 0.05,              // 5% stream interruptions
  delay: [100, 500],            // 100-500ms latency
  slowTokens: [50, 200],        // 50-200ms per token
}
rateLimit
0.1
10% chance of rate limit errors
overloaded
0.05
5% chance of model overload
streamCut
0.05
5% chance of stream interruption
delay
[100, 500]
100-500ms latency before responses
slowTokens
[50, 200]
50-200ms delay between streamed tokens
Use for:
  • CI/CD pipelines
  • Staging environment testing
  • Integration tests

harsh

Heavy chaos for stress testing.
const harsh: CruelChaosOptions = {
  rateLimit: 0.2,               // 20% rate limits
  overloaded: 0.1,              // 10% overload
  streamCut: 0.1,               // 10% stream interruptions
  contentFilter: 0.02,          // 2% content filter
  delay: [200, 1000],           // 200-1000ms latency
  slowTokens: [100, 500],       // 100-500ms per token
}
rateLimit
0.2
20% chance of rate limit errors
overloaded
0.1
10% chance of model overload
streamCut
0.1
10% chance of stream interruption
contentFilter
0.02
2% chance of content filter errors
delay
[200, 1000]
200-1000ms latency before responses
slowTokens
[100, 500]
100-500ms delay between streamed tokens
Use for:
  • Stress testing
  • Load testing
  • Verifying retry logic

nightmare

Extreme chaos for resilience testing.
const nightmare: CruelChaosOptions = {
  rateLimit: 0.3,               // 30% rate limits
  overloaded: 0.15,             // 15% overload
  streamCut: 0.15,              // 15% stream interruptions
  contentFilter: 0.05,          // 5% content filter
  contextLength: 0.05,          // 5% context length errors
  partialResponse: 0.1,         // 10% partial responses
  delay: [500, 2000],           // 500-2000ms latency
  slowTokens: [200, 1000],      // 200-1000ms per token
  toolFailure: 0.1,             // 10% tool failures
}
rateLimit
0.3
30% chance of rate limit errors
overloaded
0.15
15% chance of model overload
streamCut
0.15
15% chance of stream interruption
contentFilter
0.05
5% chance of content filter errors
contextLength
0.05
5% chance of context length errors
partialResponse
0.1
10% chance of truncated responses
delay
[500, 2000]
500-2000ms latency before responses
slowTokens
[200, 1000]
200-1000ms delay between streamed tokens
toolFailure
0.1
10% chance of tool execution failure
Use for:
  • Resilience testing
  • Chaos engineering
  • Finding edge cases

apocalypse

Maximum chaos for extreme scenarios.
const apocalypse: CruelChaosOptions = {
  rateLimit: 0.4,               // 40% rate limits
  overloaded: 0.2,              // 20% overload
  streamCut: 0.2,               // 20% stream interruptions
  contentFilter: 0.1,           // 10% content filter
  contextLength: 0.1,           // 10% context length errors
  modelUnavailable: 0.1,        // 10% model unavailable
  partialResponse: 0.15,        // 15% partial responses
  corruptChunks: 0.05,          // 5% corrupt chunks
  delay: [1000, 5000],          // 1-5 second latency
  slowTokens: [500, 2000],      // 0.5-2 second per token
  toolFailure: 0.2,             // 20% tool failures
  toolTimeout: 0.1,             // 10% tool timeouts
}
rateLimit
0.4
40% chance of rate limit errors
overloaded
0.2
20% chance of model overload
streamCut
0.2
20% chance of stream interruption
contentFilter
0.1
10% chance of content filter errors
contextLength
0.1
10% chance of context length errors
modelUnavailable
0.1
10% chance of model unavailable errors
partialResponse
0.15
15% chance of truncated responses
corruptChunks
0.05
5% chance of corrupt stream chunks
delay
[1000, 5000]
1-5 second latency before responses
slowTokens
[500, 2000]
0.5-2 second delay between streamed tokens
toolFailure
0.2
20% chance of tool execution failure
toolTimeout
0.1
10% chance of tool timeout
Use for:
  • Maximum chaos testing
  • Breaking your app to find limits
  • Disaster recovery testing

Usage Examples

import { openai } from '@ai-sdk/openai'
import { generateText } from 'ai'
import { cruelModel, presets } from 'cruel/ai-sdk'

const model = cruelModel(openai('gpt-4'), presets.unstable)

const result = await generateText({
  model,
  prompt: 'Hello',
  maxRetries: 3,
})

Customizing Presets

Override specific values while keeping preset defaults:
import { cruelModel, presets } from 'cruel/ai-sdk'
import { openai } from '@ai-sdk/openai'

const model = cruelModel(openai('gpt-4'), {
  ...presets.unstable,
  
  // Override rate limit
  rateLimit: 0.3,
  
  // Add monitoring
  onChaos: (event) => {
    console.log('Chaos:', event.type)
  },
})

Environment-Based Presets

Select preset based on environment:
import { cruelModel, presets } from 'cruel/ai-sdk'
import { openai } from '@ai-sdk/openai'

const CHAOS_PRESETS = {
  development: presets.realistic,
  staging: presets.unstable,
  production: undefined,  // No chaos in prod
} as const

const env = process.env.NODE_ENV as keyof typeof CHAOS_PRESETS
const preset = CHAOS_PRESETS[env]

const model = preset
  ? cruelModel(openai('gpt-4'), preset)
  : openai('gpt-4')

Combining Presets

Merge multiple presets:
import { presets } from 'cruel/ai-sdk'

const custom = {
  ...presets.unstable,
  
  // Add specific failures from harsh
  contentFilter: presets.harsh.contentFilter,
  
  // Add tool chaos from nightmare
  toolFailure: presets.nightmare.toolFailure,
  
  // Custom values
  delay: [75, 350],
}

Testing Strategy

Use presets progressively:
1

Start with realistic

Verify basic error handling works:
const model = cruelModel(openai('gpt-4'), presets.realistic)
2

Move to unstable

Test retry logic and resilience:
const model = cruelModel(openai('gpt-4'), presets.unstable)
3

Try harsh

Stress test with heavy failures:
const model = cruelModel(openai('gpt-4'), presets.harsh)
4

Challenge with nightmare

Find edge cases and breaking points:
const model = cruelModel(openai('gpt-4'), presets.nightmare)
5

Break with apocalypse (optional)

Test absolute worst-case scenarios:
const model = cruelModel(openai('gpt-4'), presets.apocalypse)

Real-World Example

import { openai } from '@ai-sdk/openai'
import { generateText } from 'ai'
import { cruelModel, presets } from 'cruel/ai-sdk'

const CHAOS_LEVEL = process.env.CHAOS_LEVEL || 'realistic'

const chaosPresets = {
  realistic: presets.realistic,
  unstable: presets.unstable,
  harsh: presets.harsh,
  nightmare: presets.nightmare,
  apocalypse: presets.apocalypse,
  none: undefined,
} as const

type ChaosLevel = keyof typeof chaosPresets

const preset = chaosPresets[CHAOS_LEVEL as ChaosLevel]

const model = preset
  ? cruelModel(openai('gpt-4'), {
      ...preset,
      onChaos: (event) => {
        console.log(`[${CHAOS_LEVEL}] ${event.type}`, event)
      },
    })
  : openai('gpt-4')

export async function query(prompt: string) {
  return generateText({
    model,
    prompt,
    maxRetries: CHAOS_LEVEL === 'none' ? 0 : 3,
  })
}
Run with different chaos levels:
# Development
CHAOS_LEVEL=realistic npm test

# CI/CD
CHAOS_LEVEL=unstable npm test

# Stress testing
CHAOS_LEVEL=harsh npm run stress-test

# Chaos engineering
CHAOS_LEVEL=nightmare npm run chaos-test

# Production (no chaos)
CHAOS_LEVEL=none npm start

Preset Comparison

PresetRate LimitsOverloadStream CutContent FilterLatencyUse Case
realistic2%1%0%0%50-200msDevelopment
unstable10%5%5%0%100-500msCI/Staging
harsh20%10%10%2%200-1000msStress Testing
nightmare30%15%15%5%500-2000msResilience
apocalypse40%20%20%10%1-5 secondsMax Chaos

TypeScript

import type { CruelChaosOptions } from 'cruel/ai-sdk'
import { presets } from 'cruel/ai-sdk'

// All presets satisfy CruelChaosOptions
const realistic: CruelChaosOptions = presets.realistic
const unstable: CruelChaosOptions = presets.unstable
const harsh: CruelChaosOptions = presets.harsh
const nightmare: CruelChaosOptions = presets.nightmare
const apocalypse: CruelChaosOptions = presets.apocalypse

// Type-safe preset selection
type PresetName = 'realistic' | 'unstable' | 'harsh' | 'nightmare' | 'apocalypse'

function getPreset(name: PresetName): CruelChaosOptions {
  return presets[name]
}

Source Code

Presets are defined in ai-sdk.ts:529-580.

Next Steps

Model Chaos

Learn about all chaos options

Provider Wrapping

Use presets with providers

Tool Chaos

Apply presets to tools

Overview

Back to AI SDK overview

Build docs developers (and LLMs) love