Skip navigation
Temperature, top-k and top-p, clearly explained!
0:00 / 0:28

Temperature, top-k and top-p, clearly explained!

Akshay ๐Ÿš€

@akshay_pachaar293K followers

Visit on X
327 likesDownloadOriginal post

16,127 views

Temperature, top-k and top-p, clearly explained! (how an LLM picks its next token) An LLM never picks a word directly. At every generation step, it gives a raw score to every token in its vocabulary. It then turns those raw scores into probabilities and samples one token. Temperature, top-k and top-p shape that sampling step. None of them touches the model's weights. Here's how a typical sampler applies them: โ†ณ Temperature: it rescales the raw scores. Low values sharpen the distribution, so a few tokens dominate. High values flatten it, so unlikely tokens get a real chance. โ†ณ Top-k: it keeps only the k most likely tokens. With top_k at 50, it drops every token ranked below 50, whatever the distribution looks like. โ†ณ Top-p: it keeps the smallest set of tokens whose probabilities add up to at least p. With top_p at 0.9, an obvious next token leaves only a few tokens, and an open-ended one leaves many. โ†ณ Sample: the sampler picks one token from the tokens that remain. The key difference: top-k always keeps the same number of tokens, while top-p adapts to how confident the model is. Low temperature doesn't make a model more correct, though. It only reduces randomness, so a model without the right context can still be confidently wrong. Good starting points for temperature: - Classification and extraction: 0.0 to 0.2 - Code generation: 0.0 to 0.3 - General chat: 0.5 to 0.8 - Brainstorming: 0.7 to 1.2 Tune temperature first, then add top-p to trim the long tail of unlikely tokens. While you evaluate, change one setting at a time instead of all three at once. The diagram below shows the complete flow. - Temperature reshapes the probabilities. - Top-k keeps a fixed number of tokens. - Top-p keeps just enough tokens to reach a set share of probability. I wrote a full breakdown of LLM inference in the article below.

16 Comments

Comments from the original post on X (Twitter)

  • Giacomo Masseroni's profile picture

    @giacomomasseron

    0.0 to 0.3 for code matches what i use. anything higher and it starts inventing method names

    1 like

  • Cozy's profile picture

    @Cozy2054934

    Clear framing that it never picks a word directly. For coding agents, do you keep temperature near 0 on edits and only raise it for brainstorming turns?

  • Dimri's profile picture

    @DimriLogic

    Nicely explained. Everyone talk, when we say hey how llm responds, silence, same on the temperature ๐ŸŒก. It's all about probability, do I want to give chance to all ideas - have high temperature n then have HIL I don't need noise, give me best response- Low temperature Another example- hey I am not getting right resume? Can be two-fold, too many or too less? Have moderate temperature

  • Crio Songo's profile picture

    @shuizhuyu

    Top-k vs top-p finally clicks for me.

  • JG ๐Ÿ‡ฎ๐Ÿ‡ช's profile picture

    @JamieGale7

    The bit people miss is top-p changes the shortlist size every token, top-k never does. which one do you default to for code?

  • Dipanshu Kushwaha's profile picture

    @Dipanshu_AI

    This is a great explanation of how LLMs work. It's fascinating to see the process behind how these models generate text.

  • jimerrors's profile picture

    @JMkernt

    top-k keeps a fixed 3, top-p stops at 0.9, that cat/mat example finally makes it click

  • Aakash Verma's profile picture

    @aakashverma

    The gotcha people hit in prod is stacking temperature with top-p until both are fighting each other. We should be picking one dial for exploration. Leave the other near default unless you measured the task.

  • NaLi's profile picture

    @storylove1206

    Excellent explanation of LLM sampling parameters.

  • Vini's profile picture

    @Viniai_

    Temperature, top-k, top-p explained.

  • Bรดng's profile picture

    @gaubong0808

    Excellent explanation of LLM token sampling.

  • Jason West's profile picture

    @JWestDigital

    This is the version I wish I'd had when people first asked me what temperature does. Sampling, not vibes ๐ŸŽฒ

  • Vesper๐Ÿช's profile picture

    @Vesper25251325

    Great explanation of LLM sampling parameters.

  • Patrick's profile picture

    @JourneyDeck

    Thank you so much for this. pstack is a lot to take in, but everyone keeps saying to install it. But then you don't even know everything it can do.

  • Emly's profile picture

    @emhai0909

    Excellent explanation of LLM sampling parameters. Clear and concise.

  • Hailu ๐Ÿ's profile picture

    @hoaihoai69232

    Excellent explanation of LLM sampling parameters. Clear and concise.