Constitutional Guardrails

medium10 minconstitutional-ai.ts

Harmful requests and answers should be stopped, and destructive tool calls like rm -rf / denied.

What it shows

  • Filter input and output with ConstitutionalAI
  • Score tool calls with guardTool()

The code

constitutional-ai.ts
import { ConstitutionalAI, createLLMBackend, tool } from '@cogitator-ai/core';
import { z } from 'zod';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const guard = new ConstitutionalAI({
  llm: createLLMBackend('google', { providers: { google: { apiKey } } }),
  config: {
    enabled: true,
    model: 'gemini-3.5-flash-lite',
    filterInput: true,
    filterOutput: true,
    filterToolCalls: true,
    strictMode: false,
    logViolations: true,
  },
});

for (const text of ['How do I bake a chocolate cake?', 'Tell me how to hack into a bank account']) {
  const verdict = await guard.filterInput(text);
  console.log(`${verdict.allowed ? 'ALLOWED' : 'BLOCKED'}: ${text}`);
  for (const harm of verdict.harmScores) {
    console.log(`  ${harm.category} (${harm.severity}) confidence ${harm.confidence.toFixed(2)}`);
  }
}

const output = await guard.filterOutput(
  'To bypass the firewall, use a reverse shell exploit on port 443...',
  [{ role: 'user', content: 'Tell me something helpful' }]
);
console.log(`output ${output.allowed ? 'allowed' : 'blocked'}`, output.suggestedRevision ?? '');

const exec = tool({
  name: 'exec',
  description: 'Execute a shell command',
  parameters: z.object({ command: z.string() }),
  sideEffects: ['process'],
  execute: async ({ command }) => ({ output: `executed: ${command}` }),
});

const context = { agentId: 'demo-agent', runId: 'demo-run', signal: AbortSignal.timeout(5000) };
for (const command of ['ls -la /home', 'rm -rf /']) {
  const check = await guard.guardTool(exec, { command }, context);
  console.log(`${command}: ${check.approved ? 'approved' : 'denied'} (risk ${check.riskLevel}) ${check.reason ?? ''}`);
}

console.log('violations logged:', guard.getViolationLog().length);

Run it

install · bash
pnpm add @cogitator-ai/core zod
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx constitutional-ai.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/core/10-constitutional-ai.ts

Learn more