Prompting Llama for code
Course contents (20 lessons)
- Getting to know Meta AI and Llama
- What Meta AI is and where you can use it
- Meta AI versus Llama: the assistant and the models
- Your first chat with Meta AI
- Prompt basics that work
- The anatomy of a good prompt
- Context, tone, length and follow-ups
- Everyday uses
- Writing and replying to messages
- Captions, posts and bios
- Planning and study help
- Customer replies for a small business
- Images, groups and privacy
- Writing image generation prompts
- Group chats, etiquette and privacy
- Working with Llama models directly
- Running a Llama model on your own computer
- System prompts and chat templates
- Parameters: temperature, top_p and max tokens
- Few-shot examples and structured JSON output
- Advanced prompting
- Prompting Llama for code
- Retrieval-augmented prompting: answering from your own documents
- Evaluating and comparing answers
- Safety, limits and a final project
- Safety, limits and responsible use
- Final project: build a study buddy assistant
What to expect from a model on code
Llama models can write, explain and fix code, and Meta has released code-focused variants too. Quality depends on the model size and version. Larger models do better on complex tasks. Small local models are fine for short functions, regular expressions, SQL queries and explaining errors, but they will often get subtle things wrong.
The right attitude is the one you have toward a quick junior colleague: helpful, fast, and in need of review. You are responsible for the code you ship.
Give the facts a developer would need
Compare these two requests.
Write a function to parse dates.
Write a Python 3.11 function parse_invoice_date(text: str) -> datetime.date.
Input examples: "2026-03-08", "08/03/2026" (day first), "8 Mar 2026".
It must raise ValueError with a clear message for anything else.
Use only the standard library. Include type hints and a short docstring.
After the code, give 6 unit tests using pytest, including two that should fail.
Why the second works
It states the language and version, the function name and signature, real input examples, error behaviour, library limits and a request for tests. The ambiguity about "08/03/2026" is settled with "day first". Without that, the model picks month first, and you find out in production.
Ask for reasoning before code on harder tasks
I need to remove duplicate customers from a list of 2 million rows in a CSV. Memory is limited to 2 GB.
First, describe two approaches and their trade-offs in five lines. Then implement the better one in Python. Do not load the entire file into memory.
Asking for a short comparison first makes the model think about constraints. A model that jumps directly to code often writes the first approach it recalls, even if it breaks the memory limit.
Debugging prompts
Give the error and the code, and say what you expected.
This Python code should print the total of each order, but it prints the same total for every order.
Code:
[paste the 15 relevant lines]
Output I get: 120, 120, 120
Output I expect: 120, 45, 300
Explain the cause in two sentences, then show the corrected lines only.
Keep the pasted code short. Include only what is needed. Long pastes use up the context and can bury the bug. Never paste code that contains keys, passwords or private customer data.
Explaining and converting
Explain this SQL query line by line for a beginner. Then rewrite it so it is easier to read, without changing the result.
Explanations are a good use, because you can read the code yourself and see if the explanation fits. This catches errors faster than reading the code alone.
Verify, always
- Read it. If you cannot explain a line, ask the model, or leave it out.
- Run it. Test with normal input, empty input and bad input.
- Check that libraries and functions exist. Models sometimes invent a function or option that sounds right. If an import fails, the model probably invented it.
- Check security. Look for string-built SQL, unchecked user input, hard-coded secrets and unsafe file paths.
Settings for code
Use a low temperature, around 0.2. Increase the max output length so that code is not cut off, since a half-finished function is worse than none. If the model keeps writing explanations when you only want code, say "Return only the code block, no commentary."
The common mistake
The common mistake is copying generated code into a project without running it. The code often looks convincing, and that is the danger. Treat a clean-looking answer as a hypothesis, then test it.
Try it yourself
Choose a small utility you could use, such as a function that cleans phone numbers into one format. Write the prompt with language, signature, examples, error rules and a request for tests. Run the tests. Find at least one case the model missed, add it to the prompt, and ask for a corrected version.
This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.
Sign up free I already have an account