{"id":244,"date":"2026-10-26T13:18:13","date_gmt":"2026-10-26T13:18:13","guid":{"rendered":"https:\/\/devdojo.co.in\/?p=244"},"modified":"2026-10-26T13:18:14","modified_gmt":"2026-10-26T13:18:14","slug":"genai-series-part-5-temperature-top-p","status":"publish","type":"post","link":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/","title":{"rendered":"Generative AI Part 5: Temperature and Top-p for Controlling Model Output"},"content":{"rendered":"\n<div class=\"dd-post\">\n<style>\n.single article:has(.dd-hero) .entry-media{display:none}\n.dd-post{--navy:#0b1020;--blue:#3178c6;--violet:#7c5cff;--ink:#1f2433;--muted:#5b6275;--line:#e3e6ef;font-size:17px;line-height:1.75;color:var(--ink)}\n.dd-post h2{margin:2.2em 0 .6em;font-size:1.6em;line-height:1.3;scroll-margin-top:90px}\n.dd-post h3{margin:1.6em 0 .5em;font-size:1.22em;scroll-margin-top:90px}\n.dd-post a{color:var(--blue);text-decoration:underline;text-underline-offset:3px}\n.dd-post pre{background:#0f1430;color:#e6e9f5;padding:18px 20px;border-radius:12px;overflow-x:auto;line-height:1.6;border:1px solid #232a52;margin:1.2em 0;font-size:14.5px !important}\n.dd-post pre code{font-size:1em !important;background:none;color:inherit;padding:0;font-family:\"JetBrains Mono\",\"Fira Code\",Consolas,Menlo,monospace;white-space:pre}\n.dd-post :not(pre)>code{background:#eef1fb;color:#2b3a8f;padding:2px 6px;border-radius:5px;font-size:.9em}\n.dd-post .dd-file{display:inline-block;background:#151b3b;color:#c9d1ff;font:600 13px\/1 \"JetBrains Mono\",Consolas,monospace;padding:7px 12px;border-radius:8px 8px 0 0;margin:1em 0 -1.2em;position:relative}\n.dd-post .dd-hero{margin:0 0 1.6em;border-radius:16px;overflow:hidden;box-shadow:0 10px 30px rgba(11,16,32,.25)}\n.dd-post .dd-hero svg{display:block;width:100%;height:auto}\n.dd-post .dd-hero .node{transition:transform .25s,filter .25s;transform-box:fill-box;transform-origin:center;cursor:pointer}\n.dd-post .dd-hero .node:hover{transform:scale(1.08);filter:drop-shadow(0 0 10px #7c5cff)}\n.dd-post .dd-series{display:inline-flex;align-items:center;gap:8px;background:#151b3b;color:#dfe3ff;border-radius:999px;padding:6px 14px;font-size:.88em;font-weight:600;margin:0 0 .8em}\n.dd-post .dd-series span{background:var(--violet);color:#fff;border-radius:999px;padding:2px 10px}\n.dd-post .dd-prereq{font-size:.96em;color:var(--muted);border-left:3px solid var(--line);padding-left:12px;margin:.6em 0 1.2em}\n.dd-post .dd-summary{background:linear-gradient(135deg,#eef3ff,#f4efff);border:1px solid #d6dcf5;border-left:5px solid var(--violet);border-radius:12px;padding:18px 22px;margin:1.4em 0}\n.dd-post .dd-summary strong.lbl{display:block;font-size:1.05em;margin-bottom:6px;color:#3a2fa0}\n.dd-post .dd-summary ul{margin:.3em 0 0 1.1em}\n.dd-post .dd-toc{background:#f8f9fc;border:1px solid var(--line);border-radius:12px;padding:16px 22px;margin:1.4em 0}\n.dd-post .dd-toc ol{margin:.4em 0 0 1.2em;columns:2;column-gap:28px}\n.dd-post .dd-toc li{break-inside:avoid;margin:.15em 0}\n@media(max-width:640px){.dd-post .dd-toc ol{columns:1}}\n.dd-post .dd-tip,.dd-post .dd-note,.dd-post .dd-warn{border-radius:10px;padding:14px 18px;margin:1.3em 0}\n.dd-post .dd-tip{background:#ecfbf3;border-left:4px solid #1f9d61}\n.dd-post .dd-note{background:#eef4ff;border-left:4px solid var(--blue)}\n.dd-post .dd-warn{background:#fff6e8;border-left:4px solid #e08a00}\n.dd-post .dd-cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(210px,1fr));gap:14px;margin:1.3em 0}\n.dd-post .dd-card{border:1px solid var(--line);border-radius:12px;padding:14px 16px;background:#fff;box-shadow:0 2px 8px rgba(20,30,70,.05)}\n.dd-post .dd-card b{display:block;color:#3a2fa0;margin-bottom:4px}\n.dd-post table{width:100%;border-collapse:collapse;margin:1.3em 0;font-size:.95em}\n.dd-post th,.dd-post td{border:1px solid var(--line);padding:10px 12px;text-align:left;vertical-align:top}\n.dd-post th{background:#151b3b;color:#fff}\n.dd-post tr:nth-child(even) td{background:#f8f9fc}\n.dd-post .dd-tablewrap{overflow-x:auto}\n.dd-post figure{margin:1.6em 0;text-align:center}\n.dd-post figure svg{width:100%;height:auto;border-radius:12px}\n.dd-post figcaption{font-size:.9em;color:var(--muted);margin-top:8px}\n.dd-post details{border:1px solid var(--line);border-radius:10px;padding:12px 16px;margin:.7em 0;background:#fff}\n.dd-post details summary{cursor:pointer;font-weight:600}\n.dd-post details[open] summary{margin-bottom:8px;color:#3a2fa0}\n.dd-post .dd-roadmap{border:2px solid #d6dcf5;background:#fbfbff}\n.dd-post .dd-roadmap h4{margin:1em 0 .3em;color:#151b3b;font-size:1em;text-transform:uppercase;letter-spacing:.04em}\n.dd-post .dd-roadmap ol{margin:.2em 0 .6em 1.4em;padding:0}\n.dd-post .dd-roadmap li{margin:.2em 0}\n.dd-post .dd-roadmap .here{background:var(--violet);color:#fff;border-radius:999px;padding:1px 9px;font-size:.8em;font-weight:700;margin-left:6px}\n.dd-post .dd-roadmap .soon{color:#8a90a6;font-size:.82em;margin-left:6px}\n.dd-post .dd-tabs{border:1px solid var(--line);border-radius:12px;padding:6px 16px 16px;margin:1.4em 0;background:#fbfbfe}\n.dd-post .dd-tabs>input{position:absolute;opacity:0;pointer-events:none}\n.dd-post .dd-tabs .labels label{display:inline-block;padding:8px 16px;margin:10px 6px 0 0;border-radius:999px;border:1px solid #cfd5ea;cursor:pointer;font-weight:600;font-size:.95em;background:#fff;transition:background .2s}\n.dd-post .dd-tabs .labels label:hover{border-color:var(--violet)}\n.dd-post .dd-tabs .panel{display:none;margin-top:12px}\n.dd-post .dd-tabs #ti1:checked~.labels label[for=ti1],.dd-post .dd-tabs #ti2:checked~.labels label[for=ti2],.dd-post .dd-tabs #ti3:checked~.labels label[for=ti3],.dd-post .dd-tabs #ti4:checked~.labels label[for=ti4]{background:var(--violet);color:#fff;border-color:var(--violet)}\n.dd-post .dd-tabs #ti1:checked~.p1,.dd-post .dd-tabs #ti2:checked~.p2,.dd-post .dd-tabs #ti3:checked~.p3,.dd-post .dd-tabs #ti4:checked~.p4{display:block}\n.dd-post .dd-tabs #ti1:focus-visible~.labels label[for=ti1],.dd-post .dd-tabs #ti2:focus-visible~.labels label[for=ti2],.dd-post .dd-tabs #ti3:focus-visible~.labels label[for=ti3],.dd-post .dd-tabs #ti4:focus-visible~.labels label[for=ti4]{outline:2px solid var(--blue);outline-offset:2px}\n.dd-post .dd-cta{background:linear-gradient(135deg,#0b1020,#151b3b);color:#e8ebff;border-radius:16px;padding:24px 26px;margin:2em 0;text-align:center}\n.dd-post .dd-cta h3{color:#fff;margin-top:0}\n.dd-post .dd-cta a{color:#a99bff}\n.dd-post .dd-vsc{position:relative;margin:1.5em 0;border-radius:12px;overflow:hidden;background:#1e1e1e;border:1px solid #2b2b2b;box-shadow:0 14px 34px rgba(0,0,0,.28);font-family:\"Segoe UI\",system-ui,sans-serif}\n.dd-post .dd-vsc>input{position:absolute;opacity:0;pointer-events:none}\n.dd-post .dd-vsc-bar{display:flex;align-items:center;gap:12px;background:#323233;color:#cccccc;font-size:12.5px;padding:8px 12px}\n.dd-post .dd-dots{display:inline-flex;gap:7px}.dd-post .dd-dots i{width:12px;height:12px;border-radius:50%;background:#ff5f57;display:block}.dd-post .dd-dots i:nth-child(2){background:#febc2e}.dd-post .dd-dots i:nth-child(3){background:#28c840}\n.dd-post .dd-vsc-title{flex:1;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding-right:50px;opacity:.85}\n.dd-post .dd-vsc-tabs{display:flex;align-items:stretch;background:#252526;border-bottom:1px solid #1e1e1e;overflow-x:auto}\n.dd-post .dd-vsc-tabs label{display:flex;align-items:center;gap:7px;padding:9px 16px;color:#969696;font-size:13px;cursor:pointer;background:#2d2d2d;border-right:1px solid #252526;border-top:1px solid transparent;white-space:nowrap;margin:0;transition:color .15s}\n.dd-post .dd-vsc-tabs label:hover{color:#e0e0e0}\n.dd-post .dd-vsc-tabs .ic{font:700 9.5px\/1 Consolas,monospace;padding:3px 4px;border-radius:3px;color:#1e1e1e}\n.dd-post .ic.py{background:#ffd43b}.dd-post .ic.js{background:#f1e05a}.dd-post .ic.sh{background:#89e051}\n.dd-post .dd-vsc-tabs label.run{margin-left:auto;background:transparent;border:0;color:#89d185;font-weight:600;display:none}\n.dd-post .dd-vsc-tabs label.run:hover{color:#b5f0b0;background:#2a2d2e}\n.dd-post .dd-vsc-pane{display:none}\n.dd-post .dd-vsc>.f1:checked~.dd-vsc-tabs .t1,.dd-post .dd-vsc>.f2:checked~.dd-vsc-tabs .t2,.dd-post .dd-vsc>.f3:checked~.dd-vsc-tabs .t3{background:#1e1e1e;color:#ffffff;border-top-color:#3178c6}\n.dd-post .dd-vsc>.f1:checked~.p1,.dd-post .dd-vsc>.f2:checked~.p2,.dd-post .dd-vsc>.f3:checked~.p3{display:block}\n.dd-post .dd-vsc>.f1:checked~.dd-vsc-tabs .run1,.dd-post .dd-vsc>.f2:checked~.dd-vsc-tabs .run2,.dd-post .dd-vsc>.f3:checked~.dd-vsc-tabs .run3{display:flex}\n.dd-post .dd-vsc>.f1:focus-visible~.dd-vsc-tabs .t1,.dd-post .dd-vsc>.f2:focus-visible~.dd-vsc-tabs .t2,.dd-post .dd-vsc>.f3:focus-visible~.dd-vsc-tabs .t3,.dd-post .dd-vsc>input[type=checkbox]:focus-visible~.dd-vsc-tabs .run{outline:2px solid #3178c6;outline-offset:-2px}\n.dd-post .dd-vsc-crumbs{background:#1e1e1e;color:#a9a9a9;font-size:12px;padding:5px 16px 2px}\n.dd-post .dd-vsc pre.dd-vsc-code{background:#1e1e1e;color:#d4d4d4;border:0;border-radius:0;margin:0;padding:8px 0 14px;counter-reset:ln;line-height:1.62;overflow-x:auto}\n.dd-post .dd-vsc pre code{font-family:Consolas,\"JetBrains Mono\",\"Fira Code\",Menlo,monospace;color:inherit;background:none;white-space:pre}\n.dd-post .dd-vsc .l{display:inline-block;min-width:100%;padding-right:18px;box-sizing:border-box}\n.dd-post .dd-vsc .l::before{counter-increment:ln;content:counter(ln);display:inline-block;width:3.2em;padding-right:1.4em;text-align:right;color:#6e7681;user-select:none;-webkit-user-select:none}\n.dd-post .dd-vsc .l:hover{background:#2a2d2e}.dd-post .dd-vsc .l:hover::before{color:#c6c6c6}\n.dd-post .dd-vsc .k,.dd-post .dd-vsc .kn{color:#c586c0}.dd-post .dd-vsc .kc{color:#569cd6}.dd-post .dd-vsc .s{color:#ce9178}.dd-post .dd-vsc .c{color:#6a9955}.dd-post .dd-vsc .m{color:#b5cea8}.dd-post .dd-vsc .nf{color:#dcdcaa}.dd-post .dd-vsc .nc{color:#4ec9b0}.dd-post .dd-vsc .nb{color:#4ec9b0}.dd-post .dd-vsc .nd{color:#dcdcaa}.dd-post .dd-vsc .n{color:#9cdcfe}.dd-post .dd-vsc .o{color:#d4d4d4}.dd-post .dd-vsc .gp{color:#89d185;user-select:none;-webkit-user-select:none}\n.dd-post .dd-vsc-term{display:none;border-top:1px solid #3c3c3c;background:#181818}\n.dd-post .dd-vsc>.r1:checked~.p1 .dd-vsc-term,.dd-post .dd-vsc>.r2:checked~.p2 .dd-vsc-term,.dd-post .dd-vsc>.r3:checked~.p3 .dd-vsc-term{display:block;animation:ddterm .35s ease-out}\n@keyframes ddterm{from{opacity:0;transform:translateY(6px)}to{opacity:1;transform:none}}\n.dd-post .dd-term-head{display:flex;gap:18px;align-items:center;font-size:11px;letter-spacing:.06em;color:#8b8b8b;padding:7px 16px 0}\n.dd-post .dd-term-head span:first-child{color:#e7e7e7;border-bottom:1px solid #e7e7e7;padding-bottom:4px}\n.dd-post .dd-term-head .dd-term-note{margin-left:auto;letter-spacing:0;text-transform:none;color:#d7ba7d;border:0;font-size:11.5px}\n.dd-post .dd-vsc-term pre{background:#181818;color:#cccccc;border:0;margin:0;padding:10px 16px 14px;border-radius:0}\n.dd-post .dd-vsc-status{display:flex;flex-wrap:wrap;gap:4px 14px;background:#007acc;color:#fff;font-size:11.5px;padding:4px 12px}\n.dd-post .dd-vsc-status .sp{flex:1}\n.dd-post .dd-vsc-termonly pre.dd-vsc-code{background:#181818;padding:12px 16px}\n@media(max-width:640px){.dd-post .dd-vsc-title{padding-right:0;text-align:left}.dd-post .dd-vsc-status span:nth-child(n+4){display:none}.dd-post .dd-vsc .l::before{width:2.2em;padding-right:1em}}\n.dd-post .dd-vsc-hint{font-size:.93em;color:var(--muted);margin-top:-.6em}\n.dd-post .dd-next{border:1px dashed var(--violet);border-radius:12px;padding:16px 20px;margin:1.6em 0;background:#f7f4ff}\n<\/style>\n<style>\n.dd-post .dd-dial{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:10px;margin:.8em 0}\n.dd-post .dd-dial div{background:#151b3b;color:#e8ebff;border-radius:10px;padding:10px 12px;font-size:.92em}\n.dd-post .dd-dial b{display:block;color:#a99bff;font-size:.82em;text-transform:uppercase;letter-spacing:.05em}\n.dd-post .ic.json{background:#cbcb41}\n<\/style>\n\n<div class=\"dd-hero\">\n<svg viewBox=\"0 0 1200 520\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Generative AI series Part 5 banner: next-token probabilities pass through temperature, top-p, max tokens and stop sequence dials before the LLM writes its answer\">\n<defs>\n<linearGradient id=\"ddbg\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\"><stop offset=\"0\" stop-color=\"#0b1020\"\/><stop offset=\"1\" stop-color=\"#151b3b\"\/><\/linearGradient>\n<linearGradient id=\"ddac\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\"><stop offset=\"0\" stop-color=\"#3178c6\"\/><stop offset=\"1\" stop-color=\"#7c5cff\"\/><\/linearGradient>\n<radialGradient id=\"ddglow\" cx=\".5\" cy=\".5\" r=\".5\"><stop offset=\"0\" stop-color=\"#7c5cff\" stop-opacity=\".45\"\/><stop offset=\"1\" stop-color=\"#7c5cff\" stop-opacity=\"0\"\/><\/radialGradient>\n<\/defs>\n<rect x=\"0\" y=\"-60\" width=\"1200\" height=\"640\" fill=\"url(#ddbg)\"\/>\n<circle cx=\"1000\" cy=\"110\" r=\"250\" fill=\"url(#ddglow)\"\/>\n<g opacity=\".12\" stroke=\"#9fb2ff\"><path d=\"M0 100H1200M0 200H1200M0 300H1200M0 400H1200M200 0V520M400 0V520M600 0V520M800 0V520M1000 0V520\"\/><\/g>\n<rect x=\"60\" y=\"46\" width=\"128\" height=\"38\" rx=\"19\" fill=\"url(#ddac)\"\/>\n<text x=\"124\" y=\"71\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"18\" font-weight=\"700\" fill=\"#fff\">PART 5<\/text>\n<text x=\"204\" y=\"71\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"18\" fill=\"#aab4e8\">Generative AI Series \u00b7 DevDojo<\/text>\n<text x=\"60\" y=\"148\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"52\" font-weight=\"800\" fill=\"#ffffff\">Temperature and Top-p<\/text>\n<text x=\"60\" y=\"204\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"40\" font-weight=\"800\" fill=\"url(#ddac)\">Controlling Model Output<\/text>\n<text x=\"60\" y=\"246\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"23\" fill=\"#c9d1ff\">Make LLM answers focused, creative, short or repeatable<\/text>\n<g font-family=\"Segoe UI,Arial,sans-serif\">\n<g class=\"node\"><rect x=\"70\" y=\"300\" width=\"200\" height=\"170\" rx=\"16\" fill=\"#1b2250\" stroke=\"#3178c6\" stroke-width=\"2\"\/>\n<text x=\"170\" y=\"330\" text-anchor=\"middle\" font-size=\"16\" font-weight=\"700\" fill=\"#c9d1ff\">Next-token odds<\/text>\n<rect x=\"95\" y=\"345\" width=\"150\" height=\"16\" rx=\"4\" fill=\"#7c5cff\"\/><text x=\"100\" y=\"358\" font-size=\"12\" fill=\"#fff\">practice 50%<\/text>\n<rect x=\"95\" y=\"370\" width=\"90\" height=\"16\" rx=\"4\" fill=\"#3178c6\"\/><text x=\"100\" y=\"383\" font-size=\"12\" fill=\"#fff\">projects 30%<\/text>\n<rect x=\"95\" y=\"395\" width=\"40\" height=\"16\" rx=\"4\" fill=\"#61dafb\"\/><text x=\"140\" y=\"408\" font-size=\"12\" fill=\"#c9d1ff\">reading 11%<\/text>\n<rect x=\"95\" y=\"420\" width=\"26\" height=\"16\" rx=\"4\" fill=\"#61dafb\" opacity=\".7\"\/><text x=\"126\" y=\"433\" font-size=\"12\" fill=\"#c9d1ff\">videos 7%<\/text>\n<rect x=\"95\" y=\"445\" width=\"8\" height=\"12\" rx=\"3\" fill=\"#8a90a6\"\/><text x=\"108\" y=\"456\" font-size=\"12\" fill=\"#8a90a6\">dancing 2%<\/text><\/g>\n<g class=\"node\"><rect x=\"340\" y=\"300\" width=\"150\" height=\"70\" rx=\"14\" fill=\"#0b1020\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"415\" y=\"331\" text-anchor=\"middle\" font-size=\"17\" font-weight=\"800\" fill=\"#fff\">Temperature<\/text><text x=\"415\" y=\"353\" text-anchor=\"middle\" font-size=\"13\" fill=\"#c9d1ff\">sharpen or flatten<\/text><\/g>\n<g class=\"node\"><rect x=\"510\" y=\"300\" width=\"150\" height=\"70\" rx=\"14\" fill=\"#0b1020\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"585\" y=\"331\" text-anchor=\"middle\" font-size=\"17\" font-weight=\"800\" fill=\"#fff\">Top-p<\/text><text x=\"585\" y=\"353\" text-anchor=\"middle\" font-size=\"13\" fill=\"#c9d1ff\">cut the long tail<\/text><\/g>\n<g class=\"node\"><rect x=\"340\" y=\"400\" width=\"150\" height=\"70\" rx=\"14\" fill=\"#0b1020\" stroke=\"#61dafb\" stroke-width=\"2\"\/><text x=\"415\" y=\"431\" text-anchor=\"middle\" font-size=\"17\" font-weight=\"800\" fill=\"#fff\">Max tokens<\/text><text x=\"415\" y=\"453\" text-anchor=\"middle\" font-size=\"13\" fill=\"#c9d1ff\">limit the length<\/text><\/g>\n<g class=\"node\"><rect x=\"510\" y=\"400\" width=\"150\" height=\"70\" rx=\"14\" fill=\"#0b1020\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"585\" y=\"431\" text-anchor=\"middle\" font-size=\"17\" font-weight=\"800\" fill=\"#fff\">Stop sequence<\/text><text x=\"585\" y=\"453\" text-anchor=\"middle\" font-size=\"13\" fill=\"#c9d1ff\">end at a marker<\/text><\/g>\n<g class=\"node\"><rect x=\"740\" y=\"345\" width=\"150\" height=\"80\" rx=\"18\" fill=\"url(#ddac)\"\/><text x=\"815\" y=\"384\" text-anchor=\"middle\" font-size=\"27\" font-weight=\"800\" fill=\"#fff\">LLM<\/text><text x=\"815\" y=\"408\" text-anchor=\"middle\" font-size=\"14\" fill=\"#eef\">picks a token<\/text><\/g>\n<g class=\"node\"><rect x=\"950\" y=\"345\" width=\"200\" height=\"80\" rx=\"18\" fill=\"#0b1020\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"1050\" y=\"380\" text-anchor=\"middle\" font-size=\"20\" font-weight=\"800\" fill=\"#fff\">Your answer<\/text><text x=\"1050\" y=\"405\" text-anchor=\"middle\" font-size=\"14\" fill=\"#c9d1ff\">focused or creative<\/text><\/g>\n<\/g>\n<path id=\"ddf1\" d=\"M270 385 C 300 385, 310 335, 340 335\" fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" stroke-dasharray=\"8 8\" opacity=\".8\"\/>\n<path id=\"ddf2\" d=\"M660 335 C 700 335, 700 385, 740 385\" fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" stroke-dasharray=\"8 8\" opacity=\".8\"\/>\n<path id=\"ddf3\" d=\"M660 435 C 700 435, 700 385, 740 385\" fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" stroke-dasharray=\"8 8\" opacity=\".8\"\/>\n<path id=\"ddf4\" d=\"M890 385 C 910 385, 930 385, 950 385\" fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" stroke-dasharray=\"8 8\" opacity=\".8\"\/>\n<circle r=\"7\" fill=\"#7c5cff\"><animateMotion dur=\"2.6s\" repeatCount=\"indefinite\"><mpath href=\"#ddf1\"\/><\/animateMotion><\/circle>\n<circle r=\"6\" fill=\"#3178c6\"><animateMotion dur=\"2.6s\" begin=\".8s\" repeatCount=\"indefinite\"><mpath href=\"#ddf2\"\/><\/animateMotion><\/circle>\n<circle r=\"6\" fill=\"#61dafb\"><animateMotion dur=\"2.6s\" begin=\"1.6s\" repeatCount=\"indefinite\"><mpath href=\"#ddf3\"\/><\/animateMotion><\/circle>\n<circle r=\"6\" fill=\"#ffffff\"><animateMotion dur=\"1.8s\" repeatCount=\"indefinite\"><mpath href=\"#ddf4\"\/><\/animateMotion><\/circle>\n<text x=\"60\" y=\"510\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"16\" fill=\"#7f89b5\">devdojo.co.in \u00b7 Beginner friendly \u00b7 Python + Node.js examples<\/text>\n<\/svg>\n<\/div>\n\n<div class=\"dd-series\">Series: Generative AI <span>Part 5 of 30<\/span><\/div>\n<p class=\"dd-prereq\"><strong>What you should know first:<\/strong> read <a href=\"https:\/\/devdojo.co.in\/?p=240\">Part 4: Prompt Engineering Basics<\/a> and <a href=\"https:\/\/devdojo.co.in\/?p=223\">Part 3: Your First LLM API Calls<\/a>. It also helps to remember what tokens are from <a href=\"https:\/\/devdojo.co.in\/?p=219\">Part 2<\/a>.<\/p>\n\n<p>Ask an LLM the same question twice and you often get two different answers. Sometimes that is great (brainstorming), and sometimes it is a bug (data extraction). In this part you will learn how <strong>temperature and top-p<\/strong> help with controlling model output: how a model actually picks each word, what the <strong>temperature<\/strong>, <strong>top-p<\/strong> and <strong>top-k<\/strong> settings change, how <strong>max tokens<\/strong> and <strong>stop sequences<\/strong> end an answer, and which of these dials still work on today&#8217;s reasoning models from OpenAI, Anthropic and Google. You will run two small simulations (no API key needed) to <em>see<\/em> sampling happen, then use the real settings in Python and Node.js.<\/p>\n\n<div class=\"dd-summary\">\n<strong class=\"lbl\">Quick Summary<\/strong>\n<ul>\n<li>An LLM gives every possible next token a <strong>probability<\/strong>, then <strong>samples<\/strong> one. Sampling settings change those odds.<\/li>\n<li><strong>Temperature<\/strong> sharpens (low, like 0.2) or flattens (high, like 1.2) the odds. Low = focused and repeatable, high = varied and creative.<\/li>\n<li><strong>Top-p<\/strong> keeps only the most likely words that add up to <em>p<\/em> (for example 90%); <strong>top-k<\/strong> keeps the <em>k<\/em> most likely words. Change temperature <em>or<\/em> top-p, not both.<\/li>\n<li><strong>Max tokens<\/strong> limits answer length and <strong>stop sequences<\/strong> end the answer at a marker. These work on almost every model.<\/li>\n<li>Many new reasoning models <strong>reject custom temperature<\/strong>: GPT-6 accepts it only with reasoning effort <code>none<\/code>, and newer Claude models accept only the defaults. Use prompts, verbosity and structured outputs instead.<\/li>\n<li>Open models (Ollama, vLLM) still give you every dial, including a <strong>seed<\/strong> for repeatable results.<\/li>\n<\/ul>\n<\/div>\n\n<details class=\"dd-roadmap\">\n<summary>Full Generative AI roadmap (30 parts)<\/summary>\n<h4>Beginner<\/h4>\n<ol start=\"1\">\n<li><a href=\"https:\/\/devdojo.co.in\/?p=215\">What Is Generative AI and How Do LLMs Work?<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=219\">Tokens, Context Windows and Model Parameters<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=223\">Your First LLM API Calls in Python and Node.js (OpenAI, Anthropic, Gemini)<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=240\">Prompt Engineering Basics: Roles, Instructions and Examples<\/a><\/li>\n<li>Temperature, Top-p and Controlling Model Output <span class=\"here\">You are here<\/span><\/li>\n<li>Running Open Models Locally with Ollama <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Streaming Responses and Building a Simple Chat UI <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Structured Outputs: Getting Reliable JSON from LLMs <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<h4>Intermediate<\/h4>\n<ol start=\"9\">\n<li>Embeddings Explained: Turning Text into Vectors <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Vector Databases: pgvector, Chroma and Pinecone <span class=\"soon\">Coming soon<\/span><\/li>\n<li>RAG Fundamentals: Retrieval-Augmented Generation <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Document Loading and Chunking Strategies <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Better RAG: Hybrid Search, Re-ranking and Query Rewriting <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Function Calling and Tool Use <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Conversation Memory and Long Context Management <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Multimodal AI: Vision, Images and Audio Inputs <span class=\"soon\">Coming soon<\/span><\/li>\n<li>How Image Generation Works: Diffusion Models Basics <span class=\"soon\">Coming soon<\/span><\/li>\n<li>LangChain vs LlamaIndex: When to Use a Framework <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Testing LLM Apps: Evals, Metrics and Test Sets <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Cutting Cost and Latency: Caching, Batching and Model Choice <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<h4>Advanced<\/h4>\n<ol start=\"21\">\n<li>Fine-Tuning vs Prompting vs RAG: Choosing the Right Tool <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Fine-Tuning an Open Model with LoRA <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Transformers Deep Dive: Attention Explained Simply <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Reasoning Models and Thinking Time <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Guardrails, Safety and Prompt Injection Defence <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Monitoring and Tracing LLM Apps in Production <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Serving Open Models: vLLM, Quantization and GPUs <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Building a Production GenAI App End to End <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Responsible AI: Privacy, Licensing and Bias <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Final Project: Build Your Own AI Knowledge Assistant <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<\/details>\n\n<nav class=\"dd-toc\" aria-label=\"Table of contents\">\n<strong>In this part<\/strong>\n<ol>\n<li><a href=\"#how-models-pick-words\">How a model picks the next word<\/a><\/li>\n<li><a href=\"#temperature\">What temperature does<\/a><\/li>\n<li><a href=\"#top-p-top-k\">Top-p and top-k explained<\/a><\/li>\n<li><a href=\"#length-stop\">Max tokens and stop sequences<\/a><\/li>\n<li><a href=\"#which-models\">Which models still accept these settings<\/a><\/li>\n<li><a href=\"#api-code\">Controlling output with the APIs<\/a><\/li>\n<li><a href=\"#presets\">Settings by task<\/a><\/li>\n<li><a href=\"#repeatable\">Repeatable output and seeds<\/a><\/li>\n<li><a href=\"#common-errors\">Common errors and fixes<\/a><\/li>\n<li><a href=\"#best-practices\">Best practices<\/a><\/li>\n<li><a href=\"#practice\">Practice task<\/a><\/li>\n<li><a href=\"#faq\">FAQ<\/a><\/li>\n<\/ol>\n<\/nav>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"how-models-pick-words\">How an LLM picks the next word<\/h2>\n<p>In <a href=\"https:\/\/devdojo.co.in\/?p=215\">Part 1<\/a> we learned that an LLM writes one token at a time. At every step the model gives a raw <strong>score<\/strong> (called a <em>logit<\/em>) to every token in its vocabulary. A function called <strong>softmax<\/strong> turns those scores into percentages that add up to 100%. Then the model does not simply take the top word: it <strong>samples<\/strong>, like rolling a weighted dice. A word with 50% wins half the time, a word with 2% wins now and then.<\/p>\n<p>That dice roll is why the same prompt can give different answers. The output-control settings in this post change the dice before it is rolled, or decide when to stop rolling:<\/p>\n<figure>\n<svg viewBox=\"0 0 960 230\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Pipeline of one generation step: model scores, divide by temperature, softmax, top-k and top-p filter, random pick, then repeat until max tokens or a stop sequence\" font-family=\"Segoe UI,Arial,sans-serif\">\n<rect width=\"960\" height=\"230\" rx=\"14\" fill=\"#0f1430\"\/>\n<g font-size=\"15\" font-weight=\"700\" fill=\"#fff\" text-anchor=\"middle\">\n<g class=\"node\"><rect x=\"20\" y=\"60\" width=\"150\" height=\"64\" rx=\"12\" fill=\"#1b2250\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"95\" y=\"88\">1. Model scores<\/text><text x=\"95\" y=\"108\" font-weight=\"400\" font-size=\"13\" fill=\"#c9d1ff\">logits<\/text><\/g>\n<g class=\"node\"><rect x=\"205\" y=\"60\" width=\"150\" height=\"64\" rx=\"12\" fill=\"#1b2250\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"280\" y=\"88\">2. \u00f7 temperature<\/text><text x=\"280\" y=\"108\" font-weight=\"400\" font-size=\"13\" fill=\"#c9d1ff\">sharpen \/ flatten<\/text><\/g>\n<g class=\"node\"><rect x=\"390\" y=\"60\" width=\"150\" height=\"64\" rx=\"12\" fill=\"#1b2250\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"465\" y=\"88\">3. Softmax<\/text><text x=\"465\" y=\"108\" font-weight=\"400\" font-size=\"13\" fill=\"#c9d1ff\">scores \u2192 %<\/text><\/g>\n<g class=\"node\"><rect x=\"575\" y=\"60\" width=\"150\" height=\"64\" rx=\"12\" fill=\"#1b2250\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"650\" y=\"88\">4. Top-k \/ top-p<\/text><text x=\"650\" y=\"108\" font-weight=\"400\" font-size=\"13\" fill=\"#c9d1ff\">drop unlikely words<\/text><\/g>\n<g class=\"node\"><rect x=\"760\" y=\"60\" width=\"180\" height=\"64\" rx=\"12\" fill=\"url(#f3ac)\"\/><text x=\"850\" y=\"88\">5. Random pick<\/text><text x=\"850\" y=\"108\" font-weight=\"400\" font-size=\"13\" fill=\"#eef\">seed decides the dice<\/text><\/g>\n<\/g>\n<defs><linearGradient id=\"f3ac\" x1=\"0\" x2=\"1\"><stop offset=\"0\" stop-color=\"#3178c6\"\/><stop offset=\"1\" stop-color=\"#7c5cff\"\/><\/linearGradient><\/defs>\n<g stroke=\"#7c5cff\" stroke-width=\"3\"><path d=\"M172 92h30M357 92h30M542 92h30M727 92h30\"\/><\/g>\n<path d=\"M850 126 C 850 185, 95 185, 95 128\" fill=\"none\" stroke=\"#61dafb\" stroke-width=\"2.5\" stroke-dasharray=\"7 6\"\/>\n<text x=\"480\" y=\"200\" text-anchor=\"middle\" font-size=\"15\" fill=\"#c9d1ff\">repeat for the next token \u2026 until max tokens is reached or a stop sequence appears<\/text>\n<\/svg>\n<figcaption>One generation step. Temperature, top-k and top-p change <em>which<\/em> words can be picked; max tokens and stop sequences decide <em>when<\/em> the answer ends.<\/figcaption>\n<\/figure>\n<div class=\"dd-cards\">\n<div class=\"dd-card\"><b>Temperature<\/b>How sharp or flat the odds are.<\/div>\n<div class=\"dd-card\"><b>Top-p \/ top-k<\/b>Which unlikely words are thrown away before the roll.<\/div>\n<div class=\"dd-card\"><b>Max tokens<\/b>The most tokens the answer may use.<\/div>\n<div class=\"dd-card\"><b>Stop sequences<\/b>Text that ends the answer as soon as it appears.<\/div>\n<\/div>\n\n<h3>Project setup<\/h3>\n<p>Use the same project folder and <code>.env<\/code> file (with your API keys) from Part 3. The first two demos are pure Python and plain JavaScript, so you can run them without any key or package.<\/p>\n<div class=\"dd-vsc dd-vsc-termonly\" role=\"group\" aria-label=\"Terminal: Terminal \u2014 devdojo-llm-api\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">Terminal \u2014 devdojo-llm-api<\/span><\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># Python: SDKs from Part 3<\/span><\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> pip install -U openai anthropic python-dotenv<\/span>\n<span class=\"l\"><span class=\"c\"># Node.js<\/span><\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> npm install openai @anthropic-ai\/sdk<\/span>\n<span class=\"l\"><span class=\"c\"># the two sampling demos need no packages and no API key<\/span><\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> python temperature_demo.py<\/span><\/code><\/pre>\n<\/div>\n\n<h2 id=\"temperature\">What temperature does in an LLM<\/h2>\n<p><strong>Temperature<\/strong> divides every score by a number <em>T<\/em> before softmax. A small <em>T<\/em> (like 0.2) makes big scores much bigger than small ones, so the top word wins almost every time. A large <em>T<\/em> (like 1.5) squeezes the scores together, so rare words get a real chance. <code>T = 1<\/code> leaves the model&#8217;s odds unchanged. Let&#8217;s see it with five candidate words for the sentence &#8220;The best way to learn coding is &#8230;&#8221;.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: temperature_demo.py, temperature-demo.mjs\">\n<input type=\"radio\" name=\"v1\" id=\"v1f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v1\" id=\"v1f2\" class=\"f2\">\n<input type=\"checkbox\" id=\"v1r1\" class=\"r1\">\n<input type=\"checkbox\" id=\"v1r2\" class=\"r2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">2 files \u2014 devdojo-sampling \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v1f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>temperature_demo.py<\/label>\n<label for=\"v1f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>temperature-demo.mjs<\/label>\n<label for=\"v1r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<label for=\"v1r2\" class=\"run run2\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">devdojo-sampling \u203a temperature_demo.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># See how temperature reshapes the next-token probabilities<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">math<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># Toy scores (logits) for the word after: \"The best way to learn coding is ...\"<\/span><\/span>\n<span class=\"l\"><span class=\"n\">logits<\/span> = {<span class=\"s\">\"practice\"<\/span>: <span class=\"m\">4.0<\/span>, <span class=\"s\">\"projects\"<\/span>: <span class=\"m\">3.5<\/span>, <span class=\"s\">\"reading\"<\/span>: <span class=\"m\">2.5<\/span>,<\/span>\n<span class=\"l\">          <span class=\"s\">\"videos\"<\/span>: <span class=\"m\">2.0<\/span>, <span class=\"s\">\"dancing\"<\/span>: <span class=\"m\">0.5<\/span>}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">softmax<\/span>(<span class=\"n\">scores<\/span>, <span class=\"n\">temperature<\/span>=<span class=\"m\">1.0<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">scaled<\/span> = {<span class=\"n\">w<\/span>: <span class=\"n\">s<\/span> \/ <span class=\"n\">temperature<\/span> <span class=\"k\">for<\/span> <span class=\"n\">w<\/span>, <span class=\"n\">s<\/span> in <span class=\"n\">scores<\/span>.<span class=\"n\">items<\/span>()}<\/span>\n<span class=\"l\">    <span class=\"n\">biggest<\/span> = <span class=\"nb\">max<\/span>(<span class=\"n\">scaled<\/span>.<span class=\"n\">values<\/span>())<\/span>\n<span class=\"l\">    <span class=\"n\">exps<\/span> = {<span class=\"n\">w<\/span>: <span class=\"n\">math<\/span>.<span class=\"n\">exp<\/span>(<span class=\"n\">s<\/span> - <span class=\"n\">biggest<\/span>) <span class=\"k\">for<\/span> <span class=\"n\">w<\/span>, <span class=\"n\">s<\/span> in <span class=\"n\">scaled<\/span>.<span class=\"n\">items<\/span>()}<\/span>\n<span class=\"l\">    <span class=\"n\">total<\/span> = <span class=\"nb\">sum<\/span>(<span class=\"n\">exps<\/span>.<span class=\"n\">values<\/span>())<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> {<span class=\"n\">w<\/span>: <span class=\"n\">e<\/span> \/ <span class=\"n\">total<\/span> <span class=\"k\">for<\/span> <span class=\"n\">w<\/span>, <span class=\"n\">e<\/span> in <span class=\"n\">exps<\/span>.<span class=\"n\">items<\/span>()}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">for<\/span> <span class=\"n\">t<\/span> in (<span class=\"m\">0.2<\/span>, <span class=\"m\">0.7<\/span>, <span class=\"m\">1.0<\/span>, <span class=\"m\">1.5<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">probs<\/span> = <span class=\"n\">softmax<\/span>(<span class=\"n\">logits<\/span>, <span class=\"n\">t<\/span>)<\/span>\n<span class=\"l\">    <span class=\"n\">row<\/span> = <span class=\"s\">\"<\/span>  <span class=\"s\">\"<\/span>.<span class=\"n\">join<\/span>(<span class=\"s\">f\"{<\/span><span class=\"n\">w<\/span><span class=\"s\">}={<\/span><span class=\"n\">p<\/span><span class=\"s\">:.0%}\"<\/span> <span class=\"k\">for<\/span> <span class=\"n\">w<\/span>, <span class=\"n\">p<\/span> in <span class=\"n\">probs<\/span>.<span class=\"n\">items<\/span>())<\/span>\n<span class=\"l\">    <span class=\"nb\">print<\/span>(<span class=\"s\">f\"T={<\/span><span class=\"n\">t<\/span><span class=\"s\">:&lt;4}<\/span> <span class=\"s\">{<\/span><span class=\"n\">row<\/span><span class=\"s\">}\"<\/span>)<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python temperature_demo.py\nT=0.2  practice=92%  projects=8%  reading=0%  videos=0%  dancing=0%\nT=0.7  practice=60%  projects=29%  reading=7%  videos=3%  dancing=0%\nT=1.0  practice=50%  projects=30%  reading=11%  videos=7%  dancing=2%\nT=1.5  practice=41%  projects=29%  reading=15%  videos=11%  dancing=4%<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 18, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">devdojo-sampling \u203a temperature-demo.mjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ See how temperature reshapes the next-token probabilities<\/span><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">logits<\/span> = { <span class=\"n\">practice<\/span>: <span class=\"m\">4.0<\/span>, <span class=\"n\">projects<\/span>: <span class=\"m\">3.5<\/span>, <span class=\"n\">reading<\/span>: <span class=\"m\">2.5<\/span>,<\/span>\n<span class=\"l\">                 <span class=\"n\">videos<\/span>: <span class=\"m\">2.0<\/span>, <span class=\"n\">dancing<\/span>: <span class=\"m\">0.5<\/span> };<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">function<\/span> <span class=\"n\">softmax<\/span>(<span class=\"n\">scores<\/span>, <span class=\"n\">temperature<\/span> = <span class=\"m\">1.0<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"k\">const<\/span> <span class=\"n\">scaled<\/span> = <span class=\"nb\">Object<\/span>.<span class=\"n\">entries<\/span>(<span class=\"n\">scores<\/span>).<span class=\"n\">map<\/span>(([<span class=\"n\">w<\/span>, <span class=\"n\">s<\/span>]) =&gt; [<span class=\"n\">w<\/span>, <span class=\"n\">s<\/span> \/ <span class=\"n\">temperature<\/span>]);<\/span>\n<span class=\"l\">  <span class=\"k\">const<\/span> <span class=\"n\">biggest<\/span> = <span class=\"nb\">Math<\/span>.<span class=\"n\">max<\/span>(...<span class=\"n\">scaled<\/span>.<span class=\"n\">map<\/span>(([, <span class=\"n\">s<\/span>]) =&gt; <span class=\"n\">s<\/span>));<\/span>\n<span class=\"l\">  <span class=\"k\">const<\/span> <span class=\"n\">exps<\/span> = <span class=\"n\">scaled<\/span>.<span class=\"n\">map<\/span>(([<span class=\"n\">w<\/span>, <span class=\"n\">s<\/span>]) =&gt; [<span class=\"n\">w<\/span>, <span class=\"nb\">Math<\/span>.<span class=\"n\">exp<\/span>(<span class=\"n\">s<\/span> - <span class=\"n\">biggest<\/span>)]);<\/span>\n<span class=\"l\">  <span class=\"k\">const<\/span> <span class=\"n\">total<\/span> = <span class=\"n\">exps<\/span>.<span class=\"n\">reduce<\/span>((<span class=\"n\">sum<\/span>, [, <span class=\"n\">e<\/span>]) =&gt; <span class=\"n\">sum<\/span> + <span class=\"n\">e<\/span>, <span class=\"m\">0<\/span>);<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"n\">exps<\/span>.<span class=\"n\">map<\/span>(([<span class=\"n\">w<\/span>, <span class=\"n\">e<\/span>]) =&gt; [<span class=\"n\">w<\/span>, <span class=\"n\">e<\/span> \/ <span class=\"n\">total<\/span>]);<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">for<\/span> (<span class=\"k\">const<\/span> <span class=\"n\">t<\/span> <span class=\"k\">of<\/span> [<span class=\"m\">0.2<\/span>, <span class=\"m\">0.7<\/span>, <span class=\"m\">1.0<\/span>, <span class=\"m\">1.5<\/span>]) {<\/span>\n<span class=\"l\">  <span class=\"k\">const<\/span> <span class=\"n\">row<\/span> = <span class=\"n\">softmax<\/span>(<span class=\"n\">logits<\/span>, <span class=\"n\">t<\/span>)<\/span>\n<span class=\"l\">    .<span class=\"n\">map<\/span>(([<span class=\"n\">w<\/span>, <span class=\"n\">p<\/span>]) =&gt; <span class=\"s\">`${<\/span><span class=\"n\">w<\/span><span class=\"s\">}=${<\/span><span class=\"nb\">Math<\/span>.<span class=\"n\">round<\/span>(<span class=\"n\">p<\/span> * <span class=\"m\">100<\/span>)<span class=\"s\">}%`<\/span>)<\/span>\n<span class=\"l\">    .<span class=\"n\">join<\/span>(<span class=\"s\">\"  \"<\/span>);<\/span>\n<span class=\"l\">  <span class=\"n\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"s\">`T=${<\/span><span class=\"n\">t<\/span>.<span class=\"n\">toFixed<\/span>(<span class=\"m\">1<\/span>).<span class=\"n\">padEnd<\/span>(<span class=\"m\">4<\/span>)<span class=\"s\">}<\/span> <span class=\"s\">${<\/span><span class=\"n\">row<\/span><span class=\"s\">}`<\/span>);<\/span>\n<span class=\"l\">}<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node temperature-demo.mjs\nT=0.2  practice=92%  projects=8%  reading=0%  videos=0%  dancing=0%\nT=0.7  practice=60%  projects=29%  reading=7%  videos=3%  dancing=0%\nT=1.0  practice=50%  projects=30%  reading=11%  videos=7%  dancing=2%\nT=1.5  practice=41%  projects=29%  reading=15%  videos=11%  dancing=4%<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 18, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Click the tabs to switch between Python and Node.js, then click <strong>\u25b6 Run<\/strong> to see the real output.<\/p>\n<figure>\n<svg viewBox=\"0 0 960 280\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Bar charts of next-token probabilities at temperature 0.2, 1.0 and 1.5: low temperature gives practice 92 percent, high temperature spreads probability to rarer words\" font-family=\"Segoe UI,Arial,sans-serif\">\n<rect width=\"960\" height=\"280\" rx=\"14\" fill=\"#0f1430\"\/>\n<text x=\"135\" y=\"40\" text-anchor=\"middle\" font-size=\"18\" font-weight=\"800\" fill=\"#fff\">T = 0.2 (focused)<\/text>\n<text x=\"30\" y=\"79\" font-size=\"14\" fill=\"#c9d1ff\">practice<\/text>\n<rect x=\"100\" y=\"65\" width=\"147\" height=\"20\" rx=\"5\" fill=\"#3178c6\"\/>\n<text x=\"253\" y=\"80\" font-size=\"13\" fill=\"#fff\">92%<\/text>\n<text x=\"30\" y=\"119\" font-size=\"14\" fill=\"#c9d1ff\">projects<\/text>\n<rect x=\"100\" y=\"105\" width=\"13\" height=\"20\" rx=\"5\" fill=\"#3178c6\"\/>\n<text x=\"119\" y=\"120\" font-size=\"13\" fill=\"#fff\">8%<\/text>\n<text x=\"30\" y=\"159\" font-size=\"14\" fill=\"#c9d1ff\">reading<\/text>\n<rect x=\"100\" y=\"145\" width=\"3\" height=\"20\" rx=\"5\" fill=\"#3178c6\"\/>\n<text x=\"109\" y=\"160\" font-size=\"13\" fill=\"#fff\">0%<\/text>\n<text x=\"30\" y=\"199\" font-size=\"14\" fill=\"#c9d1ff\">videos<\/text>\n<rect x=\"100\" y=\"185\" width=\"3\" height=\"20\" rx=\"5\" fill=\"#3178c6\"\/>\n<text x=\"109\" y=\"200\" font-size=\"13\" fill=\"#fff\">0%<\/text>\n<text x=\"30\" y=\"239\" font-size=\"14\" fill=\"#c9d1ff\">dancing<\/text>\n<rect x=\"100\" y=\"225\" width=\"3\" height=\"20\" rx=\"5\" fill=\"#3178c6\"\/>\n<text x=\"109\" y=\"240\" font-size=\"13\" fill=\"#fff\">0%<\/text>\n<text x=\"450\" y=\"40\" text-anchor=\"middle\" font-size=\"18\" font-weight=\"800\" fill=\"#fff\">T = 1.0 (default)<\/text>\n<text x=\"345\" y=\"79\" font-size=\"14\" fill=\"#c9d1ff\">practice<\/text>\n<rect x=\"415\" y=\"65\" width=\"80\" height=\"20\" rx=\"5\" fill=\"#7c5cff\"\/>\n<text x=\"501\" y=\"80\" font-size=\"13\" fill=\"#fff\">50%<\/text>\n<text x=\"345\" y=\"119\" font-size=\"14\" fill=\"#c9d1ff\">projects<\/text>\n<rect x=\"415\" y=\"105\" width=\"48\" height=\"20\" rx=\"5\" fill=\"#7c5cff\"\/>\n<text x=\"469\" y=\"120\" font-size=\"13\" fill=\"#fff\">30%<\/text>\n<text x=\"345\" y=\"159\" font-size=\"14\" fill=\"#c9d1ff\">reading<\/text>\n<rect x=\"415\" y=\"145\" width=\"18\" height=\"20\" rx=\"5\" fill=\"#7c5cff\"\/>\n<text x=\"439\" y=\"160\" font-size=\"13\" fill=\"#fff\">11%<\/text>\n<text x=\"345\" y=\"199\" font-size=\"14\" fill=\"#c9d1ff\">videos<\/text>\n<rect x=\"415\" y=\"185\" width=\"11\" height=\"20\" rx=\"5\" fill=\"#7c5cff\"\/>\n<text x=\"432\" y=\"200\" font-size=\"13\" fill=\"#fff\">7%<\/text>\n<text x=\"345\" y=\"239\" font-size=\"14\" fill=\"#c9d1ff\">dancing<\/text>\n<rect x=\"415\" y=\"225\" width=\"3\" height=\"20\" rx=\"5\" fill=\"#7c5cff\"\/>\n<text x=\"424\" y=\"240\" font-size=\"13\" fill=\"#fff\">2%<\/text>\n<text x=\"765\" y=\"40\" text-anchor=\"middle\" font-size=\"18\" font-weight=\"800\" fill=\"#fff\">T = 1.5 (wild)<\/text>\n<text x=\"660\" y=\"79\" font-size=\"14\" fill=\"#c9d1ff\">practice<\/text>\n<rect x=\"730\" y=\"65\" width=\"66\" height=\"20\" rx=\"5\" fill=\"#61dafb\"\/>\n<text x=\"802\" y=\"80\" font-size=\"13\" fill=\"#fff\">41%<\/text>\n<text x=\"660\" y=\"119\" font-size=\"14\" fill=\"#c9d1ff\">projects<\/text>\n<rect x=\"730\" y=\"105\" width=\"46\" height=\"20\" rx=\"5\" fill=\"#61dafb\"\/>\n<text x=\"782\" y=\"120\" font-size=\"13\" fill=\"#fff\">29%<\/text>\n<text x=\"660\" y=\"159\" font-size=\"14\" fill=\"#c9d1ff\">reading<\/text>\n<rect x=\"730\" y=\"145\" width=\"24\" height=\"20\" rx=\"5\" fill=\"#61dafb\"\/>\n<text x=\"760\" y=\"160\" font-size=\"13\" fill=\"#fff\">15%<\/text>\n<text x=\"660\" y=\"199\" font-size=\"14\" fill=\"#c9d1ff\">videos<\/text>\n<rect x=\"730\" y=\"185\" width=\"18\" height=\"20\" rx=\"5\" fill=\"#61dafb\"\/>\n<text x=\"754\" y=\"200\" font-size=\"13\" fill=\"#fff\">11%<\/text>\n<text x=\"660\" y=\"239\" font-size=\"14\" fill=\"#c9d1ff\">dancing<\/text>\n<rect x=\"730\" y=\"225\" width=\"6\" height=\"20\" rx=\"5\" fill=\"#61dafb\"\/>\n<text x=\"742\" y=\"240\" font-size=\"13\" fill=\"#fff\">4%<\/text>\n<\/svg>\n<figcaption>Same model, same prompt, three temperatures. Low temperature makes the top word win almost every time; high temperature gives rare words a real chance. (Numbers from the <code>temperature_demo.py<\/code> run below.)<\/figcaption>\n<\/figure>\n<p>Look at the numbers. At <code>T=0.2<\/code> &#8220;practice&#8221; wins 92% of the time and &#8220;reading&#8221; almost never appears. At <code>T=1.5<\/code> even &#8220;dancing&#8221; gets 4%. Over a 500-token answer those small chances add up, which is why very high temperatures produce strange or off-topic text.<\/p>\n<div class=\"dd-note\"><strong>Temperature 0 is not magic.<\/strong> Mathematically you cannot divide by zero, so APIs treat <code>0<\/code> as &#8220;always take the top token&#8221; (called <em>greedy decoding<\/em>). Even then, hosted models can return slightly different text because of how the servers batch and compute numbers. Low temperature means <em>more<\/em> repeatable, not <em>perfectly<\/em> repeatable.<\/div>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"top-p-top-k\">Top-p and top-k explained<\/h2>\n<p>Temperature changes the odds of <em>all<\/em> words. <strong>Top-p<\/strong> and <strong>top-k<\/strong> instead <strong>remove<\/strong> unlikely words before the dice roll:<\/p>\n<ul>\n<li><strong>Top-k<\/strong> keeps only the <em>k<\/em> most likely tokens. With <code>top_k=2<\/code> only &#8220;practice&#8221; and &#8220;projects&#8221; can be picked.<\/li>\n<li><strong>Top-p<\/strong> (also called <em>nucleus sampling<\/em>) sorts the tokens and keeps adding them until their total reaches <em>p<\/em>. With <code>top_p=0.9<\/code> it keeps words until 90% is covered. When the model is confident, that may be one word; when it is unsure, it may be many. This makes top-p smarter than a fixed top-k.<\/li>\n<\/ul>\n<figure>\n<svg viewBox=\"0 0 960 250\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Top-p 0.9 diagram: words are sorted by probability and added until their total reaches 90 percent; practice, projects and reading are kept, videos and dancing are removed\" font-family=\"Segoe UI,Arial,sans-serif\">\n<rect width=\"960\" height=\"250\" rx=\"14\" fill=\"#0f1430\"\/>\n<text x=\"30\" y=\"40\" font-size=\"19\" font-weight=\"800\" fill=\"#fff\">top_p = 0.9: keep the smallest group of words that covers 90%<\/text>\n<g class=\"node\"><rect x=\"30\" y=\"70\" width=\"400\" height=\"56\" rx=\"8\" fill=\"#7c5cff\"\/><text x=\"230\" y=\"104\" text-anchor=\"middle\" font-size=\"17\" font-weight=\"700\" fill=\"#fff\">practice 50%<\/text><\/g>\n<g class=\"node\"><rect x=\"434\" y=\"70\" width=\"240\" height=\"56\" rx=\"8\" fill=\"#3178c6\"\/><text x=\"554\" y=\"104\" text-anchor=\"middle\" font-size=\"17\" font-weight=\"700\" fill=\"#fff\">projects 30%<\/text><\/g>\n<g class=\"node\"><rect x=\"678\" y=\"70\" width=\"88\" height=\"56\" rx=\"8\" fill=\"#2aa4c8\"\/><text x=\"722\" y=\"104\" text-anchor=\"middle\" font-size=\"14\" font-weight=\"700\" fill=\"#fff\">reading 11%<\/text><\/g>\n<rect x=\"770\" y=\"70\" width=\"56\" height=\"56\" rx=\"8\" fill=\"#3a4170\"\/><text x=\"798\" y=\"104\" text-anchor=\"middle\" font-size=\"12\" fill=\"#aab4e8\">videos<\/text>\n<rect x=\"830\" y=\"70\" width=\"18\" height=\"56\" rx=\"6\" fill=\"#3a4170\"\/>\n<line x1=\"768\" y1=\"56\" x2=\"768\" y2=\"146\" stroke=\"#ff6b6b\" stroke-width=\"3\" stroke-dasharray=\"6 5\"\/>\n<text x=\"768\" y=\"166\" text-anchor=\"middle\" font-size=\"14\" fill=\"#ff9b9b\">cut-off: 50 + 30 + 11 = 91% \u2265 90%<\/text>\n<text x=\"30\" y=\"205\" font-size=\"16\" fill=\"#c9d1ff\">Kept and re-scaled to 100%: practice, projects, reading<\/text>\n<text x=\"30\" y=\"230\" font-size=\"16\" fill=\"#8a90a6\">Removed: videos, dancing (the &#8220;long tail&#8221; that causes odd words)<\/text>\n<\/svg>\n<figcaption>Top-p (nucleus sampling) sorts the candidate words and keeps adding them until their total probability reaches <code>top_p<\/code>. Everything after the cut-off can never be picked.<\/figcaption>\n<\/figure>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: top_p_demo.py\">\n<input type=\"radio\" name=\"v2\" id=\"v2f1\" class=\"f1\" checked>\n<input type=\"checkbox\" id=\"v2r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">top_p_demo.py \u2014 devdojo-sampling \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v2f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>top_p_demo.py<\/label>\n<label for=\"v2r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">devdojo-sampling \u203a top_p_demo.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># Top-k and top-p (nucleus) sampling on a toy distribution<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">random<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">probs<\/span> = {<span class=\"s\">\"practice\"<\/span>: <span class=\"m\">0.50<\/span>, <span class=\"s\">\"projects\"<\/span>: <span class=\"m\">0.30<\/span>, <span class=\"s\">\"reading\"<\/span>: <span class=\"m\">0.11<\/span>,<\/span>\n<span class=\"l\">         <span class=\"s\">\"videos\"<\/span>: <span class=\"m\">0.07<\/span>, <span class=\"s\">\"dancing\"<\/span>: <span class=\"m\">0.02<\/span>}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">top_k<\/span>(<span class=\"n\">p<\/span>, <span class=\"n\">k<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">keep<\/span> = <span class=\"nb\">sorted<\/span>(<span class=\"n\">p<\/span>.<span class=\"n\">items<\/span>(), <span class=\"n\">key<\/span>=<span class=\"k\">lambda<\/span> <span class=\"n\">x<\/span>: <span class=\"n\">x<\/span>[<span class=\"m\">1<\/span>], <span class=\"n\">reverse<\/span>=<span class=\"kc\">True<\/span>)[:<span class=\"n\">k<\/span>]<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"nb\">dict<\/span>(<span class=\"n\">keep<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">top_p<\/span>(<span class=\"n\">p<\/span>, <span class=\"n\">threshold<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">keep<\/span>, <span class=\"n\">running<\/span> = {}, <span class=\"m\">0.0<\/span><\/span>\n<span class=\"l\">    <span class=\"k\">for<\/span> <span class=\"n\">word<\/span>, <span class=\"n\">prob<\/span> in <span class=\"nb\">sorted<\/span>(<span class=\"n\">p<\/span>.<span class=\"n\">items<\/span>(), <span class=\"n\">key<\/span>=<span class=\"k\">lambda<\/span> <span class=\"n\">x<\/span>: <span class=\"n\">x<\/span>[<span class=\"m\">1<\/span>], <span class=\"n\">reverse<\/span>=<span class=\"kc\">True<\/span>):<\/span>\n<span class=\"l\">        <span class=\"n\">keep<\/span>[<span class=\"n\">word<\/span>] = <span class=\"n\">prob<\/span><\/span>\n<span class=\"l\">        <span class=\"n\">running<\/span> += <span class=\"n\">prob<\/span><\/span>\n<span class=\"l\">        <span class=\"k\">if<\/span> <span class=\"n\">running<\/span> &gt;= <span class=\"n\">threshold<\/span>:  <span class=\"c\"># stop once we cover enough probability<\/span><\/span>\n<span class=\"l\">            <span class=\"k\">break<\/span><\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">keep<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">sample<\/span>(<span class=\"n\">p<\/span>, <span class=\"n\">rng<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">words<\/span>, <span class=\"n\">weights<\/span> = <span class=\"nb\">zip<\/span>(*<span class=\"n\">p<\/span>.<span class=\"n\">items<\/span>())  <span class=\"c\"># choices() renormalises the weights<\/span><\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">rng<\/span>.<span class=\"n\">choices<\/span>(<span class=\"n\">words<\/span>, <span class=\"n\">weights<\/span>=<span class=\"n\">weights<\/span>)[<span class=\"m\">0<\/span>]<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">\"top_k=2   keeps:\"<\/span>, <span class=\"nb\">list<\/span>(<span class=\"n\">top_k<\/span>(<span class=\"n\">probs<\/span>, <span class=\"m\">2<\/span>)))<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">\"top_p=0.9 keeps:\"<\/span>, <span class=\"nb\">list<\/span>(<span class=\"n\">top_p<\/span>(<span class=\"n\">probs<\/span>, <span class=\"m\">0.9<\/span>)))<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">rng<\/span> = <span class=\"n\">random<\/span>.<span class=\"n\">Random<\/span>(<span class=\"m\">42<\/span>)  <span class=\"c\"># a fixed seed makes the demo repeatable<\/span><\/span>\n<span class=\"l\"><span class=\"n\">picks<\/span> = [<span class=\"n\">sample<\/span>(<span class=\"n\">top_p<\/span>(<span class=\"n\">probs<\/span>, <span class=\"m\">0.9<\/span>), <span class=\"n\">rng<\/span>) <span class=\"k\">for<\/span> <span class=\"n\">_<\/span> in <span class=\"nb\">range<\/span>(<span class=\"m\">8<\/span>)]<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">\"8 samples with top_p=0.9:\"<\/span>, <span class=\"n\">picks<\/span>)<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python top_p_demo.py\ntop_k=2   keeps: ['practice', 'projects']\ntop_p=0.9 keeps: ['practice', 'projects', 'reading']\n8 samples with top_p=0.9: ['projects', 'practice', 'practice', 'practice', 'projects', 'projects', 'reading', 'practice']<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 29, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Click <strong>\u25b6 Run<\/strong> to see which words survive each filter and eight real samples.<\/p>\n<p>Notice that &#8220;videos&#8221; and &#8220;dancing&#8221; can never appear in the samples, no matter how many times you run it. That is the job of top-p: cut off the long tail of odd words while still allowing some variety.<\/p>\n<div class=\"dd-tip\"><strong>Rule of thumb:<\/strong> change <em>either<\/em> temperature <em>or<\/em> top-p, and leave the other at its default. Changing both makes results hard to reason about, and some APIs (such as several Claude models) return an error if you send both.<\/div>\n\n<h2 id=\"length-stop\">Max tokens and stop sequences<\/h2>\n<p>The next two settings do not change <em>which<\/em> word is picked. They decide <em>when the answer ends<\/em>, and they work on almost every model, including reasoning models.<\/p>\n<h3>Max tokens<\/h3>\n<p><strong>Max tokens<\/strong> is a hard ceiling on output length. The parameter has different names: <code>max_output_tokens<\/code> in the OpenAI Responses API, <code>max_tokens<\/code> in the Claude Messages API (required there), <code>maxOutputTokens<\/code> in Gemini and <code>num_predict<\/code> in Ollama. It protects your bill and your UI, but it does <strong>not<\/strong> make the model write shorter. It simply cuts the answer off. To get a short <em>complete<\/em> answer, ask for it in the prompt (&#8220;in 2 sentences&#8221;) and keep max tokens as a safety net. On reasoning models, thinking tokens also count toward this limit, so do not set it too low.<\/p>\n<h3>Stop sequences<\/h3>\n<p>A <strong>stop sequence<\/strong> is text that ends generation the moment it appears; the stop text itself is not included in the answer. Use it to stop after one list item, before a second &#8220;Q:&#8221; in a Q&amp;A template, or at a closing tag. Claude calls it <code>stop_sequences<\/code>, Chat Completions-style APIs and Ollama call it <code>stop<\/code>, and Gemini calls it <code>stopSequences<\/code>.<\/p>\n\n<h2 id=\"which-models\">Which models still accept temperature and top-p?<\/h2>\n<p>Here is the twist for modern developers: the newest reasoning models are taking these dials away. When a model &#8220;thinks&#8221; before it answers, the vendor tunes sampling for you. Code written a year ago with <code>temperature=0<\/code> can now fail with a <code>400<\/code> error. This is what the official docs say right now:<\/p>\n<div class=\"dd-tablewrap\"><table>\n<tr><th>Model family<\/th><th>Temperature \/ top-p<\/th><th>What to use instead<\/th><\/tr>\n<tr><td>GPT-6 Luna, GPT-6 Sol (OpenAI)<\/td><td>Allowed only with <code>reasoning.effort: \"none\"<\/code>; otherwise remove them<\/td><td><code>text.verbosity<\/code>, <code>max_output_tokens<\/code>, structured outputs<\/td><\/tr>\n<tr><td>GPT-6 Astra, GPT-6.1 Sol (OpenAI)<\/td><td>Not supported (no <code>none<\/code> effort)<\/td><td>Reasoning effort, verbosity, prompt<\/td><\/tr>\n<tr><td>Claude Opus 4.7 and newer, e.g. Opus 5.5, Sonnet 5<\/td><td>Default only: any other temperature, top_p or top_k returns a 400 error<\/td><td><code>max_tokens<\/code>, <code>stop_sequences<\/code>, clear prompts<\/td><\/tr>\n<tr><td>Gemini 3 (Google)<\/td><td>Accepted, but Google recommends keeping the default 1.0<\/td><td>Thinking level, <code>maxOutputTokens<\/code><\/td><\/tr>\n<tr><td>Open models (Ollama, vLLM)<\/td><td>Fully supported, plus <code>top_k<\/code>, <code>seed<\/code>, penalties<\/td><td>&#8212;<\/td><\/tr>\n<\/table><\/div>\n<p>Sources: OpenAI&#8217;s <a href=\"https:\/\/developers.openai.com\/api\/docs\/guides\/latest-model\" target=\"_blank\" rel=\"noopener\">Using GPT-6 guide<\/a> and Anthropic&#8217;s model migration notes. Rules change often, so check the model page before you ship.<\/p>\n<div class=\"dd-warn\"><strong>Check older Claude models too.<\/strong> Even Claude models from the 4.x generation return <code>temperature and top_p cannot both be specified<\/code> if you send both. Send one or none.<\/div>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"api-code\">Controlling model output with the OpenAI and Claude APIs<\/h2>\n<p>Now let&#8217;s use the real settings. The Python file calls GPT-6 Luna with reasoning effort <code>none<\/code> (so temperature is allowed) and compares <code>temperature=0<\/code> with <code>1.2<\/code>. The Node.js file calls Claude with <code>max_tokens<\/code> and a stop sequence that ends the list before tip 4, and checks <code>stop_reason<\/code> to see <em>why<\/em> the answer ended.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: settings_openai.py, settings-claude.mjs\">\n<input type=\"radio\" name=\"v3\" id=\"v3f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v3\" id=\"v3f2\" class=\"f2\">\n<input type=\"checkbox\" id=\"v3r1\" class=\"r1\">\n<input type=\"checkbox\" id=\"v3r2\" class=\"r2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">2 files \u2014 devdojo-llm-api \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v3f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>settings_openai.py<\/label>\n<label for=\"v3f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>settings-claude.mjs<\/label>\n<label for=\"v3r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<label for=\"v3r2\" class=\"run run2\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">devdojo-llm-api \u203a settings_openai.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># Control GPT-6 Luna's output: randomness, length and detail<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">from<\/span> <span class=\"n\">dotenv<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">load_dotenv<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">from<\/span> <span class=\"n\">openai<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">OpenAI<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">load_dotenv<\/span>()<\/span>\n<span class=\"l\"><span class=\"n\">client<\/span> = <span class=\"n\">OpenAI<\/span>()<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">ask<\/span>(<span class=\"n\">prompt<\/span>, <span class=\"n\">temperature<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">response<\/span> = <span class=\"n\">client<\/span>.<span class=\"n\">responses<\/span>.<span class=\"n\">create<\/span>(<\/span>\n<span class=\"l\">        <span class=\"n\">model<\/span>=<span class=\"s\">\"gpt-6-luna\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"n\">reasoning<\/span>={<span class=\"s\">\"effort\"<\/span>: <span class=\"s\">\"none\"<\/span>},  <span class=\"c\"># temperature works only with \"none\"<\/span><\/span>\n<span class=\"l\">        <span class=\"n\">temperature<\/span>=<span class=\"n\">temperature<\/span>,       <span class=\"c\"># change temperature OR top_p, not both<\/span><\/span>\n<span class=\"l\">        <span class=\"n\">max_output_tokens<\/span>=<span class=\"m\">40<\/span>,          <span class=\"c\"># hard limit on answer length<\/span><\/span>\n<span class=\"l\">        <span class=\"n\">text<\/span>={<span class=\"s\">\"verbosity\"<\/span>: <span class=\"s\">\"low\"<\/span>},     <span class=\"c\"># low \/ medium \/ high detail<\/span><\/span>\n<span class=\"l\">        <span class=\"nb\">input<\/span>=<span class=\"n\">prompt<\/span>,<\/span>\n<span class=\"l\">    )<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">response<\/span>.<span class=\"n\">output_text<\/span>.<span class=\"n\">strip<\/span>()<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">prompt<\/span> = <span class=\"s\">\"Suggest one name for a chai delivery app. Reply with the name only.\"<\/span><\/span>\n<span class=\"l\"><span class=\"k\">for<\/span> <span class=\"n\">t<\/span> in (<span class=\"m\">0.0<\/span>, <span class=\"m\">1.2<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">names<\/span> = [<span class=\"n\">ask<\/span>(<span class=\"n\">prompt<\/span>, <span class=\"n\">t<\/span>) <span class=\"k\">for<\/span> <span class=\"n\">_<\/span> in <span class=\"nb\">range<\/span>(<span class=\"m\">3<\/span>)]<\/span>\n<span class=\"l\">    <span class=\"nb\">print<\/span>(<span class=\"s\">f\"temperature={<\/span><span class=\"n\">t<\/span><span class=\"s\">}:\"<\/span>, <span class=\"n\">names<\/span>)<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Example output \u2013 your names will differ<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python settings_openai.py\ntemperature=0.0: ['ChaiNow', 'ChaiNow', 'ChaiNow']\ntemperature=1.2: ['Kulhad Express', 'ChaiNow', 'Sip Sutra']<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 22, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">devdojo-llm-api \u203a settings-claude.mjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ Claude: control answer length and where the answer stops<\/span><\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> <span class=\"n\">Anthropic<\/span> <span class=\"k\">from<\/span> <span class=\"s\">\"@anthropic-ai\/sdk\"<\/span>;<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">client<\/span> = new <span class=\"n\">Anthropic<\/span>(); <span class=\"c\">\/\/ reads ANTHROPIC_API_KEY<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">msg<\/span> = <span class=\"k\">await<\/span> <span class=\"n\">client<\/span>.<span class=\"n\">messages<\/span>.<span class=\"n\">create<\/span>({<\/span>\n<span class=\"l\">  <span class=\"n\">model<\/span>: <span class=\"n\">process<\/span>.<span class=\"n\">env<\/span>.<span class=\"n\">ANTHROPIC_MODEL<\/span> ?? <span class=\"s\">\"claude-haiku-5-5\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"n\">max_tokens<\/span>: <span class=\"m\">200<\/span>,             <span class=\"c\">\/\/ required: upper limit for the answer<\/span><\/span>\n<span class=\"l\">  <span class=\"n\">stop_sequences<\/span>: [<span class=\"s\">\"\\n4.\"<\/span>],    <span class=\"c\">\/\/ stop before tip number 4<\/span><\/span>\n<span class=\"l\">  <span class=\"c\">\/\/ No temperature or top_p: newer Claude models only accept the defaults<\/span><\/span>\n<span class=\"l\">  <span class=\"n\">messages<\/span>: [<\/span>\n<span class=\"l\">    { <span class=\"n\">role<\/span>: <span class=\"s\">\"user\"<\/span>, <span class=\"n\">content<\/span>: <span class=\"s\">\"Give 5 short tips for learning Git, numbered 1. to 5.\"<\/span> },<\/span>\n<span class=\"l\">  ],<\/span>\n<span class=\"l\">});<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"n\">msg<\/span>.<span class=\"n\">content<\/span>[<span class=\"m\">0<\/span>].<span class=\"n\">text<\/span>);<\/span>\n<span class=\"l\"><span class=\"n\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"s\">\"stop_reason:\"<\/span>, <span class=\"n\">msg<\/span>.<span class=\"n\">stop_reason<\/span>);<\/span>\n<span class=\"l\"><span class=\"n\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"s\">\"stop_sequence:\"<\/span>, <span class=\"nb\">JSON<\/span>.<span class=\"n\">stringify<\/span>(<span class=\"n\">msg<\/span>.<span class=\"n\">stop_sequence<\/span>));<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Example output \u2013 your text will differ<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node --env-file=.env settings-claude.mjs\n1. Commit small changes often, with clear messages.\n2. Learn branches early: create one for every feature.\n3. Run git status before every commit.\nstop_reason: stop_sequence\nstop_sequence: \"\\n4.\"<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 18, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Switch tabs to see both files and click <strong>\u25b6 Run<\/strong>. Your output will differ: that is the point of sampling.<\/p>\n<p>Two habits from this code are worth copying into every project:<\/p>\n<ul>\n<li><strong>Always check why the answer ended.<\/strong> Claude returns <code>stop_reason<\/code> (<code>end_turn<\/code>, <code>max_tokens<\/code> or <code>stop_sequence<\/code>). With OpenAI, a response cut by the limit has <code>status: \"incomplete\"<\/code> with <code>incomplete_details.reason<\/code> set to <code>\"max_output_tokens\"<\/code>. Show a &#8220;continue&#8221; button or retry with a higher limit instead of displaying half an answer.<\/li>\n<li><strong>Keep the model name in config.<\/strong> When you switch from Luna to Astra, temperature stops being allowed. A small settings function (next section) handles this in one place.<\/li>\n<\/ul>\n<h3>Bonus: every dial on a local model<\/h3>\n<p>Open models running on your own machine with Ollama accept all the classic settings. We will set up Ollama properly in Part 6; here is a sneak peek of the options object:<\/p>\n<div class=\"dd-vsc dd-vsc-termonly\" role=\"group\" aria-label=\"Terminal: Terminal \u2014 Ollama (local model)\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">Terminal \u2014 Ollama (local model)<\/span><\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># open models give you every dial (full Ollama guide in Part 6)<\/span><\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> curl http:\/\/localhost:11434\/api\/generate -d '{<\/span>\n<span class=\"l\">  \"model\": \"qwen3:8b\", \"prompt\": \"Name a chai app\", \"stream\": false,<\/span>\n<span class=\"l\">  \"options\": {\"temperature\": 0.2, \"top_p\": 0.9, \"top_k\": 40,<\/span>\n<span class=\"l\">              \"seed\": 42, \"num_predict\": 50, \"stop\": [\"\\n\"]}<\/span>\n<span class=\"l\">}'<\/span><\/code><\/pre>\n<\/div>\n\n<h2 id=\"presets\">Recommended settings by task<\/h2>\n<p>There is no single &#8220;best&#8221; temperature. Pick settings by the <strong>job<\/strong> the model is doing. Click a task:<\/p>\n<div class=\"dd-tabs\" role=\"group\" aria-label=\"Recommended settings by task\">\n<input type=\"radio\" name=\"ti\" id=\"ti1\" checked><input type=\"radio\" name=\"ti\" id=\"ti2\"><input type=\"radio\" name=\"ti\" id=\"ti3\"><input type=\"radio\" name=\"ti\" id=\"ti4\">\n<div class=\"labels\"><label for=\"ti1\">Data extraction<\/label><label for=\"ti2\">Chatbot \/ support<\/label><label for=\"ti3\">Creative writing<\/label><label for=\"ti4\">Code generation<\/label><\/div>\n<div class=\"panel p1\"><div class=\"dd-dial\"><div><b>Temperature<\/b>0 \u2013 0.2<\/div><div><b>Top-p<\/b>1.0 (leave it)<\/div><div><b>Max tokens<\/b>small, e.g. 300<\/div><div><b>Also use<\/b>structured outputs (Part 8)<\/div><\/div><p>You want the <strong>same answer every time<\/strong>: pull a date from an email, classify a ticket, fill a form. Low temperature plus a strict format is the winning combo. On models that reject temperature, rely on the format and examples instead.<\/p><\/div>\n<div class=\"panel p2\"><div class=\"dd-dial\"><div><b>Temperature<\/b>0.5 \u2013 0.8<\/div><div><b>Top-p<\/b>1.0<\/div><div><b>Max tokens<\/b>400 \u2013 800<\/div><div><b>Also use<\/b>verbosity &#8220;low&#8221; or &#8220;medium&#8221;<\/div><\/div><p>Friendly and natural, but not random. A little variety stops the bot from sounding robotic, while staying on topic.<\/p><\/div>\n<div class=\"panel p3\"><div class=\"dd-dial\"><div><b>Temperature<\/b>0.9 \u2013 1.2<\/div><div><b>Top-p<\/b>0.9 \u2013 0.95<\/div><div><b>Max tokens<\/b>generous<\/div><div><b>Also use<\/b>ask for several options<\/div><\/div><p>Taglines, story ideas, app names. Higher temperature gives fresh ideas; a top-p below 1 keeps the really strange words out. Above about 1.3 text often stops making sense.<\/p><\/div>\n<div class=\"panel p4\"><div class=\"dd-dial\"><div><b>Temperature<\/b>0 \u2013 0.3<\/div><div><b>Top-p<\/b>1.0<\/div><div><b>Max tokens<\/b>enough for the whole file<\/div><div><b>Also use<\/b>a stop sequence like a closing code fence<\/div><\/div><p>Code must be correct, not creative. Keep randomness low and make sure the limit is big enough, or the code gets cut off in the middle of a function.<\/p><\/div>\n<\/div>\n<p>In code, keep these presets in one place and strip the settings a model does not accept. This tiny helper runs without any API key:<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: output_presets.py\">\n<input type=\"radio\" name=\"v4\" id=\"v4f1\" class=\"f1\" checked>\n<input type=\"checkbox\" id=\"v4r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">output_presets.py \u2014 devdojo-llm-api \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v4f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>output_presets.py<\/label>\n<label for=\"v4r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">devdojo-llm-api \u203a output_presets.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># Pick output settings by task, and drop the ones a model rejects<\/span><\/span>\n<span class=\"l\"><span class=\"n\">PRESETS<\/span> = {<\/span>\n<span class=\"l\">    <span class=\"s\">\"extract\"<\/span>:  {<span class=\"s\">\"temperature\"<\/span>: <span class=\"m\">0.0<\/span>, <span class=\"s\">\"top_p\"<\/span>: <span class=\"m\">1.0<\/span>, <span class=\"s\">\"max_tokens\"<\/span>: <span class=\"m\">300<\/span>},<\/span>\n<span class=\"l\">    <span class=\"s\">\"chat\"<\/span>:     {<span class=\"s\">\"temperature\"<\/span>: <span class=\"m\">0.7<\/span>, <span class=\"s\">\"top_p\"<\/span>: <span class=\"m\">1.0<\/span>, <span class=\"s\">\"max_tokens\"<\/span>: <span class=\"m\">600<\/span>},<\/span>\n<span class=\"l\">    <span class=\"s\">\"creative\"<\/span>: {<span class=\"s\">\"temperature\"<\/span>: <span class=\"m\">1.0<\/span>, <span class=\"s\">\"top_p\"<\/span>: <span class=\"m\">0.95<\/span>, <span class=\"s\">\"max_tokens\"<\/span>: <span class=\"m\">900<\/span>},<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># Models that only accept the default sampling values<\/span><\/span>\n<span class=\"l\"><span class=\"n\">NO_SAMPLING<\/span> = (<span class=\"s\">\"claude-opus-5\"<\/span>, <span class=\"s\">\"claude-sonnet-5\"<\/span>, <span class=\"s\">\"gpt-6-astra\"<\/span>, <span class=\"s\">\"gpt-6.1-sol\"<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">build_settings<\/span>(<span class=\"n\">task<\/span>, <span class=\"n\">model<\/span>, <span class=\"n\">reasoning<\/span>=<span class=\"s\">\"none\"<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">settings<\/span> = <span class=\"nb\">dict<\/span>(<span class=\"n\">PRESETS<\/span>[<span class=\"n\">task<\/span>])<\/span>\n<span class=\"l\">    <span class=\"n\">fixed<\/span> = <span class=\"n\">model<\/span>.<span class=\"n\">startswith<\/span>(<span class=\"n\">NO_SAMPLING<\/span>)<\/span>\n<span class=\"l\">    <span class=\"k\">if<\/span> <span class=\"n\">fixed<\/span> or (<span class=\"n\">model<\/span>.<span class=\"n\">startswith<\/span>(<span class=\"s\">\"gpt-6\"<\/span>) and <span class=\"n\">reasoning<\/span> != <span class=\"s\">\"none\"<\/span>):<\/span>\n<span class=\"l\">        <span class=\"n\">settings<\/span>.<span class=\"n\">pop<\/span>(<span class=\"s\">\"temperature\"<\/span>)<\/span>\n<span class=\"l\">        <span class=\"n\">settings<\/span>.<span class=\"n\">pop<\/span>(<span class=\"s\">\"top_p\"<\/span>)<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">settings<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">for<\/span> <span class=\"n\">model<\/span> in (<span class=\"s\">\"gpt-6-luna\"<\/span>, <span class=\"s\">\"claude-opus-5-5\"<\/span>, <span class=\"s\">\"qwen3:8b\"<\/span>):<\/span>\n<span class=\"l\">    <span class=\"nb\">print<\/span>(<span class=\"s\">f\"{<\/span><span class=\"n\">model<\/span><span class=\"s\">:&lt;16}\"<\/span>, <span class=\"n\">build_settings<\/span>(<span class=\"s\">\"extract\"<\/span>, <span class=\"n\">model<\/span>))<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">\"gpt-6-luna (reasoning=low)\"<\/span>, <span class=\"n\">build_settings<\/span>(<span class=\"s\">\"chat\"<\/span>, <span class=\"s\">\"gpt-6-luna\"<\/span>, <span class=\"s\">\"low\"<\/span>))<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python output_presets.py\ngpt-6-luna       {'temperature': 0.0, 'top_p': 1.0, 'max_tokens': 300}\nclaude-opus-5-5  {'max_tokens': 300}\nqwen3:8b         {'temperature': 0.0, 'top_p': 1.0, 'max_tokens': 300}\ngpt-6-luna (reasoning=low) {'max_tokens': 600}<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 21, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Click <strong>\u25b6 Run<\/strong>: Claude Opus 5.5 and GPT-6 Luna with reasoning switched on get only the length limit, while the local Qwen model keeps every setting.<\/p>\n\n<h2 id=\"repeatable\">Repeatable output and seeds<\/h2>\n<p>In the <code>top_p_demo.py<\/code> file we used <code>random.Random(42)<\/code>. A <strong>seed<\/strong> fixes the random dice so the same input gives the same picks, which is great for tests and demos. Ollama and most open-model servers accept a <code>seed<\/code> option, and combined with temperature 0 you get near-identical output on the same machine and model version.<\/p>\n<p>Hosted APIs are different: they make no promise of identical output, and many of them do not let you change sampling at all. For reliable results with hosted models, use these instead:<\/p>\n<ol>\n<li><strong>A strict format<\/strong> in the prompt, plus few-shot examples (<a href=\"https:\/\/devdojo.co.in\/?p=240\">Part 4<\/a>).<\/li>\n<li><strong>Structured outputs<\/strong> with a JSON schema, so the answer must fit a shape (Part 8).<\/li>\n<li><strong>Validation in code<\/strong>: check the answer and retry once if it is wrong.<\/li>\n<li><strong>Logging<\/strong> of the model name, settings and prompt version for every request, so you can explain any change in behaviour.<\/li>\n<\/ol>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"common-errors\">Common errors and fixes<\/h2>\n<div class=\"dd-tablewrap\"><table>\n<tr><th>Problem or error<\/th><th>Likely cause<\/th><th>Fix<\/th><\/tr>\n<tr><td><code>Unsupported parameter: 'temperature'<\/code> (OpenAI)<\/td><td>GPT-6 model with reasoning effort other than <code>none<\/code><\/td><td>Remove temperature and top_p, or set <code>reasoning.effort<\/code> to <code>none<\/code> on Luna\/Sol.<\/td><\/tr>\n<tr><td><code>400<\/code> error for temperature on Claude<\/td><td>Newer Claude models accept only default sampling<\/td><td>Delete <code>temperature<\/code>, <code>top_p<\/code> and <code>top_k<\/code> from the request.<\/td><\/tr>\n<tr><td><code>temperature and top_p cannot both be specified<\/code><\/td><td>Both sent to a Claude model<\/td><td>Send only one of them (or none).<\/td><\/tr>\n<tr><td>Answer cut off mid-sentence<\/td><td>Max tokens too low (thinking tokens count too)<\/td><td>Raise the limit, ask for a shorter answer in the prompt, and check <code>stop_reason<\/code> \/ <code>status<\/code>.<\/td><\/tr>\n<tr><td>Answer is empty<\/td><td>Stop sequence appears right at the start (for example <code>\"\\n\"<\/code>)<\/td><td>Use a more specific stop text, such as <code>\"\\n4.\"<\/code> or <code>\"&lt;\/answer&gt;\"<\/code>.<\/td><\/tr>\n<tr><td>Output is random or nonsense<\/td><td>Temperature too high (above about 1.3)<\/td><td>Lower it to 0.7\u20131.0, or add <code>top_p=0.9<\/code> instead.<\/td><\/tr>\n<tr><td>&#8220;temperature=0 but answers still change&#8221;<\/td><td>Hosted servers are not fully deterministic<\/td><td>Use structured outputs and validation; use a seed with local models for tests.<\/td><\/tr>\n<tr><td>Gemini 3 loops or gives weak reasoning<\/td><td>Temperature set well below the default 1.0<\/td><td>Go back to the default and control output with the prompt.<\/td><\/tr>\n<\/table><\/div>\n\n<h2 id=\"best-practices\">Best practices for controlling model output<\/h2>\n<ul>\n<li><strong>Start with the defaults.<\/strong> Change a setting only when you can name the problem it fixes.<\/li>\n<li><strong>Change one dial at a time<\/strong>: temperature <em>or<\/em> top-p, never both.<\/li>\n<li><strong>Control length with the prompt<\/strong> (&#8220;3 bullets&#8221;) and use max tokens only as a safety limit.<\/li>\n<li><strong>Prefer format over randomness tricks<\/strong> for reliable output: clear instructions, examples and structured outputs work on every model.<\/li>\n<li><strong>Keep settings per model in config<\/strong> and strip parameters a model rejects, so a model upgrade does not become an outage.<\/li>\n<li><strong>Handle the stop reason<\/strong> in your UI, so users never see a silently truncated answer.<\/li>\n<li><strong>Test with a fixed set of prompts<\/strong> whenever you change settings or models (more on this in Part 19 on evals).<\/li>\n<\/ul>\n\n<h2 id=\"practice\">Practice task<\/h2>\n<p>Build a tiny &#8220;temperature lab&#8221; for yourself:<\/p>\n<ol>\n<li>Extend <code>temperature_demo.py<\/code> so it also <strong>samples<\/strong> 20 words at each temperature (use <code>random.Random(7)<\/code>) and prints how many times each word was chosen.<\/li>\n<li>Add a <code>top_k<\/code> option and compare <code>top_k=3<\/code> with <code>top_p=0.9<\/code> at <code>T=1.5<\/code>. Which one blocks &#8220;dancing&#8221;?<\/li>\n<li>With an API key, run <code>settings_openai.py<\/code> five times at temperature 0 and five times at 1.2. Count how many unique names you get.<\/li>\n<li>Change the Claude stop sequence so the answer stops after tip 2, and print <code>stop_reason<\/code>.<\/li>\n<li>Add a <code>\"code\"<\/code> preset to <code>output_presets.py<\/code> and a test that checks Claude Opus models never receive <code>temperature<\/code>.<\/li>\n<\/ol>\n<div class=\"dd-tip\"><strong>Bonus:<\/strong> share your most surprising high-temperature output in the comments.<\/div>\n\n<h2 id=\"faq\">FAQ: temperature, top-p and controlling model output<\/h2>\n<details><summary>What is the best temperature for an LLM?<\/summary><p>There is no single best value. Use 0\u20130.2 for extraction and code, about 0.7 for chatbots and 0.9\u20131.2 for creative work. If the model does not accept temperature, keep the default and control output with the prompt.<\/p><\/details>\n<details><summary>What is the difference between temperature and top-p?<\/summary><p>Temperature changes the odds of every word (sharper or flatter). Top-p removes the unlikely words whose total probability falls outside <em>p<\/em>. Both control randomness, so change only one of them.<\/p><\/details>\n<details><summary>Does temperature 0 give the same answer every time?<\/summary><p>Not always. It makes the model pick the most likely token, but hosted APIs can still vary slightly because of how servers compute numbers. Use structured outputs and validation when you need dependable results.<\/p><\/details>\n<details><summary>Why does GPT-6 or Claude reject my temperature setting?<\/summary><p>Reasoning models are tuned by the vendor to work best with default sampling. GPT-6 Luna and Sol accept temperature only when reasoning effort is <code>none<\/code>, and newer Claude models return a 400 error for non-default values.<\/p><\/details>\n<details><summary>Does max tokens make the answer shorter?<\/summary><p>No. It only cuts the answer off when the limit is reached. Ask for a short answer in the prompt and keep max tokens as a safety net.<\/p><\/details>\n<details><summary>What is top-k and should I use it?<\/summary><p>Top-k keeps only the <em>k<\/em> most likely tokens. It is common with open models (Ollama uses 40 by default). Most hosted APIs do not need it; top-p is usually the better filter.<\/p><\/details>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>You now know how an LLM really picks its words, and how to control model output: <strong>temperature<\/strong> sharpens or flattens the odds, <strong>top-p<\/strong> and <strong>top-k<\/strong> cut off unlikely words, <strong>max tokens<\/strong> and <strong>stop sequences<\/strong> decide when the answer ends, and a <strong>seed<\/strong> makes local runs repeatable. Just as important, you know that many modern reasoning models now lock the sampling dials, so the most reliable control is a clear prompt, a strict format and code that checks the answer. Learn more in Google&#8217;s <a href=\"https:\/\/ai.google.dev\/gemini-api\/docs\/prompting-strategies\" target=\"_blank\" rel=\"noopener\">prompt design strategies<\/a>.<\/p>\n<p>Missed the previous part? Read <a href=\"https:\/\/devdojo.co.in\/?p=240\">Part 4: Prompt Engineering Basics<\/a>. Want a broader view of AI tooling? See our <a href=\"https:\/\/devdojo.co.in\/?p=182\">OpenAI Agents SDK tutorial<\/a>.<\/p>\n\n<div class=\"dd-next\"><strong>Next part \u2013 Generative AI Part 6: Running Open Models Locally with Ollama.<\/strong> Install Ollama, download an open model, chat with it from the terminal, and call it from Python and Node.js, with full control over temperature, top-p, seed and more, all free and offline.<\/div>\n\n<div class=\"dd-cta\">\n<h3>New posts every day on DevDojo<\/h3>\n<p>Follow the full <strong>Generative AI series<\/strong> \u2013 30 parts from &#8220;what is an LLM&#8221; to building a production AI assistant. Bookmark <a href=\"https:\/\/devdojo.co.in\/\">devdojo.co.in<\/a> and tell us in the comments which temperature you use most.<\/p>\n<\/div>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.<\/p>\n","protected":false},"author":1,"featured_media":243,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[113,44,133,90,112,134],"class_list":["post-244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","tag-generative-ai","tag-llm","tag-llm-temperature","tag-openai-api","tag-series-generative-ai","tag-top-p-sampling"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Lokendra\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"DevDojo - Empowering Developers\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Generative AI Part 5: Temperature and Top-p for Controlling Model Output\" \/>\n\t\t<meta property=\"og:description\" content=\"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-10-26T13:18:13+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-10-26T13:18:14+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Generative AI Part 5: Temperature and Top-p for Controlling Model Output\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#blogposting\",\"name\":\"Temperature and Top-p: Control LLM Output | DevDojo\",\"headline\":\"Generative AI Part 5: Temperature and Top-p for Controlling Model Output\",\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/genai-series-part-5-temperature-top-p.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Generative AI Part 5 banner: temperature, top-p, max tokens and stop sequence dials controlling LLM output\"},\"datePublished\":\"2026-10-26T13:18:13+00:00\",\"dateModified\":\"2026-10-26T13:18:14+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#webpage\"},\"articleSection\":\"AI, Generative AI, LLM, LLM Temperature, OpenAI API, Series: Generative AI, Top-p Sampling\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"name\":\"AI\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"position\":2,\"name\":\"AI\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#listItem\",\"name\":\"Generative AI Part 5: Temperature and Top-p for Controlling Model Output\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#listItem\",\"position\":3,\"name\":\"Generative AI Part 5: Temperature and Top-p for Controlling Model Output\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"name\":\"AI\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/\",\"name\":\"Lokendra\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Lokendra\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#webpage\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/\",\"name\":\"Temperature and Top-p: Control LLM Output | DevDojo\",\"description\":\"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/genai-series-part-5-temperature-top-p.jpg\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#mainImage\",\"width\":1200,\"height\":630,\"caption\":\"Generative AI Part 5 banner: temperature, top-p, max tokens and stop sequence dials controlling LLM output\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/26\\\/genai-series-part-5-temperature-top-p\\\/#mainImage\"},\"datePublished\":\"2026-10-26T13:18:13+00:00\",\"dateModified\":\"2026-10-26T13:18:14+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Temperature and Top-p: Control LLM Output | DevDojo","description":"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.","canonical_url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#blogposting","name":"Temperature and Top-p: Control LLM Output | DevDojo","headline":"Generative AI Part 5: Temperature and Top-p for Controlling Model Output","author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2026\/10\/genai-series-part-5-temperature-top-p.jpg","width":1200,"height":630,"caption":"Generative AI Part 5 banner: temperature, top-p, max tokens and stop sequence dials controlling LLM output"},"datePublished":"2026-10-26T13:18:13+00:00","dateModified":"2026-10-26T13:18:14+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#webpage"},"isPartOf":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#webpage"},"articleSection":"AI, Generative AI, LLM, LLM Temperature, OpenAI API, Series: Generative AI, Top-p Sampling"},{"@type":"BreadcrumbList","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","position":1,"name":"Home","item":"https:\/\/devdojo.co.in\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","name":"AI"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","position":2,"name":"AI","item":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#listItem","name":"Generative AI Part 5: Temperature and Top-p for Controlling Model Output"},"previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#listItem","position":3,"name":"Generative AI Part 5: Temperature and Top-p for Controlling Model Output","previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","name":"AI"}}]},{"@type":"Organization","@id":"https:\/\/devdojo.co.in\/#organization","name":"DevDojo","description":"Empowering Developers","url":"https:\/\/devdojo.co.in\/"},{"@type":"Person","@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author","url":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/","name":"Lokendra","image":{"@type":"ImageObject","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g","width":96,"height":96,"caption":"Lokendra"}},{"@type":"WebPage","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#webpage","url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/","name":"Temperature and Top-p: Control LLM Output | DevDojo","description":"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/devdojo.co.in\/#website"},"breadcrumb":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#breadcrumblist"},"author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"creator":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2026\/10\/genai-series-part-5-temperature-top-p.jpg","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#mainImage","width":1200,"height":630,"caption":"Generative AI Part 5 banner: temperature, top-p, max tokens and stop sequence dials controlling LLM output"},"primaryImageOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/#mainImage"},"datePublished":"2026-10-26T13:18:13+00:00","dateModified":"2026-10-26T13:18:14+00:00"},{"@type":"WebSite","@id":"https:\/\/devdojo.co.in\/#website","url":"https:\/\/devdojo.co.in\/","name":"DevDojo","description":"Empowering Developers","inLanguage":"en-US","publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"}}]},"og:locale":"en_US","og:site_name":"DevDojo - Empowering Developers","og:type":"article","og:title":"Generative AI Part 5: Temperature and Top-p for Controlling Model Output","og:description":"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.","og:url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/","article:published_time":"2026-10-26T13:18:13+00:00","article:modified_time":"2026-10-26T13:18:14+00:00","twitter:card":"summary_large_image","twitter:title":"Generative AI Part 5: Temperature and Top-p for Controlling Model Output","twitter:description":"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code."},"aioseo_meta_data":{"post_id":"244","title":"Temperature and Top-p: Control LLM Output | DevDojo","description":"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.","keywords":null,"keyphrases":{"focus":{"keyphrase":"temperature and top-p","score":82},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":"Generative AI Part 5: Temperature and Top-p for Controlling Model Output","og_description":"Learn temperature and top-p for LLMs: how sampling works, plus top-k, max tokens, stop sequences and which GPT-6 and Claude models accept them, with code.","og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":0,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"created":"2026-10-26 13:14:27","updated":"2026-10-26 15:42:34","focus_keyword":"temperature and top-p","additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/index.php\/category\/ai\/\" title=\"AI\">AI<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tGenerative AI Part 5: Temperature and Top-p for Controlling Model Output\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/devdojo.co.in\/"},{"label":"AI","link":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/"},{"label":"Generative AI Part 5: Temperature and Top-p for Controlling Model Output","link":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/26\/genai-series-part-5-temperature-top-p\/"}],"_links":{"self":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":1,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions\/245"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media\/243"}],"wp:attachment":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}