By the end of this lesson you will be able to:
- Trace how Meta's Llama models evolved and which sizes suit which jobs.
- Understand the key points of the Llama licence.
- Format a conversation with the Llama 3 chat template.
- Add safety with a guard model like Llama Guard, and run Llama models where you need them.
Why Llama matters
When Meta released LLaMA in early 2023, it kick-started the open-weights movement. Thousands of fine-tunes, tools and research projects grew from it. Today the Llama family covers tiny on-device models to very large frontier-class ones. Many open tools, including Ollama, llama.cpp and Hugging Face transformers, treat Llama as a first-class citizen.
The Llama timeline
Travel up the helix to see how the family grew:
Which Llama for which job?
- 1B–3B (Llama 3.2): on phones and edge devices for summarising, rewriting and simple tutoring offline (Lesson 19).
- 8B (Llama 3.1): laptops and single GPUs. A great base for fine-tuning (Lesson 18).
- 70B (Llama 3.1 / 3.3): high quality for most business and education tasks, on server GPUs or through cloud APIs.
- 405B (Llama 3.1): the largest dense Llama, used for the hardest tasks and to generate data or distil smaller models.
- Vision (Llama 3.2 11B/90B, Llama 4): questions about images, diagrams and screenshots.
The licence in brief
Llama models use Meta's own community licence, not a standard open-source licence. It allows free use, modification and commercial use for most organisations, with conditions: an acceptable-use policy, attribution requirements, and extra terms for companies with very large user bases. Read the exact licence for the version you use.
Not Apache 2.0. If your project or client requires a standard open-source licence, compare Llama's terms with Apache-licensed alternatives (Lesson 20) before you commit.
Talking to Llama: the chat template
Under the hood, chat messages are turned into one string with special tokens. Libraries and APIs usually do this for you, but you need it when running raw models or debugging. Llama 3 uses:
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are Vidya, a friendly tutor.<|eot_id|><|start_header_id|>user<|end_header_id|>
What is a prime number?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
The model continues from the final assistant header, and ends its reply with <|eot_id|> ("end of turn"). Using the wrong template is a classic cause of strange outputs from open models.
Safety: Llama Guard and friends
Meta publishes safety tools alongside its models. Llama Guard is a classifier model that labels a user prompt or a model response as safe or unsafe, and names the policy category it breaks. Prompt Guard detects jailbreak and injection attempts. They slot into the layered defences from Lessons 3 and 13:
Where to run Llama
- Locally:
ollama run llama3.2, LM Studio, or llama.cpp with GGUF files. - Hugging Face:
transformerswithmeta-llama/...model IDs. Accept the licence on the model page first. - Cloud: Azure AI Foundry, AWS Bedrock, Google Vertex AI and many inference providers offer Llama behind an API.
- On devices: the 1B and 3B models, quantized, for mobile apps.
Course wrap-up: you've built the whole stack
Over 21 lessons you went from "what is a token?" to agents, RAG, fine-tuning and open models. Here's the toolkit you now have:
- Foundations: tokens, next-token prediction, temperature and choosing models (1–2).
- Responsible and secure by design: harms, layered defences and red teaming (3, 13).
- Prompting: structure, few-shot, chain-of-thought and JSON (4–5).
- Building apps: text, chat, search, images and low-code (6–10).
- Going further: tools, UX, LLMOps and RAG (11–15).
- Models: open models, agents, fine-tuning, SLMs, Mistral and Llama (16–21).
- Next step: build a real project end to end. Pick a problem in your school, college or town, and ship it.
Hands-on
Your task: complete to_llama3(). It must start with <|begin_of_text|>, then for each message add <|start_header_id|>ROLE<|end_header_id|>, two newlines, the content and <|eot_id|>. Finally add the assistant header (with its two newlines) so the model knows it's its turn. Expected last line: Template OK: True.
messages = [
{"role": "system", "content": "You are Vidya, a friendly tutor."},
{"role": "user", "content": "What is a prime number?"},
]
def to_llama3(messages):
text = "<|begin_of_text|>"
# TODO: for each message add f"<|start_header_id|>{role}<|end_header_id|>\n\n{content}<|eot_id|>"
# TODO: then add the assistant header: "<|start_header_id|>assistant<|end_header_id|>\n\n"
return text
prompt = to_llama3(messages)
print(prompt)
expected_end = "What is a prime number?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
print("Template OK:", prompt.startswith("<|begin_of_text|><|start_header_id|>system") and prompt.endswith(expected_end))
Show solution
for m in messages:
text += f"<|start_header_id|>{m['role']}<|end_header_id|>\n\n{m['content']}<|eot_id|>"
text += "<|start_header_id|>assistant<|end_header_id|>\n\n"
Your task: fake_guard() imitates a guard model's output: either "safe" or "unsafe" followed by a category on the next line. Complete handle(): if the verdict's first line is "unsafe", return a kind refusal that mentions the category; otherwise return "OK: send to Llama". Expected last line: Blocked 2 of 5 messages.
def fake_guard(message):
m = message.lower()
if "hack" in m or "password" in m:
return "unsafe\nprivacy / cyber attack"
if "hurt" in m:
return "unsafe\nviolence"
return "safe"
def handle(message):
verdict = fake_guard(message)
# TODO: lines = verdict.split("\n"); if lines[0] == "unsafe": return f"Sorry, I can't help with that ({lines[1]})."
return "OK: send to Llama"
messages = [
"Explain the water cycle",
"How do I hack my school's wifi password?",
"Give me 3 MCQs on fractions",
"How to hurt someone without getting caught",
"What is Newton's second law?",
]
blocked = 0
for msg in messages:
result = handle(msg)
blocked += result.startswith("Sorry")
print(f"{msg[:42]:44} -> {result}")
print(f"Blocked {blocked} of {len(messages)} messages")
Show solution
lines = verdict.split("\n")
if lines[0] == "unsafe":
return f"Sorry, I can't help with that ({lines[1]})."
Check your understanding
You need an assistant on a mid-range phone that works offline. Which Llama fits best?
- Llama 3.1 405B
- Llama 3.2 1B or 3B, quantized
- Llama 3.3 70B
- Any Llama at 16-bit precision
The 1B and 3B models were designed for on-device use.
Your open Llama model produces odd, rambling outputs that never stop. A likely cause?
- The model is too small
- Temperature is 0
- Too many GPUs
- The wrong chat template, so it doesn't see proper turn markers
Models are trained on a specific template. Get it wrong and they lose track of turns and stopping points.
What does Llama Guard do?
- Classifies prompts and responses as safe or unsafe, with a policy category
- Encrypts the model weights
- Speeds up inference
- Translates prompts
It's a safety classifier you put before and after the main model.
Key takeaways
- Llama runs from 1B on-device models to 405B, plus vision and mixture-of-experts versions.
- The Llama licence is permissive for most uses but isn't standard open source, so read it.
- Use the right chat template, and add guard models for safety.
- You've finished the course. Now build something real!
🎉 Congratulations on completing Generative AI for Beginners! Want guided, real-world experience next? Explore our AI internships and other courses.
Your progress is saved in this browser.
Verified certificate
Make your skills count: unlock your verified certificate
The lessons are free. Unlock the certificate to prove what you've learned.
- A verified certificate with your name, the program and a unique ID
- Recruiters can check it online in seconds at aiprojects4u.com/verify
- Add it to your resume and LinkedIn to show hands-on Generative AI skills
- Covers what AI Engineer roles ask for: prompting, RAG, agents, fine-tuning and LLMOps
Secure payment via Razorpay
Adapted from Lesson 21 of Microsoft's "Generative AI for Beginners" (MIT License), rewritten and extended by Aiprojects4U. View source