/directive:hardening-ai-systemsfrom directive
Adds guardrails (input/output validation), memory strategies, token budgets, PII detection, prompt injection prevention, and circuit breakers to AI orchestrators using @directive-run/ai.
Ships in directive. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
This skill applies when the project uses @directive-run/ai. If not found in package.json, suggest installing it: npm install @directive-run/ai.
createGuardrail, GuardrailError, createAgentMemorybuilding-ai-orchestratorsbuilding-ai-agentstesting-ai-systemsWhat do you need?
├── Block bad inputs before LLM call → Input guardrail
├── Validate/filter LLM output → Output guardrail
├── Remember context across turns → createAgentMemory
├── Limit cost / token spend → maxTokenBudget
├── Detect PII in prompts → PII guardrail
├── Prevent prompt injection → Injection guardrail
└── Handle provider failures gracefully → Circuit breaker + retry
Guardrails are pure validators. They check content and throw GuardrailError on violation. They must NOT modify the content they check.
import { createGuardrail, GuardrailError } from "@directive-run/ai";
// Input guardrail – runs before each LLM call
const lengthGuardrail = createGuardrail({
name: "input-length",
type: "input",
check: async (input) => {
if (input.prompt.length > 10_000) {
throw new GuardrailError({
code: "INPUT_TOO_LONG",
message: `Input exceeds 10,000 characters (got ${input.prompt.length})`,
guardrail: "input-length",
});
}
// No return value needed – guardrails only validate
},
});
// Output guardrail – runs after each LLM response
const toxicityGuardrail = createGuardrail({
name: "toxicity-check",
type: "output",
check: async (output) => {
const score = await getToxicityScore(output.text);
if (score > 0.8) {
throw new GuardrailError({
code: "TOXIC_OUTPUT",
message: `Toxicity score ${score} exceeds threshold`,
guardrail: "toxicity-check",
});
}
},
});
// Attach to orchestrator
const orchestrator = createAgentOrchestrator({
runner: createAnthropicRunner({ model: "claude-opus-4-6", apiKey: process.env.ANTHROPIC_API_KEY }),
guardrails: [lengthGuardrail, toxicityGuardrail],
factsSchema: {
input: t.string(),
output: t.string().optional(),
status: t.string<"idle" | "done" | "blocked">(),
violationCode: t.string().optional(),
},
init: (facts) => {
facts.status = "idle";
},
resolvers: {
process: {
requirement: "PROCESS",
resolve: async (req, context) => {
try {
const result = await context.runner.run({ prompt: context.facts.input });
context.facts.output = result.text;
context.facts.status = "done";
} catch (error) {
if (error instanceof GuardrailError) {
context.facts.status = "blocked";
context.facts.violationCode = error.code;
} else {
throw error;
}
}
},
},
},
});
const piiGuardrail = createGuardrail({
name: "pii-check",
type: "input",
check: async (input) => {
const patterns: Record<string, RegExp> = {
ssn: /\b\d{3}-\d{2}-\d{4}\b/,
creditCard: /\b\d{4}[\s-]\d{4}[\s-]\d{4}[\s-]\d{4}\b/,
email: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/,
};
const detected = Object.entries(patterns)
.filter(([, re]) => re.test(input.prompt))
.map(([type]) => type);
if (detected.length > 0) {
throw new GuardrailError({
code: "PII_DETECTED",
message: `Input contains PII: ${detected.join(", ")}`,
guardrail: "pii-check",
metadata: { piiTypes: detected },
});
}
},
});
const injectionGuardrail = createGuardrail({
name: "injection-prevention",
type: "input",
check: async (input) => {
const patterns = [
/ignore\s+(all\s+)?previous\s+instructions/i,
/you\s+are\s+now\s+(?:in\s+)?(?:developer|admin|god)\s+mode/i,
/disregard\s+(?:your\s+)?(?:system\s+)?instructions/i,
/<\|im_start\|>/i,
];
const matched = patterns.some((re) => re.test(input.prompt));
if (matched) {
throw new GuardrailError({
code: "PROMPT_INJECTION",
message: "Potential prompt injection detected",
guardrail: "injection-prevention",
});
}
},
});
import { createAgentMemory } from "@directive-run/ai";
// Sliding window: keep last N messages
const memory = createAgentMemory({
strategy: "sliding-window",
maxMessages: 20,
maxTokens: 8000,
});
// Summary: compress old context with a cheap LLM
const summaryMemory = createAgentMemory({
strategy: "summary",
maxMessages: 50,
summarizeAfter: 30,
summaryRunner: createAnthropicRunner({ model: "claude-haiku-4-5", apiKey: process.env.ANTHROPIC_API_KEY }),
});
// Semantic: retrieve relevant past messages by similarity
const semanticMemory = createAgentMemory({
strategy: "semantic",
maxRetrieved: 5,
embedder: myEmbeddingFunction,
});
// Usage in resolver
resolvers: {
respond: {
requirement: "RESPOND",
resolve: async (req, context) => {
const result = await context.runner.run({
prompt: context.facts.userMessage,
messages: context.memory.getMessages(), // inject history
});
context.facts.response = result.text;
// Memory updates automatically after each run
},
},
},
const orchestrator = createAgentOrchestrator({
runner: createAnthropicRunner({ model: "claude-opus-4-6", apiKey: process.env.ANTHROPIC_API_KEY }),
budget: {
maxTokenBudget: 100_000, // Hard limit – halts execution
budgetWarningThreshold: 0.8, // Fire warning at 80%
onBudgetWarning: (used, max) => {
console.warn(`Budget at ${Math.round((used / max) * 100)}%`);
},
},
factsSchema: {
input: t.string(),
output: t.string().optional(),
tokensUsed: t.number(),
},
init: (facts) => {
facts.tokensUsed = 0;
},
resolvers: {
process: {
requirement: "PROCESS",
resolve: async (req, context) => {
if (!context.budget.canAfford(estimateTokens(context.facts.input))) {
throw new Error("Insufficient token budget");
}
const result = await context.runner.run({ prompt: context.facts.input });
context.facts.output = result.text;
context.facts.tokensUsed += result.usage.totalTokens;
},
},
},
});
function estimateTokens(text: string): number {
return Math.ceil(text.length / 4); // ~4 chars per token
}
import { createCircuitBreaker } from "@directive-run/ai";
const breaker = createCircuitBreaker({
failureThreshold: 5, // Open after 5 consecutive failures
recoveryTimeout: 30_000, // Retry after 30s
halfOpenRequests: 2,
onStateChange: (from, to) => console.log(`Circuit: ${from} → ${to}`),
});
// Retry config on the resolver
resolvers: {
callLLM: {
requirement: "CALL_LLM",
retry: {
attempts: 3,
backoff: "exponential",
initialDelay: 500,
maxDelay: 10_000,
retryOn: (error) => {
return error.message.includes("rate_limit") ||
error.message.includes("overloaded");
},
},
resolve: async (req, context) => {
const result = await context.runner.run({ prompt: context.facts.input });
context.facts.output = result.text;
},
},
},
// WRONG – guardrails must not transform input
const bad = createGuardrail({
name: "sanitizer",
type: "input",
check: async (input) => {
input.prompt = input.prompt.replace(/badword/g, "***"); // NEVER mutate
return input; // NEVER return content
},
});
// CORRECT – only validate, throw on violation
const good = createGuardrail({
name: "content-check",
type: "input",
check: async (input) => {
if (input.prompt.includes("badword")) {
throw new GuardrailError({ code: "PROHIBITED", message: "Prohibited content", guardrail: "content-check" });
}
},
});
// WRONG – runaway loops = runaway costs
const orchestrator = createAgentOrchestrator({ runner, /* no budget */ });
// CORRECT
const orchestrator = createAgentOrchestrator({
runner,
budget: { maxTokenBudget: 50_000, budgetWarningThreshold: 0.8 },
});
// WRONG – violations disappear
} catch (e) { /* swallowed */ }
// CORRECT
} catch (error) {
if (error instanceof GuardrailError) {
context.facts.status = "blocked";
context.facts.violationCode = error.code;
} else {
throw error;
}
}
Always use (req, context) – never (req, ctx) or (request, context).
ai-guardrails-memory.md – Full guardrail API, all memory strategies, configuration optionsai-budget-resilience.md – Budget configuration, circuit breaker, retry policies, estimateTokensai-security.md – PII patterns, injection detection, security best practicesexamples/auth-flow.ts – Auth-gated orchestrator with security guardrailsexamples/fraud-analysis.ts – Budget-aware multi-agent fraud detectionWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
12 skills in this plugin
/building-ai-agentsConfigure AI provider runners (Anthropic, OpenAI, Ollama), stream tokens and structured output, and wire cross-agent communication. Use when setting up LLM provider connections, implementing token streaming, choosing between providers, or building agents that communicate results to each other./building-ai-orchestratorsCreate AI orchestration systems using createAgentOrchestrator (single agent) and createMultiAgentOrchestrator (multi-agent). Define factsSchema with t.*() builders, init state, constraints for trigger conditions, and resolvers for LLM execution. Use when building LLM-powered workflows, agent pipelines, task runners, or multi-agent coordination systems./npx claudepluginhub directive-run/directive --plugin directive