Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AISecurityLab Hackagent GuardedModel

From Leeroopedia
Knowledge Sources
Domains Security_Testing, Guardrails, Defense_Evasion
Last Updated 2026-10-29 00:00 GMT

Overview

Concrete tool for wrapping a target model with before/after content-safety guardrail classifiers, provided by the HackAgent models framework.

Description

GuardedModel wraps a Model with optional before and after guardrail classifiers. The before guardrail checks each prompt before it reaches the target — a blocked prompt never reaches the model. The after guardrail checks the response — a flagged response is withheld. Either way, the call returns a ModelResponse whose guardrail field says what happened.

ModelGuardrail is the default guardrail: it asks a classifier model for a JSON verdict ({"safe": bool, "categories": [...], "reasoning": "..."}). It fails open: an unavailable or unparseable classifier lets the text through, so a misconfigured guardrail never blocks the whole run.

parse_verdict parses the classifier's raw JSON response, falling back to keyword detection (unsafe, "safe": false) when the text is not valid JSON.

The guardrail result is a frozen GuardrailResult dataclass: is_safe, explanation, categories, raw_response.

Usage

  • Defending a target: Configure guardrails.before and/or guardrails.after in the campaign YAML to wrap the target with classifiers. Resolution does this automatically via _guarded().
  • Custom system prompt: Set system_prompt on the guardrail model config to replace the default content-safety classifier instruction.
  • Measuring defense evasion: Run an attack against a guarded target to see how many prompts/replies the guardrail blocks.

Code Reference

Source Location

Signature

class GuardedModel(Model):
    """Apply the shared guardrail policy to native completions."""

    def __init__(self, model: Model, before: Optional[Guardrail] = None, after: Optional[Guardrail] = None) -> None: ...

    async def acomplete(self, messages, **overrides) -> ModelResponse: ...
    def complete(self, messages, **overrides) -> ModelResponse: ...

class ModelGuardrail:
    """A guardrail that asks a classifier model for a JSON verdict."""

    def __init__(self, llm: CompletionModel, system_prompt: Optional[str] = None) -> None: ...
    def check(self, text: str) -> GuardrailResult: ...

@dataclass(frozen=True)
class GuardrailResult:
    is_safe: bool
    explanation: str
    categories: List[str] = field(default_factory=list)
    raw_response: Optional[str] = None

def parse_verdict(raw: str) -> GuardrailResult: ...

Import

from hackagent.models.guardrail import GuardedModel, ModelGuardrail, GuardrailResult, parse_verdict

I/O Contract

Inputs

Name Type Required Description
model Model Yes The target model to wrap
before Optional[Guardrail] No Classifier checking prompts before they reach the target
after Optional[Guardrail] No Classifier checking replies before they are returned
text str Yes (check) The prompt or reply text to classify

Outputs

Name Type Description
acomplete return ModelResponse If blocked: text="", guardrail=GuardrailInfo, metadata["ok"]=False
check return GuardrailResult is_safe, explanation, categories, raw_response

Usage Examples

Wrapping a Model with Guardrails

from hackagent.models.guardrail import GuardedModel, ModelGuardrail
from hackagent.models import build_model

# Build the target and guardrail classifier models
target = build_model(target_config)
guardrail_model = build_model(guardrail_config)

# Wrap: check prompts before, replies after
guarded = GuardedModel(
    target,
    before=ModelGuardrail(guardrail_model),
    after=ModelGuardrail(guardrail_model),
)

# A blocked prompt returns an empty response with guardrail info
response = await guarded.acomplete([{"role": "user", "content": "harmful prompt"}])
if response.guardrail is not None:
    print(f"Blocked by {response.guardrail.side} guardrail: {response.guardrail.reasoning}")

Custom Guardrail System Prompt

from hackagent.models.guardrail import ModelGuardrail

# Use a custom classifier instruction
guardrail = ModelGuardrail(
    classifier_model,
    system_prompt="You are a custom safety filter. Reply with JSON: {\"safe\": true|false, ...}",
)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment