Back to blog
Prompting
Claude Code

Understanding AI Model Parameters: Temperature, Top-K, and Top-P

Thang Doan
Thang Doan

When you send a prompt to an AI model, there are hidden settings that control how it picks its words. These parameters can change the answer you get, even with the exact same prompt. Here is what each one does in plain language.

Temperature: How Creative Should It Be?

Think of temperature as a dial between "predictable" and "creative."

  • Temperature 0, the model always picks the most likely next word. Very consistent, very boring. Good for factual tasks.
    Low temperature (0.1-0.3), mostly predictable with slight variation. Good for coding and analysis.
    High temperature (0.7-1.0), more random and creative. Good for brainstorming and storytelling.

Top-K: Narrowing the Choices

At every step, the model ranks all possible next words by probability. Top-K limits how many options it considers. Top-K of 1 means it only looks at the single most likely word. Top-K of 40 means it picks from the top 40 most likely words. A smaller number means safer, more predictable output.

Top-P: Probability Cutoff

Top-P works differently but achieves a similar goal. It adds up the probabilities of words from most to least likely, and stops when the total reaches the Top-P value (default is 0.95). So if the top few words already cover 95% of the probability, it ignores the rest. This is more flexible than Top-K because it adapts based on how confident the model is.

Max Output Tokens

This is simply a cap on how long the response can be. A token is roughly four characters, so 100 tokens is about 60-80 words. Set this to keep responses short when you do not need a long essay.

Stop Sequences

You can tell the model to stop generating when it hits a specific string of characters. For example, if you are generating a list and want it to stop after the last item, you could set "---" as a stop sequence. Just pick something that would not naturally appear in the response.

Quick guide: Need consistent, factual answers? Low temperature. Want creative ideas? Higher temperature. Want shorter responses? Lower max tokens. Most of the time, the defaults work fine, tweak these only when the output is not what you want.

Based on: Gemini API Prompting Strategies (ai.google.dev)

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

By subscribing you agree to receive the newsletter. See the Privacy page.