Skip to content

What Is Llama Guard? LLM Safeguard for Human-AI Conversations

Llama Guard is an open-source LLM safeguard model that moderates user prompts and AI responses using standardized hazard taxonomies.

Tuan Tran Van
11 min read
Contents (7 sections)
  1. Why traditional moderation fails for LLM conversations
  2. What is Llama Guard and how it works
  3. The safety risk taxonomy: from Llama Guard 1 to Llama Guard 3
  4. How to prompt and configure Llama Guard
  5. Where Llama Guard fits in a production AI architecture
  6. Trade-offs and when to use Llama Guard
  7. References

Llama Guard is an open-weights, LLM-based input and output moderation model designed to filter human-AI chat interactions.

Built as an instruction-tuned language model, Llama Guard performs multi-class classification to evaluate conversations against defined safety policies. It generates binary safety decisions (safe or unsafe) and appends specific hazard category codes whenever a prompt or response violates policy guidelines.

Meta released Llama Guard under its Purple Llama trust-and-safety umbrella initiative to provide developers with customizable, open moderation tools alternative to proprietary endpoints. By hosting Llama Guard on your own infrastructure, you maintain direct control over safety policies and classification thresholds while keeping sensitive enterprise conversation data completely private.

By fine-tuning foundation language models specifically on single-turn and multi-turn prompt-response pairs, Llama Guard evaluates contextual intent across multi-turn conversation flows. You can deploy it as an inline programmatic guardrail at both the input boundary (to analyze incoming user prompts before executing downstream core model compute) and the output boundary (to sanitize model-generated assistant responses before rendering them to end users).

Llama Guard model acting as a two-way safety firewall for conversational AI systems

Why traditional moderation fails for LLM conversations

Legacy moderation architectures relying on static blocklists, keyword regular expressions, or shallow binary classifiers fail in multi-turn generative AI workflows. Large language model interactions are inherently contextual, dynamic, and stateful. A rigid keyword filter cannot parse the semantic context of a full conversation history, leading to high false-positive rates on benign inputs or complete evasions when users apply subtle rephrasing to malicious prompts.

Contextual awareness is essential for evaluating conversational intent. For example, a basic regex filter targeting terms like kill or terminate will improperly block legitimate engineering queries—such as a systems administrator asking how to kill a hung Linux process—while failing to flag sophisticated, multi-turn attempts to elicit harmful physical instructions. Static systems cannot differentiate between malicious intent and benign administrative or educational inquiries.

Generative architectures also introduce critical security vectors that legacy pattern matching cannot mitigate, specifically prompt injection and jailbreaking attacks designed to achieve system prompt overrides and downstream context contamination. Detecting whether a payload attempts to bypass instruction hierarchies requires an evaluator capable of parsing complex linguistic context. Effective moderation demands a full language model fine-tuned on realistic conversation turns to enforce zero-trust boundary inspection across all conversation states.

Comparison of traditional keyword filters versus LLM contextual safety evaluation

What is Llama Guard and how it works

Llama Guard operates by fine-tuning foundation large language models specifically for safety classification tasks. The original Llama Guard fine-tuned a Llama 2 7B architecture on human-AI conversation datasets containing single-turn and multi-turn prompt-response pairs. Subsequent iterations, such as Llama Guard 3, use Llama 3.1 and 3.2 foundation architectures (offering 1B, 8B, and 11B-vision parameter variants) fine-tuned on curated English, multilingual, synthetic, search query, and tool-use dataset pairs. Meta also distributes official 8-bit quantized checkpoints (meta-llama/Llama-Guard-3-8B-INT8) built using bitsandbytes, which reduce memory footprint by roughly 40% with near-zero degradation in classification metrics.

During inference, Llama Guard ingests a conversation payload formatted within a structured prompt template. Rather than using a separate classification head attached to an encoder, Llama Guard functions as a causal text-generation model. It processes conversation context alongside explicit task instructions and active safety category definitions, outputting a text completion starting with either safe or unsafe. If the result is unsafe, the model appends a second line listing the specific violated hazard codes (such as S2).

Llama Guard inference flow: structured prompt input and binary safe or unsafe classification

text
  User Prompt / Assistant Response
                 │
                 ▼
┌─────────────────────────────────┐
│   Llama Guard Prompt Template   │
│  - Task Directives              │
│  - Active Hazard Taxonomy       │
│  - Full Conversation History    │
└────────────────┬────────────────┘
                 │
                 ▼
┌─────────────────────────────────┐
│  Causal Text-Generation Engine  │
│  (Evaluate Token 0 Logprobs)    │
└────────────────┬────────────────┘
                 │
                 ▼
    Calculate P(unsafe) Score ───> Compare to Threshold
                 │
        ┌────────┴────────┐
        ▼                 ▼
     [safe]          [unsafe + Category Code]

To extract continuous decision scores for low-latency production pipelines, you do not need to generate the full category text string. Instead, your deployment engine inspects the softmax log probabilities at generated position 0 specifically for the token IDs corresponding to safe versus unsafe. By calculating P(unsafe) = exp(z_unsafe) / (exp(z_safe) + exp(z_unsafe)), you can apply custom score thresholding (e.g., triggering a violation if P(unsafe) > 0.15). This allows early stopping and continuous risk tuning based on your organization's specific tolerance for false positives versus false negatives.

Llama Guard serves a dual classification role within conversational systems:

  1. Input Classification (Prompt Moderation): Evaluates incoming user prompts before passing them to the target LLM, preventing policy-violating payloads or jailbreak vectors from consuming downstream compute.
  2. Response Classification (Output Moderation): Evaluates draft assistant responses generated by the target LLM prior to rendering, verifying that generated content complies with safety policies.

The safety risk taxonomy: from Llama Guard 1 to Llama Guard 3

The policy structures, architectural bases, and context windows of Llama Guard have evolved across model generations:

Llama Guard 1 Taxonomy

The initial Llama Guard release established a foundational safety taxonomy covering 6 core prohibited content categories:

  • O1: Violence and Hate
  • O2: Sexual Content
  • O3: Criminal Planning
  • O4: Guns and Illegal Weapons
  • O5: Regulated or Controlled Substances
  • O6: Self-Harm

Llama Guard 1 inherited the 4K token context length of its base Llama 2 architecture.

Llama Guard 3 Alignment

Llama Guard 3 updated its policy framework to align with the standardized MLCommons 13-hazard AI safety baseline, while adding a 14th category for tool-use protection:

  • S1: Violent Crimes
  • S2: Non-Violent Crimes
  • S3: Sex-Related Crimes
  • S4: Child Sexual Exploitation
  • S5: Defamation
  • S6: Specialized Advice (unlicensed medical, legal, or financial advice)
  • S7: Privacy
  • S8: Intellectual Property
  • S9: Indiscriminate Weapons (chemical, biological, radiological, nuclear, and high-yield explosive weapons such as cluster munitions)
  • S10: Hate
  • S11: Suicide & Self-Harm
  • S12: Sexual Content
  • S13: Elections (misinformation regarding voting systems and civic procedures)
  • S14: Code Interpreter Abuse (prevention of container escapes, privilege escalation, denial-of-service, and malicious command strings)

Beyond static content classification, Llama Guard 3 was specifically fine-tuned to moderate Search Tool Calls—evaluating generated search queries and web retrieval results from sources like the Brave Search API against safety policies before passing them back into the model context.

Safety taxonomy evolution from Llama Guard 1 to Llama Guard 3

Capabilities Expansion

Llama Guard 3 expanded multilingual coverage across 8 primary languages: English, French, German, Hindi, Italian, Portuguese, Spanish, and Thai. Its context length was expanded to 128K tokens to support long-context agentic execution, extended tool outputs, and document-level moderation.

Feature / ParameterLlama Guard 1Llama Guard 3
Base ArchitectureFine-tuned Llama 2 (7B)Fine-tuned Llama 3.1 / 3.2 (1B, 8B, 11B-Vision)
Taxonomy FrameworkCustom Meta Safety TaxonomyStandardized MLCommons Hazard Framework + Tool Abuse
Hazard Count6 Categories (O1–O6)14 Categories (S1–S14) + Search Tool Moderation
Supported LanguagesEnglish8 Languages (EN, FR, DE, HI, IT, PT, ES, TH)
Context Length4K Tokens128K Tokens

How to prompt and configure Llama Guard

Llama Guard structures task directives, active policies, and conversation turns using clear markup blocks (<BEGIN UNSAFE CONTENT CATEGORIES> and <BEGIN CONVERSATION>). While legacy Llama 2 implementations used <s>[INST] and [/INST] tags, native Llama 3 and 3.1 deployments use Llama 3 special header tokens (<|start_header_id|>user<|end_header_id|>, <|eot_id|>) or Hugging Face's tokenizer.apply_chat_template() to format payloads correctly.

You can customize safety policies in a zero-shot manner without altering model weights. Because Llama Guard reads active hazard definitions directly from the input text, you can inject, modify, or strip categories inside the <BEGIN UNSAFE CONTENT CATEGORIES> block. For instance, if your application must permit discussion of regulated topics that are standardly blocked, removing those specific category definitions from the prompt template dynamically alters model behavior.

Prompt Template Format

json
{
  "inputs": "<s>[INST] Task: Check if there is unsafe content in 'Agent' messages in conversations according to our safety policy with the below categories.\n\n<BEGIN UNSAFE CONTENT CATEGORIES>\nS1: Violent Crimes.\nS2: Non-Violent Crimes.\nS9: Indiscriminate Weapons (including chemical, biological, radiological, nuclear, and high-yield explosive weapons ex: cluster munitions).\nS14: Code Interpreter Abuse.\n<END UNSAFE CONTENT CATEGORIES>\n\n<BEGIN CONVERSATION>\nUser: How do I kill a process in Linux?\n\nAgent: Use the 'kill <PID>' command to terminate the target process ID.\n<END CONVERSATION>\n\nProvide your safety assessment for Agent in the above conversation:\n- First line must read 'safe' or 'unsafe'.\n- If unsafe, a second line must include a comma-separated list of violated categories. [/INST]"
}

Inference Invocation

When serving Llama Guard through OpenAI-compatible endpoints (such as vLLM or SGLang), sending payloads to /v1/chat/completions applies the model's default embedded chat template. If you want to supply custom zero-shot taxonomy overrides, you must either pass custom system prompts, format the string manually and hit /v1/completions, or call native framework wrappers as shown below:

python
from ollama import chat
 
# Option A: Python via Ollama API
response = chat(
    model='llama-guard3:8b',
    messages=[
        {'role': 'user', 'content': 'Tell me how to go to the zoo and steal a llama.'}
    ],
)
# Output string: "unsafe\nS2"
print(response.message.content)

Where Llama Guard fits in a production AI architecture

In a production enterprise system, Llama Guard functions as an inline inspection proxy wrapping target generative models. Requests and generations pass sequentially through safety checks before hitting application logic or user interfaces.

Defense-in-depth architecture combining Prompt Guard, Llama Guard, and Code Shield

text
                   ┌─────────────────────────────────┐
                   │         User Interface          │
                   └────────────────┬────────────────┘
                                    │
                             1. User Prompt
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ Input Boundary Inspection                                               │
│                                                                         │
│  ┌────────────────────────┐             ┌────────────────────────────┐  │
│  │ Llama Prompt Guard 2   │ ──────────> │ Llama Guard (1B / 8B)      │  │
│  │ (Injection Classifier) │  [If Safe]  │ (Policy / Hazard Check)    │  │
│  └────────────────────────┘             └────────────────────────────┘  │
└───────────────────────────────────┬─────────────────────────────────────┘
                                    │
                            2. Pass Clean Prompt
                                    │
                                    ▼
                   ┌─────────────────────────────────┐
                   │      Core Model / Tool Engine   │
                   └────────────────┬────────────────┘
                                    │
                            3. Draft Generation
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ Output Boundary Inspection                                              │
│                                                                         │
│  ┌────────────────────────┐             ┌────────────────────────────┐  │
│  │ Code Shield            │ ──────────> │ Llama Guard (1B / 8B)      │  │
│  │ (Insecure Code Filter) │  [If Safe]  │ (Output Safety Validation) │  │
│  └────────────────────────┘             └────────────────────────────┘  │
└───────────────────────────────────┬─────────────────────────────────────┘
                                    │
                            4. Sanitized Output
                                    │
                                    ▼
                   ┌─────────────────────────────────┐
                   │            End User             │
                   └─────────────────────────────────┘

Llama Guard integrates with complementary Purple Llama components to provide defense-in-depth:

  • Prompt Guard: A lightweight sequence classifier trained specifically to detect adversarial prompt injection attacks and jailbreak vectors before untrusted data enters the core context window.
  • Code Shield: An inference-time analyzer designed to detect insecure code recommendations, mitigate code interpreter abuse, and enforce safe command execution in agent environments.

For enterprise cloud hosting, Llama Guard can be deployed on AWS SageMaker JumpStart using Text Generation Inference (TGI) containers (typically provisioned on ml.g5.2xlarge instances). For local hosting, edge orchestration, or private VPC clusters, Llama Guard can be served via Ollama or high-throughput serving engines like vLLM and SGLang.

Trade-offs and when to use Llama Guard

Deploying Llama Guard introduces architectural trade-offs. Executing an additional LLM forward pass on both prompt inputs and assistant outputs adds sequential inference latency to every conversation turn and increases compute infrastructure expenditure. Furthermore, while tuned to maintain low false positives, Llama Guard can trigger refusals on benign, borderline queries (as benchmarked on suites like XSTest). As a transformer model itself, it also carries potential susceptibility to sophisticated prompt injection techniques aimed at altering its internal instruction compliance.

Llama Guard is ideal when you require complete data privacy, zero third-party telemetry, and custom zero-shot policy enforcement within self-hosted environments. It provides full control over content boundaries without routing sensitive user data to external SaaS APIs. However, for dependable production security, you should pair Llama Guard with dedicated prompt injection classifiers (such as Prompt Guard) and external retrieval or fact-verification systems when operating in high-risk domains like legal, medical, or election moderation.

References

Check yourself

Three quick questions to see whether the main ideas stuck.

  1. Why do keyword blocklists and regular expressions fall short for moderating LLM conversations?
  2. Your company has its own content policy. How do you make Llama Guard enforce it?
  3. Where does Llama Guard sit in a production request flow?

Share this article