AI Hallucination: Why AI Makes Things Up and How to Reduce It
What is an AI hallucination? Learn why language models invent facts, how to spot it, and practical ways to reduce hallucinations in chats and AI agents.
What does LLM temperature do? A simple guide to temperature, top-p, max tokens, stop sequences and system prompts, and which settings to use for each task.
LLM temperature is a setting that controls how predictable or varied a language model’s output is. Low temperature makes the model stick to its most likely words, which suits facts and code. Higher temperature lets it pick less likely words, which suits brainstorming and creative writing.
Temperature is one of a handful of settings that shape how a model responds. This guide explains the common ones, including top-p, max tokens, stop sequences, and system prompts, in plain language, with practical advice on when to change them.
Every time a model writes, it predicts the next token (a small chunk of text) by assigning a probability to every option in its vocabulary. Then it has to choose one. For a refresher on that process, see how LLMs work.
The settings below mostly control that choice. Should the model always take the top option? Should it sometimes pick the second or tenth? How long should it keep going? Different answers suit different jobs.
Note that not every app or model exposes every setting. Chat apps often hide them, while developer APIs and local tools usually let you adjust them. Some models also restrict or ignore certain settings, so check the documentation of the tool you use.
Temperature reshapes the probabilities before the model picks a token.
A simple analogy: imagine asking a friend to finish the sentence “For breakfast I had…” At low temperature, they say “toast” every time. At a moderate temperature, you sometimes get “pancakes” or “leftover pizza.” At a very high temperature, you might get “a violin,” which is creative but not useful.
At or near zero, the model almost always picks its top choice, so answers become highly repeatable. In practice, small differences can still appear between runs because of how computations are carried out on the hardware and service. Treat low temperature as “very consistent,” not “guaranteed identical.”
Not exactly. Lower temperature reduces random detours, which can help with factual and technical work. But if the model’s most likely answer is wrong, low temperature will give you that wrong answer consistently. It is one tool against AI hallucinations, not a cure.
Top-p limits which tokens the model can choose from. Instead of considering every possible token, it takes the smallest group of top tokens whose combined probability reaches a threshold, called p, and samples only from that group.
Top-p and temperature both control variety, in different ways. A common piece of advice is to adjust one of them and leave the other at its default, so you can tell which change made the difference.
Some tools also offer top-k, which limits the choice to a fixed number of the most likely tokens, such as the top 40. It is a cruder version of the same idea as top-p.
Max tokens sets a ceiling on how long the response can be, measured in tokens. When the model hits it, generation simply stops, even mid-sentence.
Use it to:
Be careful not to set it too low for tasks that need room, like writing code or long reports. If answers keep cutting off, this is the first setting to check. Remember that both your input and the output must fit in the model’s overall limit, explained in tokens and context windows.
A stop sequence is a piece of text that tells the model to stop as soon as it produces it. For example, you might stop at a blank line or a specific marker when you only want one item from a list. Stop sequences are mostly useful when building apps that parse model output.
Some APIs provide penalties that discourage repetition:
They can help when output gets repetitive, but strong values can make text awkward. Most people can leave them at default.
A system prompt is a set of instructions given to the model before the conversation, usually hidden from the end user. It sets the role, tone, rules, and boundaries for every reply.
Examples of what a system prompt might say:
Unlike temperature, a system prompt changes what the model tries to do, not how randomly it picks words. In practice it is often the most powerful setting you have. Clear, specific instructions do more for quality than fine-tuning sampling numbers.
AI agents rely heavily on system prompts. They describe the agent’s tools, rules, and working style. To learn how agents use those instructions, see what is an AI agent.
There is no universal best value, and the right numbers differ between models. As a general direction:
| Task | Temperature | Other tips |
|---|---|---|
| Code, data extraction, factual Q&A | Low | Clear system prompt, enough max tokens for full output |
| Summaries and analysis | Low to moderate | Provide the source text in the prompt |
| General chat and explanations | Moderate (often the default) | Defaults usually work well |
| Brainstorming and creative writing | Moderate to high | Ask for several options at once |
| Structured output for an app | Low | Use stop sequences or a defined format |
When in doubt, start with the default settings, change one thing at a time, and compare results on a few real examples.
If you use a coding agent such as Claude Code or Codex, you usually do not tune temperature yourself. The tool picks sensible settings for reliable code. The choices that matter more there are which model to use and how much effort or reasoning it should spend.
BotQuarters follows the same idea for teams of agents: you choose the AI and effort level per role, and a spending level per company, rather than tweaking sampling numbers.
Temperature is a setting that controls randomness when a model picks its next token. Low values make output focused and consistent, while higher values make it more varied and creative, and very high values can make it incoherent.
A low temperature is generally best for code, because you want the model’s most reliable choices rather than creative variations. Many coding tools already set this for you, so you often do not need to change it.
Temperature reshapes the probabilities of all possible tokens, making the choice sharper or flatter. Top-p limits the choice to the smallest set of top tokens that reach a set probability. Both affect variety, so it is usually best to adjust only one at a time.
Max tokens caps how many tokens the model may generate in one reply. When the limit is reached, the output stops, even mid-sentence. It is useful for controlling length and cost, but setting it too low cuts answers short.
What is an AI hallucination? Learn why language models invent facts, how to spot it, and practical ways to reduce hallucinations in chats and AI agents.
How do LLMs work? A plain explanation of tokens, training, attention and next-token prediction, with no math, plus what it means for using AI tools well.
Open source LLM or closed model? Learn what open-weight really means, why licenses vary, and the trade-offs in privacy, cost, quality and control.