Core types, chain orchestration, budget management, and utilities for the Guardrail Chain framework, providing the `Guardrail` interface and `GuardrailChain` orchestrator that every guardrail implementation must satisfy.
Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.
Core types, chain orchestration, budget management, and utilities for the Guardrail Chain framework. This package is the foundation for all @reaatech/guardrail-chain-* packages and provides the Guardrail interface that every guardrail implementation must satisfy.
Composable guardrail pipeline — chain input and output guardrails with budget-aware scheduling, priority ordering, and short-circuit-on-failure logic.
Budget management — track latency and token budgets, skip non-essential guardrails under pressure, and prevent budget overruns.
Fluent builder API — construct chains declaratively with ChainBuilder, or wire them programmatically with GuardrailChain.
Retry with exponential backoff — transparent retry for transient failures, with configurable predicates, jitter, and caps.
Circuit breaker — protect external-service guardrails from cascading failures with CLOSED/OPEN/HALF_OPEN state tracking.
LRU cache — TTL-aware cache for guardrail results, with configurable eviction policy.
Exported observability getter/setter functions — setLogger, setMetrics, setTracer re-exported from @reaatech/guardrail-chain-observability for single-import convenience.
Zero runtime dependencies beyond @reaatech/guardrail-chain-observability — lightweight and tree-shakeable.
Dual ESM/CJS output — works with import and require.
Quick Start
typescript
import { GuardrailChain, ChainBuilder, setLogger, ConsoleLogger, type Guardrail, type GuardrailResult, type ChainContext,} from '@reaatech/guardrail-chain';setLogger(new ConsoleLogger());const chain = new ChainBuilder() .withBudget({ maxLatencyMs: 500, maxTokens: 4000 }) .build();const result = await chain.execute('What is the weather today?');console.log(result.success ? 'Passed' : 'Failed');
Exports
Core Classes
Export
Description
GuardrailChain
Main orchestrator — executes input and output guardrail phases with budget-aware scheduling, timeout handling, retry, and short-circuit logic.
ChainBuilder
Fluent builder for constructing GuardrailChain instances with a declarative chaining API.
BudgetManager
Tracks and enforces latency and token budgets throughout chain execution.
Core Types
Export
Description
Guardrail<TInput, TOutput>
The fundamental guardrail interface — every guardrail must implement id, name, type, enabled, execute(), and optional metadata fields.
GuardrailResult<TOutput>
Result of a single guardrail execution — passed, optional output, confidence, metadata, error.
ChainContext
State passed between guardrails — correlationId, userId, sessionId, budget, metadata, transformedInput, originalInput.
ChainConfig
Configuration for the chain — budget, observability, errorHandling.
ChainResult
Result of a complete chain execution — success, output, error, failedGuardrail, metadata.
ChainResultMetadata
Well-known fields on ChainResult.metadata — correlationId, inputDuration, outputDuration, totalDuration, phase.