Claude Certified Architect

Foundations · CCAR-F

Domain 1 — 27%

Task 1.7

Guardrails: Pre & Post Execution

Safety checks before and after tool execution

📘What You Need to Know

What Are Guardrails?

Guardrails are programmatic safety checks that run automatically around tool execution. Unlike prompt instructions (which the model may ignore), guardrails are code-level enforcement that runs deterministically every time. They are the same category as hooks — 100% reliable.

The exam distinguishes pre-execution guardrails (validate before action) from post-execution guardrails (validate after action). Both are essential for production agent systems.

Pre-Execution Guardrails

Pre-execution guardrails run BEFORE a tool executes. They validate whether the action is allowed, check parameters against policy, and can block execution entirely if rules are violated.

Use pre-guardrails for authorization checks, parameter validation, rate limiting, and any scenario where you need to prevent an action from happening — not just clean up after it.

  • •Validate refund amount against policy before process_refund runs
  • •Check user authorization before accessing sensitive data
  • •Enforce rate limits before API calls execute
  • •Sanitize parameters before database writes
🔑Key Concept

Pre-guardrails prevent bad actions. Post-guardrails clean up bad outputs. You need both for complete protection.

Post-Execution Guardrails

Post-execution guardrails run AFTER a tool completes. They inspect the tool output, enforce business thresholds on results, redact sensitive information, and trigger side effects like escalation.

Use post-guardrails for PII redaction, output format validation, business threshold enforcement on results, and normalizing data before it reaches the user or next agent.

# Post-execution PII redaction guardrail
def post_guardrail(tool_output):
    output_text = tool_output.get("response", "")
    # Redact email addresses and phone numbers deterministically
    output_text = re.sub(r'\b[\w.-]+@[\w.-]+\.[\w]+\b', '[EMAIL REDACTED]', output_text)
    output_text = re.sub(r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b', '[PHONE REDACTED]', output_text)
    return {"response": output_text}

Real-World Example: Bank ATM Analogy

Pre-check: validates your PIN and checks if the withdrawal amount is within your daily limit BEFORE dispensing cash. If either fails, the transaction is blocked before any money moves.

Post-check: verifies the balance did not go negative AFTER dispensing. If something went wrong, it triggers an alert. Both checks happen automatically without user involvement — exactly like programmatic guardrails.

Guardrails vs Prompt Instructions

A common mistake: adding "always redact PII from responses" to the system prompt. Claude may follow this most of the time, but under edge cases or long contexts, it can leak PII. A post-execution guardrail with regex or NER-based redaction runs every time, regardless of model behavior.

The exam rule: safety-critical output validation (PII, compliance, financial thresholds) = guardrails. Style and tone preferences = prompts.

⚠️Watch out

Never use prompt instructions as guardrails for PII redaction, refund limits, or compliance checks.

⚠️Exam Traps

Trap: Using prompt instructions as guardrails for PII redaction

Why it's wrong: The model may skip or inconsistently apply prompt-based safety rules.

Correct approach: Use post-execution code to scan and redact PII deterministically.

Trap: Only implementing post-guardrails without pre-guardrails

Why it's wrong: Some actions should be blocked before execution, not just cleaned up after.

Correct approach: Pre-guardrails for authorization; post-guardrails for output validation.

Trap: Relying on Claude to self-enforce refund amount limits

Why it's wrong: Model judgment is probabilistic — limits need deterministic enforcement.

Correct approach: PreToolUse or PostToolUse hooks enforce thresholds in code.

Trap: Treating guardrails and hooks as different concepts on the exam

Why it's wrong: Guardrails ARE hooks — both are programmatic, deterministic enforcement.

Correct approach: Understand guardrails as the purpose; hooks as the mechanism.

✓Concept Check

Before executing a high-value refund, you need to verify the amount is within policy. Where should this check run?

🔨Build Exercise

Intermediate45 minutes

Implement Pre and Post Guardrails

What you'll learn

  • •Build pre and post execution safety checks
  • •Distinguish guardrails from prompt-based safety