Tutorials / Prompting Meta AI and Llama Models / Parameters: temperature, top_p and max tokens

Parameters: temperature, top_p and max tokens

10 min read text
Course contents (20 lessons)
  1. Getting to know Meta AI and Llama
  2. What Meta AI is and where you can use it
  3. Meta AI versus Llama: the assistant and the models
  4. Your first chat with Meta AI
  5. Prompt basics that work
  6. The anatomy of a good prompt
  7. Context, tone, length and follow-ups
  8. Everyday uses
  9. Writing and replying to messages
  10. Captions, posts and bios
  11. Planning and study help
  12. Customer replies for a small business
  13. Images, groups and privacy
  14. Writing image generation prompts
  15. Group chats, etiquette and privacy
  16. Working with Llama models directly
  17. Running a Llama model on your own computer
  18. System prompts and chat templates
  19. Parameters: temperature, top_p and max tokens
  20. Few-shot examples and structured JSON output
  21. Advanced prompting
  22. Prompting Llama for code
  23. Retrieval-augmented prompting: answering from your own documents
  24. Evaluating and comparing answers
  25. Safety, limits and a final project
  26. Safety, limits and responsible use
  27. Final project: build a study buddy assistant

Why settings matter

A language model does not pick one fixed answer. At each step it calculates how likely each possible next word piece is, then chooses one. The settings control how that choice is made. You usually cannot change them in Meta AI, but you can in tools you run yourself, and you can set them when you call a model from code.

Temperature

Temperature controls how adventurous the choice is.

  • Low (close to 0): the model nearly always takes the most likely next piece. Answers are steady, predictable and repeatable.
  • Higher (around 1): the model sometimes takes less likely pieces. Answers are more varied, more creative, and more likely to wander or make errors.

Think of a student answering an exam. At low temperature they write the safest standard answer. At high temperature they try more original ideas, and sometimes get them wrong.

TaskStart with
Extracting data, JSON, classificationLow, close to 0
Code, factual questionsLow to medium, 0.2 to 0.5
Emails, summariesMedium, about 0.5 to 0.7
Brainstorming, stories, namesHigher, 0.8 to 1.0

These are starting points from my own use, not rules. Each tool has its own default, and good values depend on the model. Test with your task.

Top_p

Top_p, also called nucleus sampling, is another way to limit the choice. Instead of changing how bold the model is, it cuts off the unlikely options. With top_p at 0.9, the model chooses only among the most likely options that together make up 90 percent of the probability. Lower values mean a narrower, safer choice.

A common piece of advice, which I follow, is to change temperature or top_p, but not both at once. If you change two things you cannot tell which one did the work.

Max tokens

A token is a small piece of text, often part of a word. The maximum output setting limits how many tokens the model may write in one reply. If it is too low, the answer is cut off mid-sentence. If it is very high, it costs more time and, on paid services, more money. In Ollama this option is named num_predict, and other tools use names such as max_tokens. Check the name in your tool.

A rough rule for English is that a word is around one and a third tokens, but this varies by language and model. Urdu and other non-Latin scripts often use more tokens per word.

Setting them in Ollama

In a Modelfile:

FROM llama3.2
PARAMETER temperature 0.3
PARAMETER top_p 0.9
PARAMETER num_predict 300

This makes a calm model that writes at most 300 tokens.

An experiment you can run

Use this prompt three times at temperature 0.1, then three times at 1.0:

Give me one name for a coffee shop in Lahore. Reply with the name only.

At low temperature you will see the same or nearly the same name each time. At high temperature the names will differ more. Now try a factual question:

What is the capital of Australia? Reply with one word.

At low temperature the answer should be steady. At very high temperature, some models may get it wrong more often. This experiment shows what the setting does better than any explanation.

Other settings you will meet

  • top_k: only consider the K most likely options.
  • seed: a number that makes random choices repeatable in some tools.
  • Context length: how much text the model can hold at once, including your prompt and its reply. Long chats can fall out of it.
  • Repeat penalty: discourages repeating the same words.

Not every tool offers every setting, and names differ.

The common mistake

The common mistake is raising temperature to fix bad answers. Higher temperature does not make a model smarter. It makes it more random. If the answers are wrong, fix the prompt, add examples or use a larger model. Use temperature to tune style, not accuracy.

Try it yourself

Run the coffee shop experiment at three temperatures, three times each, and write all nine results in a small table. Then run a data task, such as "Reply with only the day of the week for 1 January 2000", at low and high temperature. Write one sentence on what you learned about each kind of task.

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