Interface: IGuardrailService
Defined in: packages/agentos/src/safety/guardrails/IGuardrailService.ts:387
Contract for implementing custom guardrail logic.
Guardrails intercept content at two points:
- Input - Before user messages enter the orchestration pipeline
- Output - Before agent responses are streamed to the client
Both methods are optional—implement only what you need.
Examples
class ContentFilterGuardrail implements IGuardrailService {
async evaluateInput({ input }: GuardrailInputPayload) {
if (input.textInput?.includes('prohibited')) {
return {
action: GuardrailAction.BLOCK,
reason: 'Input contains prohibited content',
reasonCode: 'CONTENT_BLOCKED'
};
}
return null; // Allow
}
}
class CostCeilingGuardrail implements IGuardrailService {
config = { evaluateStreamingChunks: true };
private tokenCount = 0;
async evaluateOutput({ chunk }: GuardrailOutputPayload) {
if (chunk.type === 'TEXT_DELTA') {
this.tokenCount += chunk.textDelta?.length ?? 0;
if (this.tokenCount > 5000) {
// "Change mind" - stop mid-stream
return {
action: GuardrailAction.BLOCK,
reason: 'Response exceeded cost ceiling'
};
}
}
return null;
}
}
class PIIRedactionGuardrail implements IGuardrailService {
config = { evaluateStreamingChunks: true, maxStreamingEvaluations: 100 };
async evaluateOutput({ chunk }: GuardrailOutputPayload) {
if (chunk.type === 'TEXT_DELTA' && chunk.textDelta) {
const redacted = chunk.textDelta.replace(
/\b\d{3}-\d{2}-\d{4}\b/g,
'[SSN REDACTED]'
);
if (redacted !== chunk.textDelta) {
return {
action: GuardrailAction.SANITIZE,
modifiedText: redacted,
reasonCode: 'PII_REDACTED'
};
}
}
return null;
}
}
Extended by
Properties
config?
optionalconfig:GuardrailConfig
Defined in: packages/agentos/src/safety/guardrails/IGuardrailService.ts:392
Configuration for evaluation behavior. Controls streaming vs final-only evaluation and rate limiting.
Methods
evaluateInput()?
optionalevaluateInput(payload):Promise<GuardrailEvaluationResult|null>
Defined in: packages/agentos/src/safety/guardrails/IGuardrailService.ts:408
Evaluate user input before orchestration.
Called once per request before the orchestration pipeline starts. Use this to validate, sanitize, or block user messages.
Parameters
payload
Input and context to evaluate
Returns
Promise<GuardrailEvaluationResult | null>
Evaluation result, or null to allow without action
Remarks
- Return
BLOCKto prevent the request from being processed - Return
SANITIZEwithmodifiedTextto clean the input - Return
nullorALLOWto let the request through