Configuration
LLM Guard provides various configuration options to customize its behavior. This page explains how to configure LLM Guard for your specific needs.
Basic Configuration
When creating a new LLM Guard instance, you can specify which guards to enable:
import { LLMGuard } from 'llm-guard';
const guard = new LLMGuard({
pii: true,
jailbreak: true,
profanity: true,
promptInjection: true,
relevance: true,
toxicity: true
});
Guard-Specific Options
Each guard can be configured with specific options:
PII Guard Options
piiOptions is not supported. The PII guard takes no options — enable it with
pii: true and it uses its built-in patterns. Tracked in
#6.
Profanity Guard Options
profanityOptions is not supported. The profanity guard takes no options — enable it
with profanity: true and it uses its built-in word list. Tracked in
#6.
Jailbreak Guard Options
const guard = new LLMGuard({
jailbreak: true,
jailbreakOptions: {
// Additive: these run alongside the built-in patterns, never instead of them.
customPatterns: [
/enter developer mode/i,
/pretend you are unrestricted/i
]
}
});
customPatterns is the only supported field. There is no threshold — the jailbreak
guard is pattern-based, not scored.
Prompt Injection Guard Options
const guard = new LLMGuard({
promptInjection: true,
promptInjectionOptions: {
// Additive: these run alongside the built-in patterns, never instead of them.
customPatterns: [
/exfiltrate the vault/i,
/override system prompt/i
]
}
});
customPatterns is the only supported field. There is no sensitivity — the guard is
pattern-based, so widen or narrow it by supplying your own patterns.
Relevance Guard Options
const guard = new LLMGuard({
relevance: true,
relevanceOptions: {
minLength: 10, // Minimum text length
maxLength: 5000, // Maximum text length
minWords: 3, // Minimum word count
maxWords: 1000 // Maximum word count
}
});
Toxicity Guard Options
toxicityOptions is not supported. The toxicity guard takes no options — enable it with
toxicity: true and it uses its built-in word list. Tracked in
#6.
Custom Validation Rules
You can create custom validation rules to address specific validation needs:
const guard = new LLMGuard({
customRules: {
// Check for minimum length
minLength: (prompt) => {
const minLength = 10;
const isValid = prompt.length >= minLength;
return {
isValid,
errors: isValid ? [] : [`Prompt must be at least ${minLength} characters long`]
};
},
// Check for specific keywords
requiredKeywords: (prompt) => {
const requiredWords = ['security', 'authentication'];
const missingWords = requiredWords.filter(word => !prompt.toLowerCase().includes(word));
const isValid = missingWords.length === 0;
return {
isValid,
errors: isValid ? [] : [`Prompt must include: ${missingWords.join(', ')}`]
};
}
}
});
Configuration File
For CLI usage, you can create a configuration file (e.g., config.json):
{
"pii": true,
"jailbreak": true,
"profanity": true,
"promptInjection": true,
"relevance": true,
"toxicity": true,
"relevanceOptions": {
"minLength": 10,
"maxLength": 5000
}
}
Then use it with the CLI:
npx llm-guard --config config.json "Your prompt here"
Environment Variables
LLM Guard also supports configuration through environment variables:
# Enable specific guards
LLM_GUARD_PII=true
LLM_GUARD_JAILBREAK=true
LLM_GUARD_PROFANITY=true
# Configure options
LLM_GUARD_RELEVANCE_MIN_LENGTH=10
LLM_GUARD_RELEVANCE_MAX_LENGTH=5000
LLM_GUARD_TOXICITY_THRESHOLD=0.7
Best Practices
- Enable only the guards you need to improve performance
- Adjust sensitivity thresholds based on your use case
- Use custom patterns for domain-specific validation
- Create custom rules for unique validation requirements
- Regularly update your configuration to address new security concerns