TOOL WIKI · SECURITY & IDENTITY

Llama Guard

Classifies content against a safety taxonomy for application checks.

Download example files Code + setup + checks in a ZIP
Jump to a section 10 sections

EXAMPLE USE CASE

Classify conversation safety before generating an answer

An agent application needs independently enforced input, identity, data or action checks before exposing a protected capability.

Input
A chat message formatted with the selected safety model’s template.
Expected output
A safety verdict that the application parses before deciding whether to continue.
  • Success check: The output is a safety classification, not an answer to the user.
  • Try a failure: An unparseable verdict must block or escalate rather than default to safe.

Example scope: Actual local safety-model inference requiring weights/resources; the classifier does not answer the user’s task.

Official reference · Llama Guard

WHAT IT ISSafety classification model
WHERE IT FITSSecurity & identity
SEE HOW THE SYSTEM FITS TOGETHERSELECT A COMPONENT
inputprocessresultcheckoutputRequest or workloadVerified application inputLlama GuardSafety classification modelApplicationenforcementInterpret and enforceAllowed operationScoped service callAuthoritative outcomeSuccess or explicit errorAudit + reviewEvidence of the decision
  1. Identify the actor, operation, resource, and relevant evidence.

  2. A model-based content assessment stage in a larger protection system. A classification model does not enforce resource authorization.

  3. Consume the identity, policy result, credential, or detection from this component. Fail according to an explicit application policy.

  4. The business service still checks permission for the actual target resource.

  5. Record the result without leaking unnecessary credentials or sensitive content.

  6. Retain actor, policy version, operation identifier, and result with deliberate redaction.

    Result or stopping point
Llama Guard

A model-based content assessment stage in a larger protection system. A classification model does not enforce resource authorization.

Follow the arrows, then select a component to inspect its responsibility. On mobile, downward arrows connect the next step; bent arrows identify branches and return paths. Select a destination to jump to that component.

When to consider it

  • A model-based content assessment stage in a larger protection system.

A design decision to make

  • A classification model does not enforce resource authorization.

Prerequisites and setup

  • A Hugging Face account with access to the gated model, accepted model terms, and adequate GPU/CPU memory.
  • Weights are large. This is a pinned model example; use its matching prompt format and license requirements.
Shell
python3 -m venv .venv
source .venv/bin/activate
pip install transformers torch accelerate
hf auth login

Try a small integration

classify.py

Python · classify.py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "meta-llama/Llama-Guard-3-8B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")
messages = [{"role":"user","content":"Help me summarize a public product manual."}]
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
with torch.no_grad():
    output = model.generate(inputs, max_new_tokens=100, pad_token_id=tokenizer.eos_token_id)
verdict = tokenizer.decode(output[0][inputs.shape[-1]:], skip_special_tokens=True).strip()
print(verdict)
print("May continue:", verdict.splitlines()[0].lower() == "safe")

Run and check the result

Shell
python classify.py
  • The output is a safety classification, not an answer to the user.
  • Use representative safe/unsafe fixtures for the model’s published categories and treat unparseable results as blocked.

Common failures

  • Do not continue a protected operation when the policy response is missing, invalid or unavailable.
  • A successful test on one fixture is not a complete security evaluation; include bypass attempts and legitimate requests.

Production decisions

  • Enforce decisions in application/service code before an operation. A detector score or valid schema is not user authorization.
  • Test allowed, denied, malformed, timeout and unavailable outcomes; record redacted decision reasons and default to a safe failure state.
  • A content-safety classifier does not enforce tenant access, prevent all prompt injection or authorize financial/business actions.

Keep exploring

A human approval boundary for agent actionsA governed tool layer with MCP

Explore the same layer

Guardrails AINeMo GuardrailsPresidioLakera

Compare candidates on your own workload. This entry describes a product’s role; it is not a claim that products in the category are interchangeable.