Cheat sheets

Constitutional AI and Input/Output Classifiers

Critique-and-revise training, tier resolution, input and output rails, their bypasses, and a YAML rules engine.

  • Concepts and terms
  • Tasks and commands
  • Annotated code
  • Diagrams
  • Comparisons
  • Troubleshooting
  • Questions and tips

Source

Three layers side by side#

FactorCAI weightsInput/output classifierRules engine
Acts onWhat the weights preferEvery input and output turnThe candidate response
SignalPrinciples, critique, AI feedbackHazard taxonomy, S1 to S14YAML predicates
ResultRefusal with an explanationBlock, flag or route by categoryOne violation per rule, plus a fix
Wins onUnseen close analogsFast reject of obvious misuseContractual constraints
MissesPrinciple ambiguity, driftEmoji smuggling, homoglyphs, paraphraseStructural rewrites
ExampleClaude ConstitutionLlama Guard 3, NeMo Guardrailsrules.yml

Four-tier resolver (L15.17)#

block = hardcoded_block(action)substring match, HARDCODED_PROHIBITIONS
if block:
return "refuse", f"hardcoded prohibition: {block}"
if score.safety >= 3:higher tier wins on conflict
return "refuse", f"safety violation (score={score.safety})"
if score.ethics >= 3:
return "refuse", f"ethics violation (score={score.ethics})"
if score.guidelines >= 3:modify, not refuse
return "modify", f"guideline conflict (score={score.guidelines}); modify"
return "allow", "all higher tiers clear; helpfulness respected"

Normalize, then classify (L15.18)#

out = unicodedata.normalize("NFKC", text)unify compatibility forms
out = _homoglyph_map(out)Cyrillic lookalikes to Latin
return "".join(ch for ch in out if not _is_invisible(ch))drop zero-width
hits = classify_raw(normalize(text))what classify_normalized does
hits = output_rail(o)OUTPUT_DISALLOWED on the model turn

One rule in rules.yml#

- name: no-empty-refusal
severity: mediumlow, medium or high
applies_when:false gives not_applicable
contains_regex: '\b(i cannot|i will not|i refuse|unable to help)\b'
must:
any_of:
- contains_regex: '\b(try|consider|alternative|instead|suggest)\b'
- contains_regex: '\bredirect\b'
explanation: "Refusals must offer a next step (a suggestion, an alternative, or a redirect)."
fix:
append_if_missing: "\n\nIf this is something I cannot help with, ..."

Troubleshooting#

SymptomCauseFix
Zero-width chars beat the keyword guardEmoji smuggling splits wordsnormalize() before classify_raw
Cyrillic lookalike passesHomoglyph substitution_homoglyph_map. Broaden the map
Secret leaks after input passedNo output railAdd output_rail() on the model turn
Request drifts over turnsTurn-level classifier contextDialog rails in Colang
"Licensed lab" premise winsReason-based tiers aloneHardcoded prohibitions
has bad severitySeverity not low, medium, highUse one of the three
missing required fieldNo name, explanation or mustAdd the field
unknown predicateKey not in the grammarUse an atomic or composed predicate

CAI training, two phases#

1SFT modelanswers a red-team prompt. It is helpful but notyet harmless.2Criticreads one sampled principle and critiques theresponse.3Reviserrevises the response to address the critique.That is the SFT target.4Policygenerates pairs of completions.5Feedback modelranks each pair against sampled principles.6Reward modeltrains on the AI preferences. PPO runs againstit.

Layered defense#

alloweddraftdraftviolations2nd passclientUser turnprocessInput railLlama Guard, NeMomodelModel weightstrained with CAIprocessOutput railclassifier gateprocessRules engineYAML constitutionprocessFixerlocal edits perruleclientVerdictaccepted orescalatedalloweddraftdraftviolations2nd passclientUser turnprocessInput railLlama Guard, NeMomodelModel weightstrained with CAIprocessOutput railclassifier gateprocessRules engineYAML constitutionprocessFixerlocal edits per ruleclientVerdictaccepted or escalated

Tiers and zones#

Tier 1
safety and supporting human oversight. Highest.
Tier 2
ethics. It supersedes guidelines on conflict.
Tier 3
Anthropic guidelines.
Tier 4
helpfulness. Lowest.
Hardcoded
CBRN uplift, CSAM, infrastructure attacks, identity deception. No override.
Soft-coded
length, topical scope, style, tool use. Operators adjust inside a bound.
L18.05 order
catastrophic, guidelines, ethics, helpful. Conflicts resolve top-down.

Numbers to know#

Claude Constitution
79 pages, CC0, January 2026.
CAI 2022
16 principles in the constitution.
Participatory 2023
~1,000 US respondents. ~50% agreement with the corporate text.
Constitutional Classifiers
23.7% compute overhead in v1, ~1% in v2.
Llama Guard 3
Llama-3.1-8B base, 13 hazards, 8 languages.
1B-INT4 variant
over 30 tokens/sec on mobile CPUs.
Llama Guard 4
S1 to S14. S14 is Code Interpreter Abuse.
Attacks
Emoji Smuggling 100% ASR on six guards. NeMo Guard Detect 72.54%.

Rule grammar#

CommandWhat it does
contains_regex, not_contains_regexRegex found or absent, case-insensitive
starts_with_regex, ends_with_regexMatch at the start or end of the text
max_words, min_wordsWord count bounds
all_of, any_of, not_Compose predicates. any_of short-circuits
applies_whenFalse records not_applicable
append_if_missing, prepend_if_missingAdds text when it is absent
replace_regexReplaces pattern with replacement
low, medium, highSeverities. The lesson 87 gate blocks high

Run and use#

TaskCode
Four-tier resolver demo (15.17)code/main.py
Classifier and output rail demo (15.18)code/main.py
CAI toy pipeline (18.05)python3 code/main.py
Rules engine demo (19.86)python3 main.py
Load the constitutionengine = Engine()
Evaluate a draftreport = engine.evaluate(draft)
Repair the violationsfixer.apply(draft, report.violations())
Diff draft and reviseddiff(draft, revised)

Q&A#

Is RLAIF just cheaper RLHF?
No. Labeler bias moves into principle interpretation.
What does RLAIF change?
Sycophancy drops. Goodhart's Law persists.
Where does the classifier sit?
Below the constitutional layer, above the runtime layer.
Why only local fixes?
Structural rewrites belong in a separate refusal-and-help layer.
Is one layer enough?
No. CAI shapes behaviour. Classifiers enforce invariants.