01Install
Install from npm into your project.
BASH
pnpm add @lov3kaizen/agentsea-guardrails02Example
TS
import { createGuardrailsEngine, ToxicityGuard, PIIGuard, PromptInjectionGuard } from '@lov3kaizen/agentsea-guardrails';
const engine = createGuardrailsEngine({
guards: [
{ name: 'toxicity', enabled: true, onFailure: 'block' },
{ name: 'pii', enabled: true, onFailure: 'transform' },
{ name: 'prompt-injection', enabled: true, onFailure: 'block' },
], failureMode: 'fail-fast', defaultAction: 'allow',
});
engine.registerGuard(new ToxicityGuard({ sensitivity: 'medium' }));
engine.registerGuard(new PIIGuard({ types: ['email', 'phone'], enableMasking: true, maskFormat: '[REDACTED]' }));
engine.registerGuard(new PromptInjectionGuard({ sensitivity: 'high' }));
const result = await engine.checkInput('What is the weather today?', { sessionId: 'session-1', userId: 'user-1' });
if (result.passed) console.log('Input is safe');03Key API
createGuardrailsEngine({ guards, failureMode, defaultAction })Builds the engine. failureMode is fail-fast, fail-safe or collect-all.
engine.checkInput(text, context)Returns passed, message and transformedContent.
PIIGuard({ types, customPatterns, maskFormat, enableMasking })Detects PII such as email and phone. With enableMasking, it replaces each match with maskFormat.
TokenBudgetGuard / CostGuardToken limits per request and per session (TokenBudgetGuard). Cost limits per request, per session and per day (CostGuard).
createRulesEngine({ ruleSets })Runs JSON rule sets with conditions, actions and priorities.
04Caveats
- FactualityGuard needs a verifier function you supply. The example uses a fixed result.
- The docs do not explain the detection method for each guard, or how sensitivity changes detection.
- Framework adapters live under subpaths: /nestjs, /agentsea, /langchain and /vercel-ai.
- The Vercel AI SDK example uses experimental_transform. Check it against your SDK version.
05Built-in guards
Each guard is a class you register on the engine. The options below are the ones the guard classes accept.
ToxicityGuard({ sensitivity, categories, categoryThresholds })Flags toxic content by category, with a threshold per category.
PIIGuard({ types, customPatterns, maskFormat, enableMasking })Detects PII. Pass your own patterns in customPatterns.
TopicGuard({ allowedTopics, blockedTopics, topicKeywords, minConfidence })Filters content by allowed and blocked topics.
BiasGuard({ categories, customPatterns })Detects biased language by category.
PromptInjectionGuard({ sensitivity, customPatterns, useHeuristics, checkDelimiters })Detects prompt injection attempts.
JailbreakGuard({ checkDAN, checkRoleplay, checkHypothetical, customPatterns })Detects jailbreak attempts such as DAN prompts and roleplay framing.
DataLeakageGuard({ blockApiKeys, blockPasswords, blockPrivateKeys, blockConnectionStrings, customPatterns, enableMasking })Stops secrets from leaving in outputs.
SchemaGuard({ schema, strict, parseJson, errorMessages })Validates output against a Zod schema.
FormatGuard({ format, customValidator, allowEmpty })Checks that output matches an expected format, or a custom validator.
RateLimitGuard({ maxRequests, windowMs, keyBy, keyExtractor, skipSuccessfulRequests })Limits requests in a time window, keyed by user, session, IP or globally.
TS
import { SchemaGuard } from '@lov3kaizen/agentsea-guardrails';
import { z } from 'zod';
const ResponseSchema = z.object({
answer: z.string(),
confidence: z.number().min(0).max(1),
});
const guard = new SchemaGuard({ schema: ResponseSchema, parseJson: true });06Rules engine
Write policies as JSON rule sets and load them into the rules engine. Each rule has a priority and can be disabled.
TS
import { createRulesEngine, type RuleSet } from '@lov3kaizen/agentsea-guardrails';
const rules: RuleSet = {
id: 'content-policy',
name: 'Content Policy',
version: '1.0.0',
rules: [
{
id: 'block-profanity',
name: 'Block Profanity',
conditions: { field: 'input', operator: 'matches', value: '\\b(bad|word)\\b' },
actions: [{ type: 'block', params: { reason: 'Profanity detected' } }],
priority: 100,
enabled: true,
},
],
};
const engine = createRulesEngine({ ruleSets: [rules] });
const result = await engine.evaluate('Contact me at user@example.com');07Framework adapters
@lov3kaizen/agentsea-guardrails/nestjsfor NestJS modules, guards and decorators.@lov3kaizen/agentsea-guardrails/agentseafor the AgentSea integration.@lov3kaizen/agentsea-guardrails/langchainand/vercel-aifor the LangChain and Vercel AI SDK integrations.