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).

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.

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).

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:
- 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.
- 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 HateO2: Sexual ContentO3: Criminal PlanningO4: Guns and Illegal WeaponsO5: Regulated or Controlled SubstancesO6: 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 CrimesS2: Non-Violent CrimesS3: Sex-Related CrimesS4: Child Sexual ExploitationS5: DefamationS6: Specialized Advice (unlicensed medical, legal, or financial advice)S7: PrivacyS8: Intellectual PropertyS9: Indiscriminate Weapons (chemical, biological, radiological, nuclear, and high-yield explosive weapons such as cluster munitions)S10: HateS11: Suicide & Self-HarmS12: Sexual ContentS13: 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.

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 / Parameter | Llama Guard 1 | Llama Guard 3 |
|---|---|---|
| Base Architecture | Fine-tuned Llama 2 (7B) | Fine-tuned Llama 3.1 / 3.2 (1B, 8B, 11B-Vision) |
| Taxonomy Framework | Custom Meta Safety Taxonomy | Standardized MLCommons Hazard Framework + Tool Abuse |
| Hazard Count | 6 Categories (O1–O6) | 14 Categories (S1–S14) + Search Tool Moderation |
| Supported Languages | English | 8 Languages (EN, FR, DE, HI, IT, PT, ES, TH) |
| Context Length | 4K Tokens | 128K 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
{
"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:
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.

┌─────────────────────────────────┐
│ 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
- Llama Guard: LLM-based Input-Output Safeguard for Human-AI Conversations — arXiv
- Announcing Purple Llama: Towards open trust and safety in the new world of generative AI — Meta AI
- Introducing Llama 3.1: Our most capable models to date — Meta AI
- GitHub - meta-llama/PurpleLlama: Set of tools to assess and improve LLM security — GitHub
- meta-llama/Llama-Guard-3-8B — Hugging Face
- Llama Guard is now available in Amazon SageMaker JumpStart — AWS Machine Learning Blog
- llama-guard3 — Ollama Model Library
- Announcing Purple Llama — Simon Willison's Weblog