<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jidonglab</title>
    <description>The latest articles on DEV Community by jidonglab (@ji_ai).</description>
    <link>https://dev.to/ji_ai</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3791767%2F6eb19afc-a99c-4736-9d12-459108893a16.png</url>
      <title>DEV Community: jidonglab</title>
      <link>https://dev.to/ji_ai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ji_ai"/>
    <language>en</language>
    <item>
      <title>Token Healing: Why a Trailing Space Breaks Your LLM Prompt</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Thu, 26 Nov 2026 09:02:51 +0000</pubDate>
      <link>https://dev.to/ji_ai/token-healing-why-a-trailing-space-breaks-your-llm-prompt-5g6b</link>
      <guid>https://dev.to/ji_ai/token-healing-why-a-trailing-space-breaks-your-llm-prompt-5g6b</guid>
      <description>&lt;p&gt;I once spent an afternoon debugging a prompt that was fine at 11am and garbage at 2pm. The model hadn't changed and neither had the data. The diff turned out to be one character: I had "cleaned up" a template so it ended with &lt;code&gt;Answer:&lt;/code&gt; instead of &lt;code&gt;Answer:&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One trailing space. Outputs started with double spaces, odd capitalization, stray newlines, and wrong answers I couldn't explain. Deleting the space fixed all of it.&lt;/p&gt;

&lt;p&gt;That's the &lt;strong&gt;trailing space problem&lt;/strong&gt;, and the fix has a name: &lt;strong&gt;token healing&lt;/strong&gt;. It comes straight out of how BPE tokenizers work, and once you see the mechanism you'll start spotting it in eval harnesses, code completion, and assistant prefill.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;BPE tokenizers glue the space to the &lt;strong&gt;front&lt;/strong&gt; of the next word: &lt;code&gt;" Paris"&lt;/code&gt; is one token, not &lt;code&gt;" "&lt;/code&gt; + &lt;code&gt;"Paris"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A prompt ending in a space tokenizes that space as a lone token. During training, the model almost never saw a lone space followed by a word token, because the tokenizer always merged them.&lt;/li&gt;
&lt;li&gt;So a trailing space puts the model &lt;strong&gt;off-distribution&lt;/strong&gt; on its very first generated token. Expect double spaces, weird casing, or plain wrong answers.&lt;/li&gt;
&lt;li&gt;Fix: strip trailing whitespace from prompts and put the space in the expected output, or use &lt;strong&gt;token healing&lt;/strong&gt;: back up one token and constrain the first generated token to start with the text you removed.&lt;/li&gt;
&lt;li&gt;The same bug hits logprob scoring (&lt;code&gt;"positive"&lt;/code&gt; vs &lt;code&gt;" positive"&lt;/code&gt;), code completion that ends mid-indent, and URLs that end in &lt;code&gt;http:&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why does a trailing space break an LLM prompt?
&lt;/h2&gt;

&lt;p&gt;A trailing space breaks an LLM prompt because the tokenizer turns it into a standalone space token, and the model was trained on text where that space was almost always fused onto the following word. The model now has to continue from a token sequence it has rarely seen.&lt;/p&gt;

&lt;p&gt;Here's the normal case with a GPT-style BPE tokenizer (GPT-2, &lt;code&gt;cl100k_base&lt;/code&gt;, and Llama 3's tokenizer all behave this way):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"The capital of France is"
-&amp;gt; ["The", " capital", " of", " France", " is"]
next token: " Paris"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every word after the first carries its leading space. That's what the merges learned, because &lt;code&gt;" Paris"&lt;/code&gt; shows up in text constantly, so it earned its own vocabulary slot.&lt;/p&gt;

&lt;p&gt;Now add the space:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"The capital of France is "
-&amp;gt; ["The", " capital", " of", " France", " is", " "]
next token: ???
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model has two bad options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Emit &lt;code&gt;" Paris"&lt;/code&gt; anyway. Now your output reads &lt;code&gt;is  Paris&lt;/code&gt; with a double space.&lt;/li&gt;
&lt;li&gt;Emit &lt;code&gt;"Paris"&lt;/code&gt; without the space. That's a different token, and in training it almost never followed a lone &lt;code&gt;" "&lt;/code&gt; token, because any time the raw text contained &lt;code&gt;is Paris&lt;/code&gt;, the tokenizer produced &lt;code&gt;" is"&lt;/code&gt;, &lt;code&gt;" Paris"&lt;/code&gt;. The pair &lt;code&gt;[" ", "Paris"]&lt;/code&gt; basically doesn't exist in the training data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Either way you've moved probability mass somewhere the model is unsure about. On a frontier model the damage can be small. On a base model or a small local model, it's often visible in a few samples.&lt;/p&gt;

&lt;h2&gt;
  
  
  What exactly is "off-distribution" here?
&lt;/h2&gt;

&lt;p&gt;Off-distribution means the input token sequence is one the model saw rarely or never during training, so its next-token probabilities are poorly calibrated. The text you typed is perfectly normal English. The &lt;strong&gt;token sequence&lt;/strong&gt; is not.&lt;/p&gt;

&lt;p&gt;This is the part that trips people up. You think in characters. The model thinks in token IDs. Two strings that look identical to you can produce sequences that look wildly different to the model, and the tokenizer's greedy merging makes some sequences effectively impossible in training data.&lt;/p&gt;

&lt;p&gt;GPT-2's vocabulary has 50,257 tokens. &lt;code&gt;cl100k_base&lt;/code&gt; has roughly 100k. Llama 3 has 128,256. Bigger vocabularies mean more multi-character merges, and every merge creates new "this split never happens" holes in the space of sequences.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does this bug show up in production?
&lt;/h2&gt;

&lt;p&gt;It shows up anywhere you hand the model a raw string boundary instead of a chat-template boundary. Chat APIs mostly hide it, because the template ends with a fixed assistant header and the model starts fresh. These four places don't hide it.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Completion-style prompts on base models
&lt;/h3&gt;

&lt;p&gt;Anything like &lt;code&gt;Q: ... A:&lt;/code&gt;, &lt;code&gt;Label:&lt;/code&gt;, &lt;code&gt;JSON:&lt;/code&gt;, or &lt;code&gt;Translation:&lt;/code&gt; with a space at the end. Base models and small fine-tunes are the most sensitive. This was my afternoon.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. URLs and other merged punctuation
&lt;/h3&gt;

&lt;p&gt;The classic public example comes from the Guidance library's writeup on prompt boundaries. End a prompt with &lt;code&gt;http:&lt;/code&gt; and ask for the rest of a URL. In common BPE vocabularies &lt;code&gt;://&lt;/code&gt; is a single token, so the model has rarely seen &lt;code&gt;:&lt;/code&gt; followed by &lt;code&gt;//&lt;/code&gt;. It tends to produce something other than the obvious &lt;code&gt;//&lt;/code&gt;, because the path it would normally take starts with a token you already half-consumed.&lt;/p&gt;

&lt;p&gt;The same shape appears with &lt;code&gt;&amp;lt;a href="&lt;/code&gt;, &lt;code&gt;"""&lt;/code&gt;, &lt;code&gt;-&amp;gt;&lt;/code&gt;, &lt;code&gt;==&lt;/code&gt;, and any other high-frequency punctuation cluster that got its own token.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Code completion that ends mid-indent
&lt;/h3&gt;

&lt;p&gt;Code tokenizers have tokens for runs of whitespace. If your editor sends a prompt ending in &lt;code&gt;def foo():\n&lt;/code&gt; (four spaces of indent), you've split a whitespace run that the model normally sees as one unit, plus whatever comes after it. The completion can come back with the wrong indentation level or a strange first token. A lot of code-completion tooling handles this by trimming the prompt back to the last newline and letting the model generate the indentation itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Logprob scoring for classification and evals
&lt;/h3&gt;

&lt;p&gt;This one silently corrupts numbers instead of text. Say you classify by comparing log-probabilities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# wrong: these tokens almost never follow "Sentiment:" in real text&lt;/span&gt;
&lt;span class="nf"&gt;score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Sentiment:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;continuation&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"positive"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Sentiment:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;continuation&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"negative"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# right: the space belongs to the continuation&lt;/span&gt;
&lt;span class="nf"&gt;score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Sentiment:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;continuation&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;" positive"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Sentiment:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;continuation&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;" negative"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scoring &lt;code&gt;"positive"&lt;/code&gt; with no leading space measures how likely the model is to produce a rare token variant. You still get a winner, so nothing crashes. You just get a worse classifier and a benchmark number that understates the model.&lt;/p&gt;

&lt;p&gt;Eval harnesses learned this the hard way. EleutherAI's &lt;code&gt;lm-evaluation-harness&lt;/code&gt;, for example, has a &lt;code&gt;target_delimiter&lt;/code&gt; setting that defaults to a single space, and it tokenizes context and continuation together before splitting them, so the boundary matches what the model saw in training.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does this affect Claude or GPT chat APIs?
&lt;/h2&gt;

&lt;p&gt;Mostly no for normal chat, because the chat template controls the boundary. It matters when you prefill. If you start the assistant turn yourself (&lt;code&gt;{"role": "assistant", "content": "Answer: "}&lt;/code&gt;), you've recreated the raw-string boundary. Anthropic's Messages API refuses this outright: it rejects a final assistant prefill that ends in trailing whitespace. I read that error as a polite version of this whole post.&lt;/p&gt;

&lt;p&gt;If your provider doesn't reject it, nothing warns you. Strip it yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does token healing work?
&lt;/h2&gt;

&lt;p&gt;Token healing removes the last token (or last few) from the prompt, then constrains the first generated token so its text must start with what was removed. The model gets to pick the natural token for the boundary instead of being forced to continue from an awkward split.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;"The capital of France is "&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Tokens end in &lt;code&gt;" is"&lt;/code&gt;, &lt;code&gt;" "&lt;/code&gt;. Remove &lt;code&gt;" "&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Allowed first tokens: anything whose text starts with &lt;code&gt;" "&lt;/code&gt;, which includes &lt;code&gt;" Paris"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The model picks &lt;code&gt;" Paris"&lt;/code&gt;, a sequence it has seen a million times.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For &lt;code&gt;http:&lt;/code&gt;, you'd remove &lt;code&gt;":"&lt;/code&gt; and allow only tokens starting with &lt;code&gt;":"&lt;/code&gt;, which lets the model pick &lt;code&gt;"://"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's a minimal version with Hugging Face &lt;code&gt;transformers&lt;/code&gt;. It's a sketch, not a library, but it's the whole idea:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;transformers&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;AutoTokenizer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AutoModelForCausalLM&lt;/span&gt;

&lt;span class="n"&gt;tok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AutoTokenizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_pretrained&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MODEL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AutoModelForCausalLM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_pretrained&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MODEL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# decode every token id once, up front&lt;/span&gt;
&lt;span class="no"&gt;VOCAB_TEXT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tok&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;ALL_IDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;heal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tok&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;add_special_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tok&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;healed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;VOCAB_TEXT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;healed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt;

&lt;span class="n"&gt;prompt_ids&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;heal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"The capital of France is "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;first_token_only&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;batch_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# constrain step 1, free after that&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;input_ids&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="no"&gt;ALL_IDS&lt;/span&gt;

&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;input_ids&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;prompt_ids&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;prefix_allowed_tokens_fn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;first_token_only&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_new_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two caveats. Backing up a single token isn't always enough. Robust implementations back up several tokens and use a prefix trie over the vocabulary. And the output now includes the text you removed, so strip that prefix before you show it to anyone. Guidance does all of this for you, which is the main reason I'd reach for it before writing my own.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you actually do?
&lt;/h2&gt;

&lt;p&gt;Most teams never need real token healing. They need three habits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strip trailing whitespace from every prompt.&lt;/strong&gt; One &lt;code&gt;.rstrip()&lt;/code&gt; in the template renderer, plus a test that fails if a template ends in a space or tab.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Put the space in the output, not the input.&lt;/strong&gt; Score &lt;code&gt;" positive"&lt;/code&gt;, stop on &lt;code&gt;"\n"&lt;/code&gt;, and expect the model to produce the leading space itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never end a prompt mid-token on purpose.&lt;/strong&gt; Don't end on &lt;code&gt;http:&lt;/code&gt;, half an indent, or the first quote of a string. End on a clean boundary like a newline, or use token healing if you can't.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And when output quality changes after a "harmless" template edit, diff the token IDs, not the strings. &lt;code&gt;tok.encode(old)&lt;/code&gt; vs &lt;code&gt;tok.encode(new)&lt;/code&gt; will show you the problem in seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why does a trailing space break your LLM prompt?
&lt;/h2&gt;

&lt;p&gt;A trailing space breaks an LLM prompt because BPE tokenizers attach spaces to the beginning of the next word, so your space becomes a standalone token that almost never appeared before a word token in training. The model's first generated token then comes from an off-distribution context, which shows up as double spaces, odd formatting, or wrong answers, and as quietly wrong logprob scores in evals. Strip trailing whitespace and let the model generate the space itself. When you must end on a partial token, use token healing: remove the last token and constrain the first generated token to start with the removed text.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>llm</category>
      <category>machinelearning</category>
      <category>ai</category>
      <category>python</category>
    </item>
    <item>
      <title>GitHub Actions paths Filter: Why Required Checks Stay Pending Forever</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Wed, 25 Nov 2026 21:01:02 +0000</pubDate>
      <link>https://dev.to/ji_ai/github-actions-paths-filter-why-required-checks-stay-pending-forever-3gd7</link>
      <guid>https://dev.to/ji_ai/github-actions-paths-filter-why-required-checks-stay-pending-forever-3gd7</guid>
      <description>&lt;p&gt;A one-line README fix sat open in my repo for three days. No red X, no failing test. Just a grey dot next to &lt;code&gt;test&lt;/code&gt; that said &lt;strong&gt;"Expected — Waiting for status to be reported."&lt;/strong&gt; Auto-merge was on. Nothing merged.&lt;/p&gt;

&lt;p&gt;The cause was a GitHub Actions &lt;code&gt;paths&lt;/code&gt; filter combined with a required status check. The filter did its job and skipped CI for a docs-only change. Branch protection did its job too, and waited for a check that was never going to arrive. Two correct features, one deadlocked pull request.&lt;/p&gt;

&lt;p&gt;This is one of those GitHub Actions behaviours that is documented, reproducible, and still bites almost everyone who adds &lt;code&gt;paths:&lt;/code&gt; to a workflow. Here is exactly why the required check stays pending, and the pattern I now use everywhere instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A workflow skipped by &lt;code&gt;on.pull_request.paths&lt;/code&gt;, &lt;code&gt;paths-ignore&lt;/code&gt;, a branch filter, or &lt;code&gt;[skip ci]&lt;/code&gt; &lt;strong&gt;never creates a check run&lt;/strong&gt;, so a required check with that name stays "Pending" forever and blocks the merge.&lt;/li&gt;
&lt;li&gt;A &lt;em&gt;job&lt;/em&gt; skipped by an &lt;code&gt;if:&lt;/code&gt; condition is different: GitHub reports it as &lt;strong&gt;Success&lt;/strong&gt;, and it satisfies a required check.&lt;/li&gt;
&lt;li&gt;The fix: remove workflow-level &lt;code&gt;paths&lt;/code&gt;, detect changes inside a job, skip the expensive jobs with &lt;code&gt;if:&lt;/code&gt;, and make one always-running &lt;strong&gt;gate job&lt;/strong&gt; the only required check.&lt;/li&gt;
&lt;li&gt;Renaming a job or a matrix entry causes the same "Expected" deadlock, because required checks match by name.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why does a GitHub Actions paths filter leave a required check pending?
&lt;/h2&gt;

&lt;p&gt;Because a required status check is just a name that branch protection waits to see reported, and a workflow skipped by a &lt;code&gt;paths&lt;/code&gt; filter never reports anything. GitHub doesn't record "skipped" for it. It records nothing. From branch protection's point of view, CI simply hasn't started yet.&lt;/p&gt;

&lt;p&gt;Here was my workflow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;CI&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'src/**'&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'package.json'&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'package-lock.json'&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm ci&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in branch protection, &lt;code&gt;test&lt;/code&gt; was marked as a required status check.&lt;/p&gt;

&lt;p&gt;Open a PR that touches only &lt;code&gt;README.md&lt;/code&gt;. The workflow trigger evaluates the changed files, none match, and the whole workflow is dropped before a runner is ever requested. No workflow run, no job, no check run named &lt;code&gt;test&lt;/code&gt;. The PR shows &lt;code&gt;test&lt;/code&gt; as "Expected" with no link to click, because there is no run to link to.&lt;/p&gt;

&lt;p&gt;The same thing happens with &lt;code&gt;paths-ignore&lt;/code&gt;, with &lt;code&gt;branches&lt;/code&gt; filters, and with &lt;code&gt;[skip ci]&lt;/code&gt; in the commit message. GitHub's own docs say it plainly: checks for a workflow skipped this way remain in a "Pending" state, and a PR that requires them is blocked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does a job skipped with &lt;code&gt;if:&lt;/code&gt; count as passing?
&lt;/h2&gt;

&lt;p&gt;Because a job skipped by its &lt;code&gt;if:&lt;/code&gt; condition still creates a check run, and that check run concludes as skipped, which GitHub treats as Success for required checks. The docs say a job skipped this way reports "Success" and will not prevent a pull request from merging, even if it is a required check.&lt;/p&gt;

&lt;p&gt;That asymmetry is the whole trick:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;How it was skipped&lt;/th&gt;
&lt;th&gt;Check run created?&lt;/th&gt;
&lt;th&gt;Required check result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Workflow &lt;code&gt;paths&lt;/code&gt; / &lt;code&gt;paths-ignore&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Pending forever&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Workflow &lt;code&gt;branches&lt;/code&gt; filter&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Pending forever&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;[skip ci]&lt;/code&gt; in commit message&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Pending forever&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Job-level &lt;code&gt;if:&lt;/code&gt; evaluates false&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Success&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So the move is obvious once you see the table: stop skipping at the workflow level, and skip at the job level instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I skip CI for docs-only changes without blocking merges?
&lt;/h2&gt;

&lt;p&gt;Run the workflow on every PR, decide inside a cheap first job whether code changed, and gate the expensive jobs with &lt;code&gt;if:&lt;/code&gt;. Then make a single gate job the only required check.&lt;/p&gt;

&lt;p&gt;Here is the version I use now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;CI&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;changes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;outputs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ steps.filter.outputs.code }}&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dorny/paths-filter@v3&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;filter&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
            &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
              &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'src/**'&lt;/span&gt;
              &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'package.json'&lt;/span&gt;
              &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'package-lock.json'&lt;/span&gt;

  &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;changes&lt;/span&gt;
    &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;needs.changes.outputs.code == 'true'&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm ci&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm test&lt;/span&gt;

  &lt;span class="na"&gt;lint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;changes&lt;/span&gt;
    &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;needs.changes.outputs.code == 'true'&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm ci&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm run lint&lt;/span&gt;

  &lt;span class="na"&gt;ci-ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always()&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;changes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;lint&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Fail if any needed job failed or was cancelled&lt;/span&gt;
        &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;exit 1&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;echo "CI gate passed"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in branch protection (or your ruleset), remove &lt;code&gt;test&lt;/code&gt; and &lt;code&gt;lint&lt;/code&gt; from required checks and require only &lt;code&gt;ci-ok&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dorny/paths-filter&lt;/code&gt; is a third-party action. If you'd rather not depend on it, a &lt;code&gt;git diff --name-only&lt;/code&gt; against the base SHA in a shell step does the same job; you just need &lt;code&gt;fetch-depth: 0&lt;/code&gt; or an explicit fetch of the base branch so the diff has something to compare against.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do I need a separate gate job instead of requiring &lt;code&gt;test&lt;/code&gt; directly?
&lt;/h2&gt;

&lt;p&gt;Because a docs-only PR would work fine, but the moment &lt;code&gt;changes&lt;/code&gt; itself fails, &lt;code&gt;test&lt;/code&gt; gets skipped, and a skipped required job counts as Success. You'd merge a PR whose CI never actually ran.&lt;/p&gt;

&lt;p&gt;The gate job closes that hole:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;if: always()&lt;/code&gt; makes &lt;code&gt;ci-ok&lt;/code&gt; run even when upstream jobs fail or skip. Without it, a failure upstream would skip &lt;code&gt;ci-ok&lt;/code&gt;, and a skipped required job passes. That is the exact opposite of what you want.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;needs.*.result&lt;/code&gt; gives you the result of every job in &lt;code&gt;needs&lt;/code&gt;. A docs PR produces &lt;code&gt;success, skipped, skipped&lt;/code&gt;, which passes. A real failure produces &lt;code&gt;failure&lt;/code&gt; somewhere, and the gate exits 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;changes&lt;/code&gt; is in the &lt;code&gt;needs&lt;/code&gt; list on purpose. If the filter job breaks, the gate fails instead of quietly waving the PR through.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The other benefit is boring and real: you never edit branch protection again. Add a &lt;code&gt;typecheck&lt;/code&gt; job next month, put it in &lt;code&gt;ci-ok&lt;/code&gt;'s &lt;code&gt;needs&lt;/code&gt;, done. The required check list stays at one entry.&lt;/p&gt;

&lt;h2&gt;
  
  
  What else causes "Expected — Waiting for status to be reported"?
&lt;/h2&gt;

&lt;p&gt;Renaming things. Required status checks are matched by check name, and for GitHub Actions that name is the job's &lt;code&gt;name:&lt;/code&gt; (or its job ID if there's no name). Change &lt;code&gt;test&lt;/code&gt; to &lt;code&gt;unit-tests&lt;/code&gt; and every open PR now waits for a &lt;code&gt;test&lt;/code&gt; check that no workflow produces anymore.&lt;/p&gt;

&lt;p&gt;Matrix jobs are worse, because their check names include the matrix values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That produces checks named &lt;code&gt;test (18)&lt;/code&gt; and &lt;code&gt;test (20)&lt;/code&gt;. Bump the matrix to &lt;code&gt;[20, 22]&lt;/code&gt; and &lt;code&gt;test (18)&lt;/code&gt; becomes a required check that will never report again. Every PR blocks until someone updates branch protection. The gate-job pattern fixes this too, since the matrix lives behind &lt;code&gt;ci-ok&lt;/code&gt; and its names stop mattering.&lt;/p&gt;

&lt;p&gt;One more sharp edge from the name matching: if two workflows both have a job called &lt;code&gt;test&lt;/code&gt;, either one reporting success can satisfy a required check named &lt;code&gt;test&lt;/code&gt;. Old advice for this exact problem was to create a duplicate no-op workflow with the inverse &lt;code&gt;paths-ignore&lt;/code&gt; and the same job name, so docs PRs got a fake green &lt;code&gt;test&lt;/code&gt;. It works, but it's two files to keep in sync, and it means a trivial &lt;code&gt;echo&lt;/code&gt; job can satisfy your most important check. I stopped doing that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Doesn't running the workflow on every PR waste minutes?
&lt;/h2&gt;

&lt;p&gt;A little, and much less than you'd think. A docs-only PR now starts the workflow, runs the &lt;code&gt;changes&lt;/code&gt; job (a few seconds of a runner), skips &lt;code&gt;test&lt;/code&gt; and &lt;code&gt;lint&lt;/code&gt; without allocating a runner for them, and runs &lt;code&gt;ci-ok&lt;/code&gt; for a few seconds. That's two tiny jobs instead of zero.&lt;/p&gt;

&lt;p&gt;Compared with a PR blocked for three days, or someone with admin rights clicking "merge without waiting for requirements" until it becomes a habit, that's a cheap trade. The admin bypass habit is the real cost. Once people learn the button exists, they'll use it on a PR where CI actually mattered.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick checklist before you add &lt;code&gt;paths:&lt;/code&gt; to a workflow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Is any job in this workflow a required status check? If yes, don't put &lt;code&gt;paths&lt;/code&gt; on the workflow trigger.&lt;/li&gt;
&lt;li&gt;Move the path logic into a &lt;code&gt;changes&lt;/code&gt; job and gate jobs with &lt;code&gt;if:&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add an &lt;code&gt;if: always()&lt;/code&gt; gate job that fails on &lt;code&gt;failure&lt;/code&gt; or &lt;code&gt;cancelled&lt;/code&gt; in &lt;code&gt;needs.*.result&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Make the gate job the only required check.&lt;/li&gt;
&lt;li&gt;Keep job names stable, and never require matrix-expanded check names directly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Workflow-level &lt;code&gt;paths&lt;/code&gt; is still fine for workflows that are &lt;em&gt;not&lt;/em&gt; required: a docs site deploy, a nightly benchmark, a label bot. The deadlock only exists when a required check depends on a workflow that might not start.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why does the GitHub Actions paths filter leave required checks pending forever?
&lt;/h2&gt;

&lt;p&gt;A GitHub Actions paths filter skips the entire workflow before any job is created, so no check run with the required name is ever reported, and branch protection waits for it indefinitely as "Expected — Waiting for status to be reported." Jobs skipped by a job-level &lt;code&gt;if:&lt;/code&gt; behave differently: they create a check run, conclude as skipped, and count as Success. The reliable fix is to trigger the workflow on every pull request, detect changed paths inside a first job, skip expensive jobs with &lt;code&gt;if:&lt;/code&gt;, and require only a single &lt;code&gt;if: always()&lt;/code&gt; gate job that fails when any needed job failed or was cancelled.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>tooling</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>LLM-as-a-Judge Position Bias: Claude Flipped 103 of 400 Verdicts</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Wed, 25 Nov 2026 08:59:46 +0000</pubDate>
      <link>https://dev.to/ji_ai/llm-as-a-judge-position-bias-claude-flipped-103-of-400-verdicts-1i9e</link>
      <guid>https://dev.to/ji_ai/llm-as-a-judge-position-bias-claude-flipped-103-of-400-verdicts-1i9e</guid>
      <description>&lt;p&gt;My new prompt won 63% of the time. I had the Slack message half-typed. Then I swapped which answer went first, ran the exact same 400 comparisons again, and the new prompt won 43%.&lt;/p&gt;

&lt;p&gt;Same judge. Same pairs. Same rubric. The only change was which answer appeared under "Response A". That is &lt;strong&gt;LLM-as-a-judge position bias&lt;/strong&gt;, and it had been quietly deciding my A/B tests for weeks.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;I ran 400 pairwise comparisons with Claude as the judge, then ran all 400 again with the answer order swapped. 800 judge calls total.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;103 of 400 verdicts (25.75%) flipped&lt;/strong&gt; when I swapped the order. 92 of those flips went to whichever answer was shown first.&lt;/li&gt;
&lt;li&gt;The new prompt looked like a 63% winner. Counting only verdicts that survived the swap, it won 160 to 137, about 54%. A small edge, not a landslide.&lt;/li&gt;
&lt;li&gt;On a blind sample I labeled myself, most flipped pairs were ones I'd call a tie. A flip is the judge's way of saying "I can't tell."&lt;/li&gt;
&lt;li&gt;Fix: always judge both orders, treat disagreement as a tie, and give the judge an explicit TIE option.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What was I actually testing?
&lt;/h2&gt;

&lt;p&gt;I was A/B testing two versions of a report-writing prompt. The system takes an interview transcript and writes a feedback report: what went well, what was weak, what to practice next.&lt;/p&gt;

&lt;p&gt;This is the report generator inside Preterview, an interview prep tool I built and run (full disclosure: I built it). It runs voice mock interviews with three interviewer styles, scores a resume or portfolio, and ends with a written report, which is the part I was trying to improve. If you want to see what the reports look like, it's at &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;preterview.com/en&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftd5zed80xfjpv7qjalsn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftd5zed80xfjpv7qjalsn.jpg" alt="Preterview — an interview session in progress" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Prompt v2 was supposed to make reports more specific: quote the candidate's actual words, tie every criticism to a moment in the transcript. I generated reports from v1 and v2 for the same 400 transcripts, then asked Claude to pick the better one.&lt;/p&gt;

&lt;p&gt;The obvious setup: new report in slot A, old report in slot B, ask "which is better?" One order, one verdict per pair. That single fixed order is exactly why v2 looked so good.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is LLM-as-a-judge position bias?
&lt;/h2&gt;

&lt;p&gt;LLM-as-a-judge position bias is the tendency of a model acting as an evaluator to prefer an answer because of where it appears in the prompt, not because of what it says. In pairwise comparisons, the judge favors slot A or slot B more often than the content justifies. It's a well-documented effect in the LLM evaluation literature, and I still walked straight into it.&lt;/p&gt;

&lt;p&gt;The nasty part is that each individual verdict looks reasonable. The judge writes a confident paragraph explaining why Response A is more specific. You read it and nod. Nothing in a single output tells you the reasoning was built after the slot already won.&lt;/p&gt;

&lt;h2&gt;
  
  
  How did I measure it?
&lt;/h2&gt;

&lt;p&gt;I ran every pair twice: once with v2 in slot A, once with v2 in slot B. Same system prompt, same rubric, forced choice between A and B. Here's the core of the harness:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;anthropic&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;anthropic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Anthropic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;JUDGE_PROMPT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"""You are grading two interview feedback reports
written for the same transcript. Judge on: specificity (quotes
the candidate's words), accuracy (claims match the transcript),
and actionability (clear next steps).

&amp;lt;transcript&amp;gt;{transcript}&amp;lt;/transcript&amp;gt;
&amp;lt;response_a&amp;gt;{a}&amp;lt;/response_a&amp;gt;
&amp;lt;response_b&amp;gt;{b}&amp;lt;/response_b&amp;gt;

Reply with exactly one line: WINNER: A or WINNER: B"""&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;judge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transcript&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;JUDGE_MODEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;max_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;JUDGE_PROMPT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;transcript&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;transcript&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# "A" or "B"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;judge_both_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transcript&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;judge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transcript&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# new in slot A&lt;/span&gt;
    &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;judge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transcript&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# new in slot B&lt;/span&gt;
    &lt;span class="n"&gt;new_won_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"A"&lt;/span&gt;
    &lt;span class="n"&gt;new_won_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"B"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;new_won_1&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;new_won_2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"new"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;new_won_1&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;new_won_2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"old"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"flip"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each call carried a transcript excerpt plus two full reports, roughly 5,200 input tokens. 800 calls came to about 4.2M input tokens. Not free, but cheap next to shipping the wrong prompt to every user.&lt;/p&gt;

&lt;h2&gt;
  
  
  How many verdicts flipped when I swapped the order?
&lt;/h2&gt;

&lt;p&gt;103 of 400 verdicts flipped, a 25.75% flip rate. One in four comparisons gave a different winner depending only on which report was printed first.&lt;/p&gt;

&lt;p&gt;Here's the full breakdown:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Outcome&lt;/th&gt;
&lt;th&gt;Pairs&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;v2 wins in both orders&lt;/td&gt;
&lt;td&gt;160&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v1 wins in both orders&lt;/td&gt;
&lt;td&gt;137&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flipped toward slot A&lt;/td&gt;
&lt;td&gt;92&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flipped toward slot B&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;400&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So the flips weren't random noise. 92 of 103 went to whoever sat in slot A. If the judge were just uncertain and guessing, you'd expect something close to a 50/50 split between the two flip directions. 92 to 11 is a thumb on the scale.&lt;/p&gt;

&lt;p&gt;Now look at what that did to the headline numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Run 1 (v2 in slot A):&lt;/strong&gt; v2 won 252 of 400. That's 160 consistent wins plus 92 slot-A flips. 63%.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run 2 (v2 in slot B):&lt;/strong&gt; v2 won 171 of 400. That's 160 plus the 11 slot-B flips. 42.75%.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Swap-consistent only:&lt;/strong&gt; v2 won 160, v1 won 137. 53.9% of decided pairs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My first run happened to put v2 in slot A. I was about to announce a 63% win rate for a prompt whose real edge was closer to 54%. If I'd randomly done it the other way around, I would have thrown out a prompt that was actually slightly better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Are flipped verdicts just ties in disguise?
&lt;/h2&gt;

&lt;p&gt;Mostly, yes. I took 50 random pairs, hid which prompt produced which report, and picked the winner myself before looking at any judge output.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;13 of the 50 had flipped verdicts, which tracks the 26% overall rate.&lt;/li&gt;
&lt;li&gt;On the 37 pairs with a swap-consistent verdict, the judge agreed with me on 31 (84%).&lt;/li&gt;
&lt;li&gt;Of the 13 flipped pairs, I marked 9 as "honestly, these are about the same."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That changed how I read the whole result. Position bias doesn't mostly override clear quality differences. It fills the vacuum when there isn't one. When two reports are close, the judge needs some tiebreaker, and the tiebreaker it reaches for is "the first one I read."&lt;/p&gt;

&lt;p&gt;Which means a forced-choice prompt is lying to you twice. It hides ties, and then it resolves those hidden ties in a biased direction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does adding a TIE option fix position bias?
&lt;/h2&gt;

&lt;p&gt;It helps a lot but doesn't eliminate it. I changed the output line to &lt;code&gt;WINNER: A&lt;/code&gt;, &lt;code&gt;WINNER: B&lt;/code&gt;, or &lt;code&gt;WINNER: TIE&lt;/code&gt; and reran all 400 pairs in both orders.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;88 pairs got a TIE in at least one order.&lt;/li&gt;
&lt;li&gt;Strict flips, where one order said A and the other said B, dropped from 103 to 31.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;31 is still 31. The judge is still nudged by order on close calls, it just has a more honest exit now. That's why I don't trust a tie option by itself. The swap is the part that does the real work.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should you run pairwise LLM evals now?
&lt;/h2&gt;

&lt;p&gt;Here's my current setup, and it's the one I'd recommend to anyone A/B testing prompts with a model as the grader:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Always judge both orders.&lt;/strong&gt; It doubles the cost. Pay it. A single-order pairwise eval tells you about slot preference as much as about quality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count a flip as a tie.&lt;/strong&gt; Only swap-consistent verdicts go in the win column. Report the tie count next to the win rate so nobody reads 54% as 54-out-of-100-decisive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Offer an explicit TIE.&lt;/strong&gt; It catches the close calls before they become flips.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hand-label a small blind sample.&lt;/strong&gt; 50 pairs took me under an hour and told me more than the other 750 calls about what the judge's disagreement meant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Track the flip rate as a metric.&lt;/strong&gt; If it jumps after you edit the judge prompt, your judge got worse, even if the win rates look exciting.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The thing I keep coming back to: none of this showed up in any single output. Every verdict came with a tidy justification. The bias only appeared when I ran the same question twice and compared.&lt;/p&gt;

&lt;h2&gt;
  
  
  So how bad is LLM-as-a-judge position bias with Claude?
&lt;/h2&gt;

&lt;p&gt;In my setup, bad enough to flip a decision. Swapping answer order changed the winner on 103 of 400 pairwise comparisons (25.75%), and 92 of those flips favored the answer shown first. It turned a real 54% edge into either a 63% win or a 43% loss depending on which slot the new prompt landed in. The flips clustered on pairs that were close in quality, so the fix is cheap and mechanical: run every comparison in both orders, count disagreements as ties, give the judge a TIE option, and spot-check a blind sample by hand. If your LLM-as-a-judge eval only runs one order, your win rate is partly measuring position.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>llm</category>
      <category>ai</category>
      <category>python</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>requests timeout=30 Isn't a Deadline: One LLM Stream Ran 31 Minutes</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Sat, 21 Nov 2026 08:57:59 +0000</pubDate>
      <link>https://dev.to/ji_ai/requests-timeout30-isnt-a-deadline-one-llm-stream-ran-31-minutes-42ea</link>
      <guid>https://dev.to/ji_ai/requests-timeout30-isnt-a-deadline-one-llm-stream-ran-31-minutes-42ea</guid>
      <description>&lt;p&gt;I set &lt;code&gt;timeout=30&lt;/code&gt; on every request in my batch job. One of those requests ran for 31 minutes, and nothing in the stack considered that a problem.&lt;/p&gt;

&lt;p&gt;No exception and no log line. One of my eight worker threads sat there, receiving a few bytes every 15 seconds, for half an hour. The Python requests timeout did exactly what it's documented to do. I just hadn't read the documentation closely, and I suspect most people calling LLM streaming endpoints with &lt;code&gt;requests&lt;/code&gt; haven't either.&lt;/p&gt;

&lt;p&gt;Here's the setup, the numbers, and the fix that brought my worst case from 31 minutes down to 105 seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Python requests &lt;code&gt;timeout&lt;/code&gt; is not a total deadline.&lt;/strong&gt; It caps how long the client waits for the &lt;em&gt;next&lt;/em&gt; bytes from the socket. Every byte that arrives resets the clock.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Streaming LLM responses never trip it.&lt;/strong&gt; Tokens trickle in every few hundred milliseconds, so a 9-minute generation never goes 30 seconds without a byte.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SSE keep-alive pings make it worse.&lt;/strong&gt; My proxy (sse-starlette) sends a &lt;code&gt;: ping&lt;/code&gt; comment every 15 seconds by default. A stalled upstream looked alive forever.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;In 2,000 streamed calls: 49 ran past 30s, 0 raised &lt;code&gt;ReadTimeout&lt;/code&gt;, the longest ran 31 minutes.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix:&lt;/strong&gt; keep the read timeout, add your own wall-clock deadline checked inside the stream loop (or use &lt;code&gt;asyncio.timeout&lt;/code&gt; with httpx). Worst case becomes deadline + one read interval.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;The job: summarize and tag 2,000 support transcripts with a self-hosted model. The pieces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A vLLM server running an 8B instruct model on one GPU&lt;/li&gt;
&lt;li&gt;A small FastAPI gateway in front of it that handles auth and re-emits the stream using &lt;code&gt;sse-starlette&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A Python client using &lt;code&gt;requests.post(..., stream=True, timeout=30)&lt;/code&gt; with a &lt;code&gt;ThreadPoolExecutor&lt;/code&gt; of 8 workers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;max_tokens=8192&lt;/code&gt;, because some transcripts were long and I was lazy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My mental model was "any single call that takes over 30 seconds gets killed, so the job finishes in about 25 minutes." The job took 58 minutes. That's what made me start logging.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the timeout parameter in Python requests actually do?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;timeout&lt;/code&gt; in Python requests is two separate idle timers: a connect timeout (how long to wait to establish the TCP connection) and a read timeout (how long to wait between bytes from the server). Passing &lt;code&gt;timeout=30&lt;/code&gt; sets both to 30 seconds. Neither one limits the total duration of the request.&lt;/p&gt;

&lt;p&gt;The requests docs say this plainly: the read timeout is the number of seconds the client will wait for the server to send a byte. Strictly speaking it's the time between socket reads. A server that sends one byte every 29 seconds can keep a request open until the heat death of the universe.&lt;/p&gt;

&lt;p&gt;For a normal JSON API this barely matters. The server computes, then dumps the whole response at once. Idle time and total time are roughly the same thing.&lt;/p&gt;

&lt;p&gt;For a streaming LLM endpoint they are completely different things.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why don't streaming LLM responses ever hit the read timeout?
&lt;/h2&gt;

&lt;p&gt;Streaming LLM responses send a new chunk every time the model emits a token, typically every 20 to 100 milliseconds. The read timer resets on each chunk, so the request only times out if the model goes silent for the full timeout window. A slow or endless generation never does.&lt;/p&gt;

&lt;p&gt;I added timing to every call: start time, first-byte time, end time, bytes received, and how many of those bytes were SSE comment lines. Here's what 2,000 calls looked like:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bucket&lt;/th&gt;
&lt;th&gt;Calls&lt;/th&gt;
&lt;th&gt;What was happening&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Finished under 30s&lt;/td&gt;
&lt;td&gt;1,951&lt;/td&gt;
&lt;td&gt;Normal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;30s to 2 min&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;Long transcripts, legitimately long outputs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4 to 9 min&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Repetition loops running all the way to 8,192 tokens&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;31 min&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Upstream stalled, proxy kept pinging&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Raised &lt;code&gt;ReadTimeout&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Zero. Not one call in 2,000 tripped the timeout I thought was protecting me.&lt;/p&gt;

&lt;p&gt;The 8 repetition loops were the classic small-model failure: the summary ends, then the model starts repeating its last bullet point, over and over, until it hits &lt;code&gt;max_tokens&lt;/code&gt;. At around 30 tokens per second under load, 8,192 tokens takes about four and a half minutes. The slowest one took 9 minutes and 14 seconds because it overlapped with the other loops and throughput dropped.&lt;/p&gt;

&lt;p&gt;None of those were a timeout bug in the strict sense. They were the timeout doing its job while I expected it to do a different job.&lt;/p&gt;

&lt;h2&gt;
  
  
  How did one request run for 31 minutes?
&lt;/h2&gt;

&lt;p&gt;One request ran for 31 minutes because the gateway kept the connection alive with SSE keep-alive comments after the model server stopped sending tokens. Each &lt;code&gt;: ping&lt;/code&gt; line arrived every 15 seconds, well inside the 30-second read timeout, so the client treated a dead stream as a healthy one.&lt;/p&gt;

&lt;p&gt;This is the one that actually hurt. &lt;code&gt;sse-starlette&lt;/code&gt; sends a ping comment every 15 seconds by default. It's a good default: it stops load balancers and browsers from killing idle connections. But it means the gateway is telling the client "still here!" regardless of whether anything upstream is happening.&lt;/p&gt;

&lt;p&gt;What happened upstream: the vLLM server hit a rough patch (I never fully pinned it down; it lined up with the 8 repetition loops hogging the batch), and one request stopped producing tokens. The gateway's own call to vLLM had no read timeout configured. So:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;vLLM goes quiet on one request&lt;/li&gt;
&lt;li&gt;The gateway waits forever on its upstream read&lt;/li&gt;
&lt;li&gt;sse-starlette keeps emitting &lt;code&gt;: ping&lt;/code&gt; every 15 seconds on the downstream side&lt;/li&gt;
&lt;li&gt;The client's 30-second read timer resets every 15 seconds&lt;/li&gt;
&lt;li&gt;Nobody ever gives up&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My logs showed that request receiving 124 ping comments and zero data events. 124 × 15 seconds is 31 minutes. I killed it by hand when I noticed one worker hadn't logged anything since the first coffee.&lt;/p&gt;

&lt;p&gt;That one stuck request held one of my 8 workers for over half the job's runtime. The repetition loops held more. Together they explain most of the gap between the 25 minutes I expected and the 58 minutes I got.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you set a total timeout for a Python requests stream?
&lt;/h2&gt;

&lt;p&gt;Python requests has no built-in total timeout, so you enforce a wall-clock deadline yourself. Record &lt;code&gt;time.monotonic()&lt;/code&gt; before the call, check it inside the loop that reads the stream, and close the response once the deadline passes. Keep the read timeout too, because it covers the case where no bytes arrive at all.&lt;/p&gt;

&lt;p&gt;Here's the client loop after the fix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="no"&gt;READ_TIMEOUT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;      &lt;span class="c1"&gt;# max silence between bytes&lt;/span&gt;
&lt;span class="no"&gt;DEADLINE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;          &lt;span class="c1"&gt;# max total wall-clock time&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeadlineExceeded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stream_completion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;READ_TIMEOUT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raise_for_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;iter_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decode_unicode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;DEADLINE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;DeadlineExceeded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{DEADLINE}s exceeded"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;  &lt;span class="c1"&gt;# blank separator or keep-alive ping&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"data: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"[DONE]"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;break&lt;/span&gt;
                &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The deadline check runs before the ping filter.&lt;/strong&gt; If you &lt;code&gt;continue&lt;/code&gt; on comment lines before checking the clock, a stream of pings never reaches your deadline check. That's the exact bug I was fixing, so I'd rather not reintroduce it one line later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The deadline is only checked when a line arrives.&lt;/strong&gt; If the server goes fully silent, the loop blocks in &lt;code&gt;iter_lines&lt;/code&gt; and only the read timeout can save you. That's why you need both. Your real worst case is &lt;code&gt;DEADLINE + READ_TIMEOUT&lt;/code&gt;, or &lt;code&gt;DEADLINE + ping interval&lt;/code&gt; if pings are flowing. With my numbers: 90 + 15 = 105 seconds.&lt;/p&gt;

&lt;p&gt;If you need a hard cap that doesn't depend on bytes arriving, a watchdog thread that calls &lt;code&gt;resp.close()&lt;/code&gt; after the deadline works in my testing, but closing a socket from another thread is the kind of thing I'd rather not rely on. If you can go async, it's cleaner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stream_completion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AsyncClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# Python 3.11+&lt;/span&gt;
        &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"POST"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                 &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;httpx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aiter_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that httpx's &lt;code&gt;Timeout&lt;/code&gt; has the same shape as requests: connect, read, write, and pool timeouts, all per-operation. None of them is a total deadline. &lt;code&gt;asyncio.timeout&lt;/code&gt; is what actually cancels the whole thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The other two fixes
&lt;/h2&gt;

&lt;p&gt;The client deadline handles symptoms. Two more changes handled causes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Gateway upstream timeout.&lt;/strong&gt; The FastAPI gateway now has a 30-second read timeout on its own call to vLLM. If upstream goes silent, the gateway closes the stream and sends an error event instead of pinging forever. Keep-alive pings should mean "I'm alive &lt;em&gt;and&lt;/em&gt; my upstream is alive," and that only holds if the gateway is enforcing the second half.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;max_tokens&lt;/code&gt; sized to the task.&lt;/strong&gt; My summaries never legitimately exceeded about 900 tokens. I dropped &lt;code&gt;max_tokens&lt;/code&gt; from 8,192 to 2,048. A repetition loop now costs about a minute of GPU time instead of four to nine.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The rerun
&lt;/h2&gt;

&lt;p&gt;Same 2,000 transcripts, same model, same 8 workers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Job time:&lt;/strong&gt; 58 minutes → 27 minutes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Longest single call:&lt;/strong&gt; 31 minutes → 104.8 seconds&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Calls stopped by the deadline:&lt;/strong&gt; 11 (8 repetition loops, 2 slow long answers, 1 upstream stall caught by the gateway first)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ReadTimeout&lt;/code&gt; raised:&lt;/strong&gt; still 0 on the client, which is now fine, because that's not the timer doing the real work&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The 2 slow long answers that hit the deadline were real losses. I retry deadline failures once with a shorter prompt, and both succeeded on retry. A 90-second deadline is a policy choice: pick it from your own latency distribution, not from my numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  So is requests timeout=30 a deadline?
&lt;/h2&gt;

&lt;p&gt;No. In Python requests, &lt;code&gt;timeout=30&lt;/code&gt; means the client gives up if the server goes 30 seconds without sending a byte; it does not cap total request time. For streaming LLM calls, tokens and SSE keep-alive pings reset that timer constantly, so a stream can run for minutes or, in my case, 31 minutes without ever raising &lt;code&gt;ReadTimeout&lt;/code&gt;. To get a real deadline, track elapsed time with &lt;code&gt;time.monotonic()&lt;/code&gt; inside your stream loop (before filtering pings) and abort when it's exceeded, or wrap an async httpx stream in &lt;code&gt;asyncio.timeout&lt;/code&gt;. Keep the read timeout as well, and put a read timeout on any proxy that emits keep-alive pings, or a dead stream will look healthy until somebody notices it.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>llm</category>
      <category>ai</category>
      <category>performance</category>
    </item>
    <item>
      <title>INT4 Absmax Quantization: One Outlier Zeroed 4,095 of 4,096 Values</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Fri, 20 Nov 2026 20:56:25 +0000</pubDate>
      <link>https://dev.to/ji_ai/int4-absmax-quantization-one-outlier-zeroed-4095-of-4096-values-bhp</link>
      <guid>https://dev.to/ji_ai/int4-absmax-quantization-one-outlier-zeroed-4095-of-4096-values-bhp</guid>
      <description>&lt;p&gt;I planted one number in a vector of 4,096 floats. I set it to 60, quantized the vector to INT4 with absmax scaling, and looked at what was left.&lt;/p&gt;

&lt;p&gt;All 4,095 of the other values were exactly zero.&lt;/p&gt;

&lt;p&gt;Not "a bit noisy." Zero. The whole row went down to one integer level, plus the outlier. That one experiment explains most of what goes wrong in LLM quantization: why per-tensor INT4 is never shipped, why GPTQ and AWQ default to &lt;code&gt;group_size=128&lt;/code&gt;, and why bitsandbytes has a &lt;code&gt;llm_int8_threshold=6.0&lt;/code&gt; knob that most people never touch.&lt;/p&gt;

&lt;p&gt;This post walks through &lt;strong&gt;absmax quantization&lt;/strong&gt;, the outlier failure mode, and the three fixes real libraries use. Everything here ran on my laptop with numpy, and the script is short enough to paste.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Absmax quantization&lt;/strong&gt; sets one scale per block: &lt;code&gt;scale = max(|x|) / qmax&lt;/code&gt;. The largest value in the block decides the step size for every other value.&lt;/li&gt;
&lt;li&gt;One outlier of 60 in 4,096 standard-normal values pushed the INT4 step to 8.57. Everything with &lt;code&gt;|x| &amp;lt; 4.29&lt;/code&gt; rounds to 0, so &lt;strong&gt;4,095 of 4,096 values became zero&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;INT8 survives better but still breaks: the same outlier left the normal values only &lt;strong&gt;16 distinct levels out of 255&lt;/strong&gt;, and relative error went from 0.89% to 13.78%.&lt;/li&gt;
&lt;li&gt;The fix is to stop sharing a scale with the outlier: &lt;strong&gt;group-wise scales&lt;/strong&gt; (e.g. 128 values per scale), &lt;strong&gt;mixed-precision decomposition&lt;/strong&gt; (LLM.int8), or &lt;strong&gt;moving the outlier's magnitude into the weights&lt;/strong&gt; (SmoothQuant).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is absmax quantization, exactly?
&lt;/h2&gt;

&lt;p&gt;Absmax quantization maps floats to signed integers using one scale per block: divide by &lt;code&gt;max(|x|) / qmax&lt;/code&gt;, round, and clip. For INT8, &lt;code&gt;qmax = 127&lt;/code&gt;. For INT4, &lt;code&gt;qmax = 7&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;absmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;qmax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bits&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;          &lt;span class="c1"&gt;# 127 for int8, 7 for int4&lt;/span&gt;
    &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;qmax&lt;/span&gt;      &lt;span class="c1"&gt;# one number for the whole block&lt;/span&gt;
    &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;qmax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qmax&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;                 &lt;span class="c1"&gt;# ints, and the dequantized floats&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. It's symmetric, cheap, and it's the starting point for nearly every quantization scheme you've used. The weakness is sitting right there on the &lt;code&gt;scale&lt;/code&gt; line. One &lt;code&gt;max()&lt;/code&gt; decides the ruler for everybody.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does one outlier break absmax quantization?
&lt;/h2&gt;

&lt;p&gt;Because the step size is &lt;code&gt;max / qmax&lt;/code&gt;, and any value smaller than half a step rounds to zero. If one value is 15x bigger than the rest, the step grows 15x and the "dead zone" around zero swallows the rest of the block.&lt;/p&gt;

&lt;p&gt;Here's my setup. 4,096 values drawn from a standard normal (seed 0), so the natural max was about 3.9. Then I set index 7 to 60.0 and compared.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setup&lt;/th&gt;
&lt;th&gt;Step size&lt;/th&gt;
&lt;th&gt;Normal values rounded to 0&lt;/th&gt;
&lt;th&gt;Distinct levels used&lt;/th&gt;
&lt;th&gt;Relative error&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;INT8, per-tensor, clean&lt;/td&gt;
&lt;td&gt;0.031&lt;/td&gt;
&lt;td&gt;1.1%&lt;/td&gt;
&lt;td&gt;191&lt;/td&gt;
&lt;td&gt;0.89%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INT8, per-tensor, outlier = 60&lt;/td&gt;
&lt;td&gt;0.472&lt;/td&gt;
&lt;td&gt;17.8%&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;13.78%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INT4, per-tensor, clean&lt;/td&gt;
&lt;td&gt;0.557&lt;/td&gt;
&lt;td&gt;20.8%&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;16.22%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INT4, per-tensor, outlier = 60&lt;/td&gt;
&lt;td&gt;8.571&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;100%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;100%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Relative error is measured on the 4,095 normal values only, so the outlier doesn't flatter the numbers.&lt;/p&gt;

&lt;p&gt;Read the INT4 row slowly. The step is &lt;code&gt;60 / 7 = 8.57&lt;/code&gt;. Half a step is 4.29. Not a single one of my 4,095 Gaussian values was larger than 4.29 in magnitude, so every one of them rounded to 0. The outlier is preserved perfectly. Everything else is gone.&lt;/p&gt;

&lt;p&gt;The INT8 row is sneakier. It &lt;em&gt;looks&lt;/em&gt; fine if you only check that the tensor dequantizes without NaNs. But your 255-level integer grid now spends 239 levels on a range only the outlier lives in. The normal values get squeezed into 16 levels.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does this do to the actual matmul?
&lt;/h2&gt;

&lt;p&gt;It biases the dot product, not just the stored values. Quantized activations feed a matrix multiply, so I took a random weight vector and computed the dot product with the 4,095 normal values before and after quantization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;True contribution from the normal values: &lt;strong&gt;-19.22&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;After INT8 with the outlier: &lt;strong&gt;-8.96&lt;/strong&gt; (more than half the signal lost)&lt;/li&gt;
&lt;li&gt;After INT4 with the outlier: &lt;strong&gt;0.00&lt;/strong&gt; (all of it)&lt;/li&gt;
&lt;li&gt;After INT8 on the clean vector, for reference: -19.82&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the outlier's own term still lands correctly, but the 4,095 "boring" dimensions stop contributing. In a real transformer those boring dimensions are most of the hidden state. The model doesn't crash. It just gets dumber in ways that show up as worse perplexity or weird generations, and you go hunting for a bug in your prompt template.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do LLMs really have outliers like this?
&lt;/h2&gt;

&lt;p&gt;Yes, and that's the well-known part of this story. The LLM.int8() paper (Dettmers et al., 2022) reported that once models get to the multi-billion-parameter range, a small number of hidden-state feature dimensions carry values far larger than the rest, and those dimensions show up consistently across layers and tokens. They're called emergent outlier features, and the paper argues they matter for model quality, so you can't just clip them.&lt;/p&gt;

&lt;p&gt;My planted 60 is a toy. The shape of the problem is the real thing: a handful of huge values in a sea of small ones, sharing one scale.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you fix absmax quantization outliers?
&lt;/h2&gt;

&lt;p&gt;You stop letting the outlier set the scale for values it has nothing to do with. There are three standard ways to do that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix 1: group-wise scales (what GPTQ, AWQ and llama.cpp do)
&lt;/h3&gt;

&lt;p&gt;Split the tensor into small groups and give each group its own absmax scale. The outlier can only ruin its own group.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;absmax_grouped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;absmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concatenate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same vector, same outlier, groups of 128:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setup&lt;/th&gt;
&lt;th&gt;Normal values rounded to 0&lt;/th&gt;
&lt;th&gt;Distinct levels&lt;/th&gt;
&lt;th&gt;Relative error&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;INT8, group 128, outlier&lt;/td&gt;
&lt;td&gt;1.4%&lt;/td&gt;
&lt;td&gt;249&lt;/td&gt;
&lt;td&gt;2.58%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INT4, group 128, outlier&lt;/td&gt;
&lt;td&gt;17.4%&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;20.34%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INT4, group 128, clean&lt;/td&gt;
&lt;td&gt;14.7%&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;11.53%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The damage didn't disappear, it got fenced in. Inside the group that holds the outlier, INT4 still zeroed all 127 neighbors. The other 31 groups were untouched, which is why the totals look close to the clean run.&lt;/p&gt;

&lt;p&gt;This is the reason &lt;code&gt;group_size=128&lt;/code&gt; shows up as a default in GPTQ and AWQ configs, and why llama.cpp's quant formats store a scale per small block. The cost is storage: one extra scale per group. At 128 values per FP16 scale, that's 16 extra bits per 128 weights, about 0.125 bits per weight. For INT4 that's a small price for not deleting entire rows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix 2: pull the outliers out (LLM.int8 mixed precision)
&lt;/h3&gt;

&lt;p&gt;LLM.int8() does matrix decomposition: any activation feature above a threshold gets computed in FP16, and everything else goes through the INT8 path. In bitsandbytes this is the &lt;code&gt;llm_int8_threshold&lt;/code&gt; setting, default 6.0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;transformers&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BitsAndBytesConfig&lt;/span&gt;

&lt;span class="n"&gt;cfg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BitsAndBytesConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;load_in_8bit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llm_int8_threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;6.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I did the crude version of this on my vector: set the outlier aside, quantize the rest to INT8, put the outlier back in full precision. Relative error on the normal values: &lt;strong&gt;0.89%&lt;/strong&gt;, identical to the clean run. Makes sense. Once the outlier leaves the block, the scale goes back to what the 4,095 normal values need.&lt;/p&gt;

&lt;p&gt;The tradeoff is speed. You now run two matmuls and stitch them together, which is why 8-bit bitsandbytes inference is often slower than FP16 on the same GPU. You're trading throughput for memory and accuracy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix 3: move the scale into the weights (SmoothQuant)
&lt;/h3&gt;

&lt;p&gt;SmoothQuant (Xiao et al., 2022) handles activation outliers by dividing each activation channel by a per-channel factor and multiplying the matching weight row by the same factor. The matmul result is mathematically identical, but the activation outliers shrink and the weights, which are easier to quantize, absorb the difference. A migration strength &lt;code&gt;alpha&lt;/code&gt; (0.5 is the common starting point) controls how much difficulty moves from activations to weights.&lt;/p&gt;

&lt;p&gt;Use this when you want both weights and activations in INT8 (W8A8) with a single fast integer matmul, which mixed decomposition can't give you.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I check my own quantized model for this?
&lt;/h2&gt;

&lt;p&gt;Measure the ratio of max to typical magnitude per block before you pick a scheme. If &lt;code&gt;max(|x|) / median(|x|)&lt;/code&gt; is huge for some blocks, per-tensor absmax will crush them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outlier_report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;ratio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dim&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;median&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dim&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clamp_min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1e-8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ratio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ratio&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it on a layer's weights, and on activations captured with a forward hook over a few real prompts. Then check how many dequantized values came back exactly 0 that weren't 0 before. That count is the cheapest smoke test I know, and it would have caught my INT4 row instantly: 4,095 new zeros.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why did one outlier zero 4,095 of 4,096 values in INT4 absmax quantization?
&lt;/h2&gt;

&lt;p&gt;Because absmax quantization uses a single scale per block, &lt;code&gt;max(|x|) / qmax&lt;/code&gt;, and INT4 has only 7 positive levels. An outlier of 60 made the step 8.57, so any value under 4.29 in magnitude rounded to zero, and in a block of standard-normal values that's all of them. The fix is to never let an outlier share a scale with normal values: use group-wise scales (128 values per scale is the common default), split outliers into a higher-precision path like LLM.int8 does, or migrate activation outliers into the weights with SmoothQuant. If you're picking a quantized checkpoint, check its group size before you check its bit width.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>llm</category>
      <category>machinelearning</category>
      <category>performance</category>
      <category>python</category>
    </item>
    <item>
      <title>GitHub Actions GITHUB_TOKEN: 11 Tags Pushed, 0 Release Workflows Ran</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Fri, 20 Nov 2026 08:54:28 +0000</pubDate>
      <link>https://dev.to/ji_ai/github-actions-githubtoken-11-tags-pushed-0-release-workflows-ran-5c2i</link>
      <guid>https://dev.to/ji_ai/github-actions-githubtoken-11-tags-pushed-0-release-workflows-ran-5c2i</guid>
      <description>&lt;p&gt;For two weeks my release pipeline did exactly half of its job. A bump job ran on every merge to &lt;code&gt;main&lt;/code&gt;, wrote a new version, pushed a &lt;code&gt;v*&lt;/code&gt; tag, and finished green. A second workflow, &lt;code&gt;release.yml&lt;/code&gt;, was supposed to wake up on that tag, build the artifacts and publish a GitHub Release.&lt;/p&gt;

&lt;p&gt;Eleven tags went out. Zero releases came back. The Actions tab didn't show a failed run or a skipped run. It showed nothing, as if the tags had never been pushed. The cause is a rule that surprises almost everyone the first time: &lt;strong&gt;GITHUB_TOKEN doesn't trigger workflows&lt;/strong&gt;. Anything a workflow does with that token is invisible to every other workflow's &lt;code&gt;on:&lt;/code&gt; block.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Events caused by the built-in &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; (pushes, tags, opened PRs, published releases, labels) &lt;strong&gt;do not start new workflow runs&lt;/strong&gt;. GitHub does this on purpose to stop infinite loops.&lt;/li&gt;
&lt;li&gt;The only exceptions are &lt;code&gt;workflow_dispatch&lt;/code&gt; and &lt;code&gt;repository_dispatch&lt;/code&gt;. Those still fire when sent with &lt;code&gt;GITHUB_TOKEN&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;No error is logged anywhere. The downstream run simply never exists.&lt;/li&gt;
&lt;li&gt;Best fix with zero secrets: call the downstream workflow directly with &lt;code&gt;workflow_call&lt;/code&gt;, or dispatch it with &lt;code&gt;gh workflow run&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If you truly need the push itself to trigger things (for example, CI on a bot-opened PR), push with a GitHub App installation token instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why doesn't a push made with GITHUB_TOKEN trigger my workflow?
&lt;/h2&gt;

&lt;p&gt;Because GitHub deliberately drops workflow triggers for events created by &lt;code&gt;GITHUB_TOKEN&lt;/code&gt;, except &lt;code&gt;workflow_dispatch&lt;/code&gt; and &lt;code&gt;repository_dispatch&lt;/code&gt;. The reasoning is simple: a workflow that runs on &lt;code&gt;push&lt;/code&gt; and also pushes a commit would retrigger itself forever. Rather than asking every user to write loop guards, GitHub cut the link at the token level.&lt;/p&gt;

&lt;p&gt;The token still works. Your push lands, your tag exists, your PR opens. The event is recorded in the repository. It's just never delivered to the workflow scheduler.&lt;/p&gt;

&lt;p&gt;That's what makes it nasty. Nothing fails. Your logs say &lt;code&gt;* [new tag] v1.4.0 -&amp;gt; v1.4.0&lt;/code&gt; and the job ends with a green check.&lt;/p&gt;

&lt;h2&gt;
  
  
  What did my broken setup look like?
&lt;/h2&gt;

&lt;p&gt;Here's the bump workflow, trimmed down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# .github/workflows/bump.yml
&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bump&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;main&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;bump&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;fetch-depth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Tag next version&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;NEXT=$(./scripts/next-version.sh)&lt;/span&gt;
          &lt;span class="s"&gt;git tag "v$NEXT"&lt;/span&gt;
          &lt;span class="s"&gt;git push origin "v$NEXT"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the release workflow that never ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# .github/workflows/release.yml
&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;release&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'v*'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The trap is hidden in &lt;code&gt;actions/checkout&lt;/code&gt;. By default it uses &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; and leaves it in the local git config (&lt;code&gt;persist-credentials: true&lt;/code&gt;). So when the script later runs a plain &lt;code&gt;git push&lt;/code&gt;, it pushes as &lt;code&gt;github-actions[bot]&lt;/code&gt; with &lt;code&gt;GITHUB_TOKEN&lt;/code&gt;. You never typed the token name, but you used it.&lt;/p&gt;

&lt;p&gt;If I had pushed that same tag from my laptop, &lt;code&gt;release.yml&lt;/code&gt; would have fired instantly. That's the quickest way to confirm you're hitting this rule: push the same thing by hand and watch it work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which GitHub Actions events still fire from GITHUB_TOKEN?
&lt;/h2&gt;

&lt;p&gt;Only two do directly. Everything else that the token creates is silent.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action taken with &lt;code&gt;GITHUB_TOKEN&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;Starts a new workflow run?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;git push&lt;/code&gt; to a branch&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;git push&lt;/code&gt; of a tag&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Opening or updating a pull request&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Publishing a release (&lt;code&gt;release: published&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Adding a label, opening an issue&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;workflow_dispatch&lt;/code&gt; (e.g. &lt;code&gt;gh workflow run&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;repository_dispatch&lt;/code&gt; (API call)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There's also &lt;code&gt;workflow_run&lt;/code&gt;, which fires when another workflow &lt;em&gt;completes&lt;/em&gt;. It keys off the finished run of &lt;code&gt;bump.yml&lt;/code&gt; (which a human push started), not off the tag, so it does fire. It's a usable workaround, but it gives you awkward context: the downstream run sees the upstream commit, not your tag, and you have to dig the version back out.&lt;/p&gt;

&lt;p&gt;The pull request row is the one that bites the most people. Tools that open "chore: bump dependencies" PRs from inside a workflow with &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; produce PRs where required checks sit on "Waiting for status to be reported" forever. CI was never asked to run.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I fix it without adding any secrets?
&lt;/h2&gt;

&lt;p&gt;Call the release workflow yourself instead of hoping the tag wakes it up. There are two clean ways, and neither needs a personal token.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix 1: make release a reusable workflow (&lt;code&gt;workflow_call&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;This is what I shipped. The release workflow accepts a tag as input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# .github/workflows/release.yml
&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;release&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;workflow_call&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;string&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'v*'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;   &lt;span class="c1"&gt;# keep this so manual tag pushes still work
&lt;/span&gt;
&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;release&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ inputs.tag || github.ref }}&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./scripts/build-and-publish.sh "${{ inputs.tag || github.ref_name }}"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the bump workflow calls it as a second job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;bump&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;outputs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ steps.tag.outputs.tag }}&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;fetch-depth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;tag&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;NEXT="v$(./scripts/next-version.sh)"&lt;/span&gt;
          &lt;span class="s"&gt;git tag "$NEXT" &amp;amp;&amp;amp; git push origin "$NEXT"&lt;/span&gt;
          &lt;span class="s"&gt;echo "tag=$NEXT" &amp;gt;&amp;gt; "$GITHUB_OUTPUT"&lt;/span&gt;

  &lt;span class="na"&gt;release&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bump&lt;/span&gt;
    &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./.github/workflows/release.yml&lt;/span&gt;
    &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ needs.bump.outputs.tag }}&lt;/span&gt;
    &lt;span class="na"&gt;secrets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;inherit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter here. First, a called workflow runs with the &lt;strong&gt;caller's&lt;/strong&gt; &lt;code&gt;github&lt;/code&gt; context, so &lt;code&gt;github.ref&lt;/code&gt; is still &lt;code&gt;refs/heads/main&lt;/code&gt;, not your tag. That's why I pass the tag in explicitly. Second, the called workflow can't have more &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; permissions than the caller grants, so if the release step uploads assets, the caller needs &lt;code&gt;contents: write&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix 2: dispatch it with &lt;code&gt;gh workflow run&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If you'd rather keep the workflows separate, &lt;code&gt;workflow_dispatch&lt;/code&gt; is one of the two exempt events. Add it to the release workflow's triggers, then from the bump job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;
  &lt;span class="na"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;   &lt;span class="c1"&gt;# required to dispatch workflows
&lt;/span&gt;
&lt;span class="c1"&gt;# ...after pushing the tag:
&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;gh workflow run release.yml --ref "$NEXT"&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;GH_TOKEN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ github.token }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dispatching with &lt;code&gt;--ref&lt;/code&gt; set to the tag means the release run checks out the tag and sees &lt;code&gt;github.ref&lt;/code&gt; as &lt;code&gt;refs/tags/v1.4.0&lt;/code&gt;, exactly like a real tag push would. It shows up in the Actions tab as a separate run, which I find easier to read than nested jobs.&lt;/p&gt;

&lt;h2&gt;
  
  
  When do I actually need a different token?
&lt;/h2&gt;

&lt;p&gt;When the event itself has to trigger things you don't control, like required CI checks on a bot-opened pull request. You can't &lt;code&gt;workflow_call&lt;/code&gt; your way into a PR's status checks. The push has to come from an identity that isn't &lt;code&gt;GITHUB_TOKEN&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix 3: a GitHub App installation token (recommended)
&lt;/h3&gt;

&lt;p&gt;Create a small GitHub App with contents and pull request permissions, install it on the repo, store its ID and private key as secrets, then mint a short-lived token per run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/create-github-app-token@v1&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;app-id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.RELEASE_APP_ID }}&lt;/span&gt;
    &lt;span class="na"&gt;private-key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.RELEASE_APP_KEY }}&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ steps.app.outputs.token }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because checkout persists whatever token you give it, every later &lt;code&gt;git push&lt;/code&gt; in that job now uses the App token, and the resulting events trigger workflows normally. The token expires after an hour and isn't tied to any person's account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix 4: a personal access token (fastest, worst)
&lt;/h3&gt;

&lt;p&gt;Same pattern, with &lt;code&gt;token: ${{ secrets.RELEASE_PAT }}&lt;/code&gt;. It works in two minutes. It also means your release pipeline is now logged in as you, breaks when the token expires, and gets whatever access your account has unless you scope a fine-grained token carefully. I use this only for throwaway repos.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I avoid infinite loops after switching tokens?
&lt;/h2&gt;

&lt;p&gt;You just removed the safety net GitHub gave you, so add your own. If a workflow on &lt;code&gt;push&lt;/code&gt; to &lt;code&gt;main&lt;/code&gt; also pushes to &lt;code&gt;main&lt;/code&gt; with an App token, it will retrigger itself.&lt;/p&gt;

&lt;p&gt;Three guards that work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put &lt;code&gt;[skip ci]&lt;/code&gt; in bot commit messages. GitHub skips &lt;code&gt;push&lt;/code&gt; and &lt;code&gt;pull_request&lt;/code&gt; runs for commits that contain it.&lt;/li&gt;
&lt;li&gt;Filter on the actor: &lt;code&gt;if: github.actor != 'my-release-app[bot]'&lt;/code&gt; at the job level.&lt;/li&gt;
&lt;li&gt;Push tags only, and trigger the loop-prone workflow on branches only. A tag push doesn't match &lt;code&gt;branches: [main]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pick at least one before you merge. A self-triggering release loop burns minutes fast and spams your Releases page with versions you'll have to delete.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can I tell this is my problem in under a minute?
&lt;/h2&gt;

&lt;p&gt;Check three things. Is there no run at all in the Actions tab for the expected trigger (not skipped, not failed, just absent)? Was the commit, tag or PR created by &lt;code&gt;github-actions[bot]&lt;/code&gt;? Does the same action performed by hand trigger the workflow? Three yeses means you've hit the &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; rule and nothing in your YAML filters is wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why did 11 tags produce 0 release runs?
&lt;/h2&gt;

&lt;p&gt;Because the tags were pushed with &lt;code&gt;GITHUB_TOKEN&lt;/code&gt;, and in GitHub Actions any event created by &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; (pushes, tags, pull requests, releases) never starts a new workflow run, with only &lt;code&gt;workflow_dispatch&lt;/code&gt; and &lt;code&gt;repository_dispatch&lt;/code&gt; exempt. GitHub does this to prevent recursive runs, and it logs nothing when it drops the trigger. The fix is to stop relying on the event: call the downstream workflow with &lt;code&gt;workflow_call&lt;/code&gt; or &lt;code&gt;gh workflow run&lt;/code&gt;, which needs no extra secrets. If the event itself must trigger other workflows, such as CI on a bot-opened PR, push with a GitHub App installation token and add your own loop guard.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>tooling</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Celery visibility_timeout: 214 LLM Jobs Ran Twice on Redis</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Thu, 19 Nov 2026 20:52:49 +0000</pubDate>
      <link>https://dev.to/ji_ai/celery-visibilitytimeout-214-llm-jobs-ran-twice-on-redis-4f53</link>
      <guid>https://dev.to/ji_ai/celery-visibilitytimeout-214-llm-jobs-ran-twice-on-redis-4f53</guid>
      <description>&lt;p&gt;The bug showed up as a spreadsheet problem. My nightly pipeline summarizes long PDFs with Claude and writes one row per document. One morning, document 4471 had two summaries. Slightly different wording, start times 61 minutes apart, same Celery task ID.&lt;/p&gt;

&lt;p&gt;Same task ID is the part that made my stomach drop. That isn't a retry. That's the broker handing the exact same message to a second worker while the first one was still chewing on it. The cause was Celery visibility_timeout on the Redis broker, and over 14 nights it quietly ran 214 of my 1,380 jobs at least twice.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;On the Redis (and SQS) broker, Celery's &lt;code&gt;visibility_timeout&lt;/code&gt; defaults to &lt;strong&gt;3600 seconds&lt;/strong&gt;. Any message a worker has taken but not acknowledged within that hour gets pushed back onto the queue and delivered again.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;acks_late=True&lt;/code&gt;, a task isn't acknowledged until it finishes. Any task that runs longer than an hour gets &lt;strong&gt;redelivered while it's still running&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Prefetch makes it worse: messages sitting in a worker's prefetch buffer are also unacknowledged. Short tasks queued behind long ones can hit the hour without ever starting.&lt;/li&gt;
&lt;li&gt;Fix: raise &lt;code&gt;visibility_timeout&lt;/code&gt; above your longest task plus queue wait, set &lt;code&gt;worker_prefetch_multiplier = 1&lt;/code&gt;, and add an idempotency guard per job.&lt;/li&gt;
&lt;li&gt;Trade-off: a higher timeout means a task lost in a real worker crash waits that long before it's retried.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What does Celery visibility_timeout actually do?
&lt;/h2&gt;

&lt;p&gt;Celery visibility_timeout is how long the Redis transport waits for an acknowledgement before assuming the worker died and putting the message back on the queue. Redis has no native concept of "this consumer has this message," so Kombu fakes it.&lt;/p&gt;

&lt;p&gt;When a worker reserves a message, Kombu copies it into an &lt;code&gt;unacked&lt;/code&gt; hash with a timestamp. When the task is acknowledged, the entry is removed. Kombu periodically scans that hash, and anything older than &lt;code&gt;visibility_timeout&lt;/code&gt; gets restored to the queue as if it had never been delivered.&lt;/p&gt;

&lt;p&gt;That's a reasonable design for crash recovery. It just has no idea the difference between "the worker died" and "the worker is on minute 73 of a big PDF."&lt;/p&gt;

&lt;h2&gt;
  
  
  My setup
&lt;/h2&gt;

&lt;p&gt;Nothing exotic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2 worker boxes, &lt;code&gt;--concurrency=4&lt;/code&gt; each, prefork pool&lt;/li&gt;
&lt;li&gt;Redis as broker, Postgres for results and job state&lt;/li&gt;
&lt;li&gt;Each job: split a PDF into chunks, summarize each chunk with Claude, then merge into a final summary&lt;/li&gt;
&lt;li&gt;Runtime: most jobs 4 to 15 minutes, a long tail of 60 to 110 minutes for big documents&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;acks_late=True&lt;/code&gt; on the task, because I wanted a crashed job to come back&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last line is the one I'd written with pride. "If a worker dies mid-job, the job gets retried. Very robust." It was robust in exactly the way that bills you twice.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I found 214 duplicate runs
&lt;/h2&gt;

&lt;p&gt;Every job writes a &lt;code&gt;job_runs&lt;/code&gt; row when it starts, keyed by Celery's &lt;code&gt;task_id&lt;/code&gt;. A real retry gets a new row with the same task ID too, so I couldn't just count rows. I needed runs that &lt;strong&gt;overlapped in time&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS runs
FROM job_runs a
JOIN job_runs b
  ON a.task_id = b.task_id
 AND a.id &amp;lt;&amp;gt; b.id
 AND a.started_at &amp;lt; coalesce(b.finished_at, now())
 AND b.started_at &amp;lt; coalesce(a.finished_at, now())
WHERE a.started_at &amp;gt; now() - interval '14 days'
GROUP BY a.task_id;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Results for the 14 nights:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Count&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Jobs dispatched&lt;/td&gt;
&lt;td&gt;1,380&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jobs that ran at least twice&lt;/td&gt;
&lt;td&gt;214&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Of those, ran three times&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Duplicates from long runtime (&lt;code&gt;acks_late&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;171&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Duplicates from waiting in prefetch&lt;/td&gt;
&lt;td&gt;43&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The second run almost always started 60 to 62 minutes after the first. That gap is the fingerprint. If you see duplicates clustered at exactly your timeout plus a little scan lag, stop looking for a bug in your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why did tasks longer than one hour run twice?
&lt;/h2&gt;

&lt;p&gt;Tasks longer than one hour ran twice because &lt;code&gt;acks_late=True&lt;/code&gt; delays the acknowledgement until the task returns, so the message stays in Redis's &lt;code&gt;unacked&lt;/code&gt; hash for the whole runtime. At the 3600-second mark, Kombu restores it and another worker picks it up.&lt;/p&gt;

&lt;p&gt;Here's the part that scared me more than the duplicates. When the first run finally finishes and acks, it doesn't cancel the restored copy. That copy is a fresh delivery with its own timer. If the second run &lt;em&gt;also&lt;/em&gt; takes more than an hour, it gets restored again. In principle a 70-minute job loops forever.&lt;/p&gt;

&lt;p&gt;Mine didn't, and not because of anything clever. My pipeline caches chunk summaries in Postgres. The second run found the chunks the first run had already finished and skipped them, so it was usually faster. The 9 triple runs were the biggest documents, where even the cached rerun crossed the hour.&lt;/p&gt;

&lt;p&gt;So the only thing standing between me and an infinite loop of paid API calls was a cache I'd added for a completely unrelated reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why did short tasks get duplicated too?
&lt;/h2&gt;

&lt;p&gt;Short tasks got duplicated because prefetched messages count as unacknowledged, even when they haven't started. With the default &lt;code&gt;worker_prefetch_multiplier = 4&lt;/code&gt; and concurrency 4, each worker box reserves up to 16 messages at once.&lt;/p&gt;

&lt;p&gt;Picture a box where all 4 processes grab big documents that take 90 minutes. The 12 messages sitting behind them in the prefetch buffer are reserved, not started, and their timer is running. At 60 minutes they get restored, the other box runs them, and 30 minutes later the original box runs them too.&lt;/p&gt;

&lt;p&gt;These 43 were the expensive ones. A long job's duplicate partly hit the chunk cache. A prefetched short job's duplicate ran from scratch in parallel on another machine, full price, no cache hits.&lt;/p&gt;

&lt;h2&gt;
  
  
  What did the duplicates cost?
&lt;/h2&gt;

&lt;p&gt;Across the 14 nights, the duplicate runs consumed about 23 million input tokens on the Claude API. That was roughly 11% of the pipeline's total input tokens for the period. Not a catastrophe, but it was 11% of the bill buying literally nothing, plus two summaries per document that my downstream code had to dedupe.&lt;/p&gt;

&lt;p&gt;The less visible cost: every duplicate occupied a worker slot, which pushed more jobs into the prefetch buffer, which created more prefetch duplicates. The bug fed itself on busy nights. My worst night had 31 duplicates; my quietest had 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you fix Celery visibility_timeout on Redis?
&lt;/h2&gt;

&lt;p&gt;You fix it by setting &lt;code&gt;visibility_timeout&lt;/code&gt; longer than your longest possible task plus its worst queue wait, cutting prefetch to 1 for long tasks, and making the task itself idempotent. Here's the config before and after:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# before&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task_acks_late&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;span class="c1"&gt;# visibility_timeout: default 3600&lt;/span&gt;
&lt;span class="c1"&gt;# worker_prefetch_multiplier: default 4&lt;/span&gt;

&lt;span class="c1"&gt;# after&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task_acks_late&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;worker_prefetch_multiplier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;broker_transport_options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"visibility_timeout"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# 12h, longest job is ~2h&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put this in the shared config module that every process imports: workers, beat, and the web app that calls &lt;code&gt;.delay()&lt;/code&gt;. The restore scan can run from any client connected to the transport, and you don't want one process quietly using the 1-hour default.&lt;/p&gt;

&lt;p&gt;Then the seatbelt, because config drifts and someday someone will add a 14-hour job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;acks_late&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;summarize_doc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;job_is_done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="c1"&gt;# a duplicate delivery after success&lt;/span&gt;

    &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"summarize:{doc_id}:lock"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;redis_client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nx&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"duplicate delivery doc=%s task=%s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;run_pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mark_done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;redis_client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;done&lt;/code&gt; check catches redeliveries after a successful run. The &lt;code&gt;SET NX&lt;/code&gt; lock catches concurrent ones. The lock TTL is your crash escape hatch: if a worker dies holding it, the job is blocked for at most 6 hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Did the fix work?
&lt;/h2&gt;

&lt;p&gt;Yes. Over the next 14 nights, 1,302 jobs ran with 0 overlapping duplicates, and the idempotency guard logged 0 skips. Zero skips is what I want to see: the guard isn't doing the work, the config is. The guard is there for the day I'm wrong about the longest job.&lt;/p&gt;

&lt;p&gt;I also tested the failure path on staging with &lt;code&gt;visibility_timeout&lt;/code&gt; temporarily set to 5 minutes and a &lt;code&gt;kill -9&lt;/code&gt; on a worker mid-job. The job came back after about 5 minutes and ran once. In production that same scenario now means a 12-hour wait, which I'm fine with for a nightly batch. For a user-facing queue, I'd split long jobs into a separate queue with its own timeout instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell myself before turning on acks_late
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;acks_late&lt;/code&gt; and &lt;code&gt;visibility_timeout&lt;/code&gt; are one setting.&lt;/strong&gt; Never touch one without the other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your timeout budget is runtime plus queue wait&lt;/strong&gt;, not just runtime. Prefetch is queue wait you can't see in Flower.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Same task ID, overlapping runs, ~60 minutes apart&lt;/strong&gt; is a diagnosis, not a mystery.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LLM tasks are the worst case.&lt;/strong&gt; They're long, they're variable, and every duplicate is real money.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idempotency isn't optional&lt;/strong&gt; on any queue that promises at-least-once delivery. Redis-backed Celery promises exactly that.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  So why did my Celery jobs run twice on Redis?
&lt;/h2&gt;

&lt;p&gt;My Celery jobs ran twice because the Redis broker's &lt;code&gt;visibility_timeout&lt;/code&gt; defaults to one hour, and any message not acknowledged within that hour is put back on the queue for another worker. With &lt;code&gt;acks_late=True&lt;/code&gt;, every job longer than 60 minutes crossed that line while still running, and with the default prefetch of 4, short jobs waiting behind long ones crossed it without ever starting. That added up to 214 duplicate runs out of 1,380 and about 11% wasted input tokens. Raising &lt;code&gt;visibility_timeout&lt;/code&gt; to 12 hours, setting &lt;code&gt;worker_prefetch_multiplier = 1&lt;/code&gt;, and adding a per-job idempotency lock brought duplicates to zero over the next 1,302 jobs.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>automation</category>
      <category>devops</category>
    </item>
    <item>
      <title>pgvector HNSW Filtering: 431 of 2,000 LIMIT 10 Queries Came Back Short</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Thu, 19 Nov 2026 08:51:05 +0000</pubDate>
      <link>https://dev.to/ji_ai/pgvector-hnsw-filtering-431-of-2000-limit-10-queries-came-back-short-33fd</link>
      <guid>https://dev.to/ji_ai/pgvector-hnsw-filtering-431-of-2000-limit-10-queries-came-back-short-33fd</guid>
      <description>&lt;p&gt;I wrote &lt;code&gt;LIMIT 10&lt;/code&gt;. Postgres returned 3 rows. There was no error, no warning and no log line, and for five weeks I didn't notice, because my own fallback code covered for it.&lt;/p&gt;

&lt;p&gt;This post is about &lt;strong&gt;pgvector HNSW filtering&lt;/strong&gt;: what happens when you put a &lt;code&gt;WHERE&lt;/code&gt; clause on an approximate nearest neighbor query. Short version: the index finds its neighbors first and your filter runs afterward. If most of those neighbors fail the filter, you get fewer rows than you asked for, and nothing tells you.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;pgvector's HNSW index collects &lt;code&gt;hnsw.ef_search&lt;/code&gt; candidates (default &lt;strong&gt;40&lt;/strong&gt;), and only then does Postgres apply your &lt;code&gt;WHERE&lt;/code&gt; filter. When the filter rejects most of them, the query returns fewer than &lt;code&gt;LIMIT&lt;/code&gt; rows without any error.&lt;/li&gt;
&lt;li&gt;In a replay of 2,000 production queries, &lt;strong&gt;431 (21.6%)&lt;/strong&gt; returned fewer than 10 rows and &lt;strong&gt;97&lt;/strong&gt; returned zero.&lt;/li&gt;
&lt;li&gt;Raising &lt;code&gt;hnsw.ef_search&lt;/code&gt; to 200 cut short results to 63. pgvector 0.8's &lt;code&gt;hnsw.iterative_scan = relaxed_order&lt;/code&gt; cut them to 9, and those 9 were correct answers (the category didn't have 10 rows in total).&lt;/li&gt;
&lt;li&gt;The worst-hit categories were mid-sized ones that sit close in embedding space to a bigger category. The tiniest categories mostly escaped because the planner skipped HNSW for them.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;len(rows) &amp;lt; limit&lt;/code&gt; in application code. The database will never flag it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What was I building?
&lt;/h2&gt;

&lt;p&gt;The system is the question retriever inside Preterview, a voice mock-interview platform I run that has three interviewer styles and writes a report after each session (full disclosure: I built it, and it's at &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;preterview.com/en&lt;/a&gt;). Before each interviewer turn, an agent pulls 10 candidate follow-up questions for the candidate's target role, and an LLM picks one based on the conversation so far.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftd5zed80xfjpv7qjalsn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftd5zed80xfjpv7qjalsn.jpg" alt="Preterview — an interview session in progress"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The table looked like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;52,000 questions&lt;/li&gt;
&lt;li&gt;140 role tags (&lt;code&gt;backend_engineer&lt;/code&gt;, &lt;code&gt;data_engineer&lt;/code&gt;, &lt;code&gt;sre&lt;/code&gt;, &lt;code&gt;ios_engineer&lt;/code&gt;, ...)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;embedding vector(1024)&lt;/code&gt; with an HNSW index on default build settings (&lt;code&gt;m = 16&lt;/code&gt;, &lt;code&gt;ef_construction = 64&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;a plain btree index on &lt;code&gt;role&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The query was the one every tutorial shows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;interview_questions&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's the part I'm least proud of. If the retriever got fewer than 10 rows back, it padded the list from a pool of 30 general questions. I'd written that for brand-new roles with tiny question banks. In practice it hid the bug: users preparing for data engineering roles got the same three general questions about "a project you're proud of" session after session. The complaint I eventually got was "the interviewer feels repetitive." It didn't look like a retrieval bug at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does pgvector HNSW return fewer rows than LIMIT?
&lt;/h2&gt;

&lt;p&gt;pgvector's HNSW index scan returns at most &lt;code&gt;hnsw.ef_search&lt;/code&gt; nearest candidates (40 by default), and Postgres applies the &lt;code&gt;WHERE&lt;/code&gt; clause to those candidates after the scan. If 37 of the 40 nearest questions belong to another role, you get 3 rows, even though thousands of matching rows exist in the table.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; shows exactly this once you know where to look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Limit  (actual rows=3 loops=1)
  -&amp;gt;  Index Scan using questions_embedding_idx on interview_questions
        Order By: (embedding &amp;lt;=&amp;gt; '[...]'::vector)
        Filter: (role = 'data_engineer'::text)
        Rows Removed by Filter: 37
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;40 candidates, 37 removed, 3 left. Postgres didn't fail at anything. It did exactly what the index promised: an approximate top 40, then a filter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which filtered vector queries get hit hardest?
&lt;/h2&gt;

&lt;p&gt;Mid-sized categories that are semantically close to a large category get hit hardest. The trouble isn't that a role is rare. It's that its nearest neighbors in embedding space mostly belong to some other role.&lt;/p&gt;

&lt;p&gt;I replayed 2,000 real retrieval calls from logs against a copy of the table and bucketed them:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role bucket&lt;/th&gt;
&lt;th&gt;Queries&lt;/th&gt;
&lt;th&gt;Came back short&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Large roles (more than 3% of rows each)&lt;/td&gt;
&lt;td&gt;1,020&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mid roles, close to a large role&lt;/td&gt;
&lt;td&gt;610&lt;/td&gt;
&lt;td&gt;379&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mid roles, semantically distinct&lt;/td&gt;
&lt;td&gt;240&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tiny roles (under 0.2% of rows)&lt;/td&gt;
&lt;td&gt;130&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2,000&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;431&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;data_engineer&lt;/code&gt; was the poster child. Its questions about pipelines, partitioning and backfills sit right next to &lt;code&gt;backend_engineer&lt;/code&gt; questions about queues, partitioning and retries, and backend had roughly six times as many rows. So the 40 nearest neighbors of a data engineering query were mostly backend questions. &lt;code&gt;sre&lt;/code&gt; lost to &lt;code&gt;devops&lt;/code&gt; the same way.&lt;/p&gt;

&lt;p&gt;Meanwhile &lt;code&gt;embedded_firmware&lt;/code&gt; was fine at the same size, because nothing else in the table talks about interrupt handlers.&lt;/p&gt;

&lt;p&gt;The tiny roles surprised me. They mostly worked, because for a highly selective filter the planner used the btree on &lt;code&gt;role&lt;/code&gt;, fetched every matching row and sorted them exactly by distance. No HNSW, no candidate cap. All 9 short results in that bucket came from roles with fewer than 10 questions in total, which is the right answer.&lt;/p&gt;

&lt;p&gt;So the failure curve is U-shaped. Big roles win the neighbor vote. Tiny roles skip the index. The ones in the middle fall through the gap.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does raising hnsw.ef_search fix it?
&lt;/h2&gt;

&lt;p&gt;Raising &lt;code&gt;hnsw.ef_search&lt;/code&gt; reduces short results but doesn't remove them, and it makes every query slower, including the ones that were already fine. Here's the same 2,000-query replay on my box:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Short&lt;/th&gt;
&lt;th&gt;Zero rows&lt;/th&gt;
&lt;th&gt;p50&lt;/th&gt;
&lt;th&gt;p99&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ef_search = 40&lt;/code&gt; (default)&lt;/td&gt;
&lt;td&gt;431&lt;/td&gt;
&lt;td&gt;97&lt;/td&gt;
&lt;td&gt;4.1 ms&lt;/td&gt;
&lt;td&gt;9 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ef_search = 100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;168&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;5.0 ms&lt;/td&gt;
&lt;td&gt;13 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ef_search = 200&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;63&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;6.8 ms&lt;/td&gt;
&lt;td&gt;19 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ef_search = 1000&lt;/code&gt; (max)&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;19.5 ms&lt;/td&gt;
&lt;td&gt;41 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;iterative_scan = relaxed_order&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;5.2 ms&lt;/td&gt;
&lt;td&gt;31 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Even at the maximum of 1000, 5 queries still came back short of what the table could provide. A bigger fixed candidate pool is still a fixed candidate pool.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does pgvector iterative index scan work?
&lt;/h2&gt;

&lt;p&gt;pgvector 0.8.0 added iterative index scans: when the filter leaves too few rows, the scan keeps walking the graph and fetching more candidates until it fills the &lt;code&gt;LIMIT&lt;/code&gt; or hits &lt;code&gt;hnsw.max_scan_tuples&lt;/code&gt; (20,000 by default). You turn it on per session or per transaction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;strict_order&lt;/code&gt; keeps results exactly ordered by distance.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;relaxed_order&lt;/code&gt; allows slight reordering and is faster, so you re-sort at the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what I shipped:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;LOCAL&lt;/span&gt; &lt;span class="n"&gt;hnsw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iterative_scan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;relaxed_order&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;LOCAL&lt;/span&gt; &lt;span class="n"&gt;hnsw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ef_search&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;distance&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;interview_questions&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;distance&lt;/span&gt;
  &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter here. &lt;code&gt;SET LOCAL&lt;/code&gt; scopes the setting to the transaction, so it doesn't leak into other queries on a pooled connection. And the &lt;code&gt;MATERIALIZED&lt;/code&gt; CTE plus the outer &lt;code&gt;ORDER BY&lt;/code&gt; fixes the small reordering that &lt;code&gt;relaxed_order&lt;/code&gt; allows.&lt;/p&gt;

&lt;p&gt;The p99 jump from 9 ms to 31 ms comes from the worst queries, the data engineering ones that have to walk deep into backend territory before they find 10 matches. Median latency barely moved. For a voice interview where an LLM call comes right after this query, 31 ms at p99 doesn't register.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is checking the row count still required?
&lt;/h2&gt;

&lt;p&gt;Iterative scans stop at &lt;code&gt;hnsw.max_scan_tuples&lt;/code&gt;, so a very unlucky filter can still come back short, and the database still won't raise an error. The only reliable signal is in your code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;QUERY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;LIMIT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;metrics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"retrieval.short"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"short_retrieval"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;want&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;LIMIT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I kept the padding fallback, but now it fires a metric. If it fires for any role with more than 10 questions in the bank, that's a bug, not a cold start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should you use partial indexes instead?
&lt;/h2&gt;

&lt;p&gt;A partial HNSW index per category (&lt;code&gt;CREATE INDEX ... WHERE role = 'data_engineer'&lt;/code&gt;) makes filtered search exact for that category, and it's the right fix if you filter on a handful of values. With 140 roles that's 140 indexes to build and maintain, so I only considered it for the two worst roles and found iterative scan was good enough. If your filter has 3 to 5 values, partitioning by that column is also a clean answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What did this bug actually cost?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;5 weeks&lt;/strong&gt; live before I found it, all hidden behind a fallback I wrote myself&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;one afternoon&lt;/strong&gt; to build the replay harness and get the numbers above&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;4 lines of SQL&lt;/strong&gt; to fix it, plus 4 lines of Python to make sure I see it next time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The expensive part wasn't the fix. It was a symptom that looked like bad LLM output when the cause was the database quietly returning less than I asked for.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why does pgvector HNSW filtering return fewer rows than LIMIT?
&lt;/h2&gt;

&lt;p&gt;Because pgvector's HNSW index scan fetches only &lt;code&gt;hnsw.ef_search&lt;/code&gt; candidates (40 by default) and Postgres applies your &lt;code&gt;WHERE&lt;/code&gt; filter afterward, so a selective filter can leave you with 3 rows, or none, out of a &lt;code&gt;LIMIT 10&lt;/code&gt;, with no error. It hurts most when the filtered category sits close in embedding space to a larger one. Raising &lt;code&gt;ef_search&lt;/code&gt; helps partially at a latency cost. On pgvector 0.8 or later, set &lt;code&gt;hnsw.iterative_scan = relaxed_order&lt;/code&gt; inside the transaction, re-sort in a materialized CTE, and always check the returned row count in code, because the database won't do it for you.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>performance</category>
      <category>python</category>
    </item>
    <item>
      <title>Reciprocal Rank Fusion k=60: My #1 BM25 Match Fell to Position 31</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Wed, 18 Nov 2026 20:49:01 +0000</pubDate>
      <link>https://dev.to/ji_ai/reciprocal-rank-fusion-k60-my-1-bm25-match-fell-to-position-31-54n5</link>
      <guid>https://dev.to/ji_ai/reciprocal-rank-fusion-k60-my-1-bm25-match-fell-to-position-31-54n5</guid>
      <description>&lt;p&gt;I typed &lt;code&gt;ERR_CONN_4012&lt;/code&gt; into my hybrid search and got 30 pages about connection errors in general. The one runbook that actually contained the string &lt;code&gt;ERR_CONN_4012&lt;/code&gt; sat at position 31.&lt;/p&gt;

&lt;p&gt;BM25 had ranked it #1. Of course it did: exact token, rare token, short doc. The vector index didn't have it in its top 50 at all. Then Reciprocal Rank Fusion merged the two lists and quietly buried the only correct answer below every vaguely related page.&lt;/p&gt;

&lt;p&gt;Nothing was broken. Reciprocal Rank Fusion with &lt;code&gt;k=60&lt;/code&gt; did exactly what the formula says. This post is about that formula, why the textbook default punishes "one retriever is very sure" results, and what I changed.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reciprocal Rank Fusion (RRF)&lt;/strong&gt; scores each doc as &lt;code&gt;sum(1 / (k + rank))&lt;/code&gt; across retrievers. With the common default &lt;code&gt;k=60&lt;/code&gt;, rank #1 is worth &lt;code&gt;1/61 = 0.0164&lt;/code&gt; and rank #50 is worth &lt;code&gt;1/110 = 0.0091&lt;/code&gt;. The curve is almost flat.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Any doc ranked 61st or better in both lists beats a doc ranked #1 in only one list.&lt;/strong&gt; In general, the cutoff is rank &lt;code&gt;k + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Exact-match queries (error codes, SKUs, function names) are exactly the case where BM25 is right and embeddings miss, so RRF demotes them by design.&lt;/li&gt;
&lt;li&gt;If a reranker only sees the fused top 20, the buried doc never even gets a second chance.&lt;/li&gt;
&lt;li&gt;Fixes: detect identifier queries and pin exact matches, retrieve deeper on the vector side, lower &lt;code&gt;k&lt;/code&gt; or weight BM25, and widen the rerank window.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How does Reciprocal Rank Fusion actually score documents?
&lt;/h2&gt;

&lt;p&gt;Reciprocal Rank Fusion ignores raw scores and uses only rank positions. Each retriever contributes &lt;code&gt;1 / (k + rank)&lt;/code&gt; for every doc it returned, and a doc missing from a list contributes zero.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rrf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rankings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ranking&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rankings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc_id&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ranking&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;doc_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the whole algorithm. It's popular for good reasons: BM25 scores and cosine similarities live on completely different scales, and RRF sidesteps calibration entirely. No normalization, no tuning, works out of the box. Elasticsearch, OpenSearch, Weaviate, Qdrant and most RAG tutorials reach for it, and &lt;code&gt;k=60&lt;/code&gt; is the number almost everyone copies from the original 2009 paper.&lt;/p&gt;

&lt;p&gt;The trouble is what &lt;code&gt;k=60&lt;/code&gt; does to the shape of the curve.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does RRF with k=60 bury exact keyword matches?
&lt;/h2&gt;

&lt;p&gt;RRF with &lt;code&gt;k=60&lt;/code&gt; makes appearing in two lists worth far more than ranking first in one. Here are the actual numbers:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;RRF score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;#1 in BM25, absent from vector list&lt;/td&gt;
&lt;td&gt;1/61 = &lt;strong&gt;0.01639&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#50 in both lists&lt;/td&gt;
&lt;td&gt;2/110 = &lt;strong&gt;0.01818&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#61 in both lists&lt;/td&gt;
&lt;td&gt;2/121 = &lt;strong&gt;0.01653&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#62 in both lists&lt;/td&gt;
&lt;td&gt;2/122 = 0.01639 (tie)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Read the second row again. A document that both retrievers considered mediocre, 50th place each, outscores the document BM25 was most confident about.&lt;/p&gt;

&lt;p&gt;The break-even is easy to derive. A doc at rank &lt;code&gt;r&lt;/code&gt; in both lists beats an exclusive #1 when:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 / (k + r) &amp;gt; 1 / (k + 1)
       k + r &amp;lt; 2k + 2
           r &amp;lt; k + 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So with &lt;code&gt;k=60&lt;/code&gt;, every doc ranked 61st or better in both lists outranks your exclusive #1. If each retriever returns 50 results, that means &lt;strong&gt;every single overlapping doc&lt;/strong&gt; wins.&lt;/p&gt;

&lt;p&gt;That's where position 31 came from. My query hit 30 generic "connection error / timeout / retry" pages that both BM25 and the embedding model liked. All 30 appeared in both top-50 lists. The runbook appeared in one. Final position: overlap count plus one. It's not bad luck, it's arithmetic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do embeddings miss error codes in the first place?
&lt;/h2&gt;

&lt;p&gt;Embedding models compress meaning, and an identifier like &lt;code&gt;ERR_CONN_4012&lt;/code&gt; has very little meaning to compress. The tokenizer chops it into pieces like &lt;code&gt;ERR&lt;/code&gt;, &lt;code&gt;_&lt;/code&gt;, &lt;code&gt;CONN&lt;/code&gt;, &lt;code&gt;_&lt;/code&gt;, &lt;code&gt;401&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, and the model maps the whole thing to roughly "some connection error." Every page about connection errors lands nearby.&lt;/p&gt;

&lt;p&gt;BM25 sees the opposite picture. The full token is rare across the corpus, so its IDF is huge, and the one doc containing it shoots to the top.&lt;/p&gt;

&lt;p&gt;This split is the entire reason hybrid search exists. The queries where one retriever is decisively right and the other is lost are the queries hybrid search was supposed to save. Flat RRF treats that disagreement as noise and votes it down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does a reranker fix bad RRF fusion?
&lt;/h2&gt;

&lt;p&gt;A cross-encoder reranker only fixes RRF if the correct doc is inside the window it reranks. A lot of pipelines fuse, take the top 20, and rerank those. My runbook was at 31, so the reranker never saw it. The most accurate component in the pipeline was handed a list that didn't contain the answer.&lt;/p&gt;

&lt;p&gt;If you rerank, the window has to be at least as large as the plausible overlap between your retrievers. With two top-50 lists, the worst case for an exclusive #1 is position 51. I rerank the fused top 60 now. It costs more cross-encoder calls, but a reranker that sees the answer is cheaper than one that confidently orders 20 wrong ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you fix Reciprocal Rank Fusion for exact-match queries?
&lt;/h2&gt;

&lt;p&gt;Four changes, roughly in order of impact.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Detect identifier queries and pin exact matches
&lt;/h3&gt;

&lt;p&gt;If the query contains something that looks like a code, the user wants that string. Don't make them win a popularity contest for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;

&lt;span class="no"&gt;IDENT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"\b(?:[A-Z]{2,}[_-][A-Z0-9_-]+|[A-Z]+-\d+|0x[0-9a-fA-F]+|\w+\(\))"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hybrid_search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bm25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;bm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bm25&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;vc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fused&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;rrf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;idents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;IDENT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;idents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;exact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;bm&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;doc_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;idents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;fused&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exact&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fused&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;exact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fused&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The check against &lt;code&gt;doc_text&lt;/code&gt; matters. BM25's top 5 can still contain partial matches on &lt;code&gt;ERR&lt;/code&gt; or &lt;code&gt;CONN&lt;/code&gt;, and you only want to pin docs that contain the literal string.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Retrieve deeper on the vector side
&lt;/h3&gt;

&lt;p&gt;An exclusive #1 loses because the other list gives it zero. Give the other list a chance to give it something. If the vector search returns 500 instead of 50 and the runbook shows up at rank 400, it earns &lt;code&gt;1/460 = 0.00217&lt;/code&gt; extra:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1/61 + 1/460 = 0.01857   (runbook)
2/110        = 0.01818   (doc at #50 in both)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it beats every doc ranked 50th in both. It still loses to shared docs near the top, but it climbs out of the basement. Deeper ANN retrieval is cheap compared to the LLM call that follows.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Lower k, or weight the retrievers
&lt;/h3&gt;

&lt;p&gt;Lower &lt;code&gt;k&lt;/code&gt; makes top ranks matter more. From the break-even above, an exclusive #1 is beaten by docs in both lists down to rank &lt;code&gt;k + 1&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;k&lt;/th&gt;
&lt;th&gt;Exclusive #1 loses to shared docs ranked...&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;td&gt;1 through 61&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;1 through 21&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;1 through 11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1 through 2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Weighted RRF multiplies each retriever's contribution. Be careful: the weight also boosts shared docs' BM25 half, so it's weaker than it looks. With BM25 weight 2 and &lt;code&gt;k=60&lt;/code&gt;, a shared doc at rank &lt;code&gt;r&lt;/code&gt; scores &lt;code&gt;3/(60+r)&lt;/code&gt; against the exclusive doc's &lt;code&gt;2/61&lt;/code&gt;. Shared docs still win down to rank 31. Weight 3 still lets shared docs win down to rank 21. Weights help, but they don't solve the identifier case alone.&lt;/p&gt;

&lt;p&gt;I ended up with &lt;code&gt;k=20&lt;/code&gt; as the default and kept the identifier pin as the real fix.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Look at disagreement, don't average it
&lt;/h3&gt;

&lt;p&gt;The most useful debugging change was logging, per query, how many docs appeared in only one list and where they landed after fusion. A doc that's #1 in one retriever and missing from the other is a signal, not an outlier. If that pattern shows up on a large share of your queries, your two retrievers are answering different questions and flat fusion is hiding it.&lt;/p&gt;

&lt;h2&gt;
  
  
  When is RRF k=60 still the right choice?
&lt;/h2&gt;

&lt;p&gt;RRF with &lt;code&gt;k=60&lt;/code&gt; is a fine default when your queries are natural-language questions and both retrievers mostly agree. In that regime, the flat curve smooths out noise from either retriever, and consensus really is a good proxy for relevance. It's also hard to beat for zero-tuning setups.&lt;/p&gt;

&lt;p&gt;It's the wrong default for corpora full of identifiers: runbooks, API references, ticket histories, log search, product catalogs, legal citations. There, the query often has exactly one correct answer, and only the lexical retriever knows it.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why did my #1 BM25 match fall to position 31?
&lt;/h2&gt;

&lt;p&gt;Reciprocal Rank Fusion with &lt;code&gt;k=60&lt;/code&gt; gives rank #1 a score of &lt;code&gt;1/61&lt;/code&gt; and rank #50 a score of &lt;code&gt;1/110&lt;/code&gt;, so the rank curve is nearly flat and showing up in two lists dominates ranking high in one. Mathematically, any document ranked &lt;code&gt;k + 1&lt;/code&gt; or better in both lists beats a document ranked #1 in only one. My runbook was BM25's #1 and absent from the vector top 50, so it landed behind all 30 documents that appeared in both lists. To fix it, detect identifier-style queries and pin literal matches, retrieve deeper on the vector side so exclusive hits earn partial credit, lower &lt;code&gt;k&lt;/code&gt; (I use 20), and rerank a window wide enough to include the worst-case position of an exclusive #1.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>llm</category>
      <category>machinelearning</category>
      <category>ai</category>
      <category>python</category>
    </item>
    <item>
      <title>Why Docker Re-Runs npm ci Every Build: .git Was in My Context</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Tue, 17 Nov 2026 20:47:04 +0000</pubDate>
      <link>https://dev.to/ji_ai/why-docker-re-runs-npm-ci-every-build-git-was-in-my-context-efn</link>
      <guid>https://dev.to/ji_ai/why-docker-re-runs-npm-ci-every-build-git-was-in-my-context-efn</guid>
      <description>&lt;p&gt;I fixed a typo in a README. One character. Then I ran &lt;code&gt;docker build&lt;/code&gt; and watched it reinstall every package in my &lt;code&gt;package-lock.json&lt;/code&gt; from scratch, which took about a minute and a half.&lt;/p&gt;

&lt;p&gt;I hadn't touched &lt;code&gt;package.json&lt;/code&gt; and I hadn't touched the lockfile. Docker re-ran &lt;code&gt;npm ci&lt;/code&gt; on every build anyway, and for a few weeks I assumed that was just how Docker worked.&lt;/p&gt;

&lt;p&gt;It isn't. My build context had two things in it that never belonged there, and one of them changes every time you look at it.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Docker re-runs &lt;code&gt;npm ci&lt;/code&gt; every build when any file copied &lt;em&gt;before&lt;/em&gt; that step changes. With &lt;code&gt;COPY . .&lt;/code&gt; above &lt;code&gt;RUN npm ci&lt;/code&gt;, that means every file in the repo.&lt;/li&gt;
&lt;li&gt;Without a &lt;code&gt;.dockerignore&lt;/code&gt;, your &lt;code&gt;.git&lt;/code&gt; folder is part of the build context. &lt;code&gt;.git/index&lt;/code&gt; can be rewritten by a plain &lt;code&gt;git status&lt;/code&gt;, so the cache can break even when you haven't made a commit.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;.dockerignore&lt;/code&gt;, &lt;code&gt;node_modules&lt;/code&gt; only matches the folder at the context root. Nested folders in a monorepo need &lt;code&gt;**/node_modules&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Fix: copy only &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt;, run &lt;code&gt;npm ci&lt;/code&gt;, then &lt;code&gt;COPY . .&lt;/code&gt;. Add a &lt;code&gt;.dockerignore&lt;/code&gt; that excludes &lt;code&gt;.git&lt;/code&gt;, &lt;code&gt;**/node_modules&lt;/code&gt;, build output, and &lt;code&gt;.env&lt;/code&gt; files.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why does Docker re-run npm ci every build?
&lt;/h2&gt;

&lt;p&gt;Docker re-runs &lt;code&gt;npm ci&lt;/code&gt; every build because the layer cache is a chain. When one step's cache key changes, every step after it rebuilds too. If &lt;code&gt;COPY . .&lt;/code&gt; sits above &lt;code&gt;RUN npm ci&lt;/code&gt;, any changed byte anywhere in the build context invalidates the copy, and the install runs again.&lt;/p&gt;

&lt;p&gt;This was my original Dockerfile, and I'd bet half of you have one like it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt; node:22-slim
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /app
&lt;span class="k"&gt;COPY&lt;/span&gt; . .
&lt;span class="k"&gt;RUN&lt;/span&gt; npm ci
&lt;span class="k"&gt;RUN&lt;/span&gt; npm run build
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;, &lt;span class="s2"&gt;"dist/server.js"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It reads fine and builds fine. It also ties a 90-second install to every file in the repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Docker decide a COPY layer is cached?
&lt;/h2&gt;

&lt;p&gt;For &lt;code&gt;COPY&lt;/code&gt; and &lt;code&gt;ADD&lt;/code&gt;, Docker checksums the contents of the files being copied. File modification times aren't part of that checksum, so touching a file without changing it won't break anything. Changing a single byte in any copied file will.&lt;/p&gt;

&lt;p&gt;The checksum covers everything the instruction copies, and &lt;code&gt;COPY . .&lt;/code&gt; copies the whole context minus whatever &lt;code&gt;.dockerignore&lt;/code&gt; excludes. With no &lt;code&gt;.dockerignore&lt;/code&gt;, that's every file on disk under your project root, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.git/&lt;/code&gt;, the entire repository history and its working files&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;node_modules/&lt;/code&gt; from your host machine&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dist/&lt;/code&gt;, &lt;code&gt;coverage/&lt;/code&gt;, &lt;code&gt;.next/&lt;/code&gt;, and other build output&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.env&lt;/code&gt; files, which is a separate and worse problem&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any one of those changing is enough to make the build start over at &lt;code&gt;COPY . .&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does .git break the Docker build cache without a commit?
&lt;/h2&gt;

&lt;p&gt;Some files inside &lt;code&gt;.git&lt;/code&gt; change constantly, and not only when you commit. The most important one is &lt;code&gt;.git/index&lt;/code&gt;. Git keeps cached file stat information in it, and commands like &lt;code&gt;git status&lt;/code&gt; can refresh that information and write the index back to disk, even when nothing is staged.&lt;/p&gt;

&lt;p&gt;That means none of these needed a commit to break my cache:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;running &lt;code&gt;git status&lt;/code&gt; in a terminal&lt;/li&gt;
&lt;li&gt;my editor's Git integration polling the repo in the background&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git fetch&lt;/code&gt;, which updates &lt;code&gt;FETCH_HEAD&lt;/code&gt; and remote refs&lt;/li&gt;
&lt;li&gt;switching branches and switching back, which leaves the files identical but changes &lt;code&gt;HEAD&lt;/code&gt; history and the reflog&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So two back-to-back builds with no code changes could still miss the cache, because something read the repo between them. That's why this felt random for so long: the cache wasn't randomly missing, it was following &lt;code&gt;.git&lt;/code&gt; exactly.&lt;/p&gt;

&lt;p&gt;Your image almost never needs &lt;code&gt;.git&lt;/code&gt;. If you need a commit SHA inside the image, pass it in with &lt;code&gt;--build-arg GIT_SHA=$(git rev-parse HEAD)&lt;/code&gt; instead of shipping the whole repo history.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I see what's in my Docker build context?
&lt;/h2&gt;

&lt;p&gt;Build a throwaway image that copies the context and lists it. This shows you exactly what Docker received after &lt;code&gt;.dockerignore&lt;/code&gt; was applied:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build --no-cache --progress=plain -f - . &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s1"&gt;'EOF'&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; busybox
&lt;span class="k"&gt;COPY&lt;/span&gt; . /ctx
&lt;span class="k"&gt;RUN&lt;/span&gt; du -sh /ctx &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; find /ctx -maxdepth 2 &lt;span class="o"&gt;|&lt;/span&gt; sort &lt;span class="o"&gt;|&lt;/span&gt; head -80
EOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-f -&lt;/code&gt; reads the Dockerfile from stdin while &lt;code&gt;.&lt;/code&gt; remains the context, so you don't need a temp file. &lt;code&gt;--progress=plain&lt;/code&gt; prints the &lt;code&gt;RUN&lt;/code&gt; output instead of collapsing it.&lt;/p&gt;

&lt;p&gt;The first time I ran this, &lt;code&gt;du&lt;/code&gt; reported a context of over a gigabyte for a service whose source is a few megabytes. Most of it was &lt;code&gt;.git&lt;/code&gt; and two &lt;code&gt;node_modules&lt;/code&gt; folders. You can also see the transfer at the top of a normal build in the &lt;code&gt;transferring context&lt;/code&gt; line. If that number is large for a small app, it's coming from one of these folders.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why doesn't node_modules in .dockerignore exclude nested folders?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;.dockerignore&lt;/code&gt; doesn't match patterns the way &lt;code&gt;.gitignore&lt;/code&gt; does. A bare &lt;code&gt;node_modules&lt;/code&gt; line only matches &lt;code&gt;node_modules&lt;/code&gt; at the root of the build context. Unlike &lt;code&gt;.gitignore&lt;/code&gt;, it does not match &lt;code&gt;packages/api/node_modules&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is what got me after the first fix. I added &lt;code&gt;node_modules&lt;/code&gt; to &lt;code&gt;.dockerignore&lt;/code&gt;, the context shrank, and builds were still slow. My repo is a small workspace monorepo, and every package had its own &lt;code&gt;node_modules&lt;/code&gt; that was still being sent.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;**/&lt;/code&gt; for anything that can appear at any depth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# .dockerignore
.git
**/node_modules
**/dist
**/coverage
**/.next
**/*.log
**/.env*
.DS_Store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two more rules worth knowing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Docker reads &lt;code&gt;.dockerignore&lt;/code&gt; from the &lt;strong&gt;root of the build context&lt;/strong&gt;, not from the folder that holds your Dockerfile. If you build with &lt;code&gt;docker build -f docker/api.Dockerfile .&lt;/code&gt;, the file belongs in &lt;code&gt;.&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;With BuildKit, a file named after your Dockerfile (for example &lt;code&gt;api.Dockerfile.dockerignore&lt;/code&gt;, placed next to it) takes priority over the root one. That's useful when two images built from the same context need different exclusions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Does host node_modules actually break anything besides speed?
&lt;/h2&gt;

&lt;p&gt;Yes. If your host's &lt;code&gt;node_modules&lt;/code&gt; gets into the context and you &lt;code&gt;COPY . .&lt;/code&gt; after &lt;code&gt;npm ci&lt;/code&gt;, the host folder &lt;strong&gt;overwrites&lt;/strong&gt; the one you just installed in the container.&lt;/p&gt;

&lt;p&gt;On a Mac, that means you can end up with macOS-native binaries inside a Linux image. Packages with native code or platform-specific binaries (esbuild, sharp, bcrypt, and others) then fail at runtime or build time with errors about the wrong platform. The build passed on your machine, and the container still breaks.&lt;/p&gt;

&lt;p&gt;Excluding &lt;code&gt;**/node_modules&lt;/code&gt; fixes speed and correctness together.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Dockerfile order keeps npm ci cached?
&lt;/h2&gt;

&lt;p&gt;Copy the dependency manifests first, install, then copy the source. The install layer then depends only on &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt;, so it stays cached until your dependencies actually change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# syntax=docker/dockerfile:1&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; node:22-slim
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /app

&lt;span class="c"&gt;# 1. Only the files that decide what gets installed&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt; package.json package-lock.json ./

&lt;span class="c"&gt;# 2. Install. Cached until one of those two files changes.&lt;/span&gt;
&lt;span class="k"&gt;RUN&lt;/span&gt; --mount=type=cache,target=/root/.npm npm ci

&lt;span class="c"&gt;# 3. Everything else, filtered by .dockerignore&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt; . .
&lt;span class="k"&gt;RUN&lt;/span&gt; npm run build

&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;, &lt;span class="s2"&gt;"dist/server.js"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;--mount=type=cache&lt;/code&gt; line keeps npm's download cache between builds on the same builder. Even when the lockfile does change and &lt;code&gt;npm ci&lt;/code&gt; has to run, most tarballs come from the local cache instead of the registry. It needs BuildKit, which is the default builder in current Docker releases.&lt;/p&gt;

&lt;p&gt;For a workspace monorepo, step 1 also has to copy every workspace &lt;code&gt;package.json&lt;/code&gt;, because &lt;code&gt;npm ci&lt;/code&gt; needs them to resolve the workspace graph. List them explicitly. It's tedious, but it's the only way to keep a change to one package's source from invalidating the install.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed after the fix?
&lt;/h2&gt;

&lt;p&gt;On my machine, a source-only change now skips straight past &lt;code&gt;npm ci&lt;/code&gt; and shows &lt;code&gt;CACHED&lt;/code&gt; on that step. The build is down to the &lt;code&gt;COPY . .&lt;/code&gt; plus &lt;code&gt;npm run build&lt;/code&gt;, which takes seconds instead of most of a minute. The build context went from over a gigabyte to a few megabytes, so the &lt;code&gt;transferring context&lt;/code&gt; line finishes almost instantly.&lt;/p&gt;

&lt;p&gt;The README typo I started with now costs nothing, which is how it should have been from the start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;.dockerignore&lt;/code&gt; at the &lt;strong&gt;context root&lt;/strong&gt; with &lt;code&gt;.git&lt;/code&gt;, &lt;code&gt;**/node_modules&lt;/code&gt;, build output, and &lt;code&gt;**/.env*&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Run the busybox listing build above and confirm what's left in the context.&lt;/li&gt;
&lt;li&gt;Move &lt;code&gt;COPY package.json package-lock.json ./&lt;/code&gt; and &lt;code&gt;RUN npm ci&lt;/code&gt; above &lt;code&gt;COPY . .&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;--mount=type=cache,target=/root/.npm&lt;/code&gt; to the install step.&lt;/li&gt;
&lt;li&gt;Build twice with &lt;code&gt;--progress=plain&lt;/code&gt; and check that &lt;code&gt;npm ci&lt;/code&gt; says &lt;code&gt;CACHED&lt;/code&gt; the second time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  So why does Docker re-run npm ci every build?
&lt;/h2&gt;

&lt;p&gt;Docker re-runs &lt;code&gt;npm ci&lt;/code&gt; every build when the install step sits below a &lt;code&gt;COPY&lt;/code&gt; that includes files that change between builds. With &lt;code&gt;COPY . .&lt;/code&gt; and no &lt;code&gt;.dockerignore&lt;/code&gt;, that includes &lt;code&gt;.git&lt;/code&gt;, where files like &lt;code&gt;.git/index&lt;/code&gt; can be rewritten by a plain &lt;code&gt;git status&lt;/code&gt;, and your host &lt;code&gt;node_modules&lt;/code&gt;, which can also overwrite the container's platform-specific install. To fix it, copy &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt; first, run &lt;code&gt;npm ci&lt;/code&gt;, then copy the source, and add a &lt;code&gt;.dockerignore&lt;/code&gt; at the build context root that excludes &lt;code&gt;.git&lt;/code&gt;, &lt;code&gt;**/node_modules&lt;/code&gt; (with the &lt;code&gt;**/&lt;/code&gt; prefix, because a bare pattern only matches at the root), build output, and &lt;code&gt;.env&lt;/code&gt; files.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>tooling</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Pydantic Lax Mode Hid 689 Type Errors in My Claude JSON Outputs</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Tue, 17 Nov 2026 08:45:26 +0000</pubDate>
      <link>https://dev.to/ji_ai/pydantic-lax-mode-hid-689-type-errors-in-my-claude-json-outputs-idd</link>
      <guid>https://dev.to/ji_ai/pydantic-lax-mode-hid-689-type-errors-in-my-claude-json-outputs-idd</guid>
      <description>&lt;p&gt;My contract extraction pipeline had a 99.7% validation pass rate. Then a Node script summed the contract values and printed &lt;code&gt;01200.50300&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's not a number. It's what JavaScript gives you when you add &lt;code&gt;0 + "1200.50" + 300&lt;/code&gt;. The amount had come back from Claude as a string, Pydantic had quietly accepted it, and I had stored the raw JSON. Pydantic lax mode is the default in v2, and it had been coercing my LLM's type mistakes into valid-looking objects for weeks.&lt;/p&gt;

&lt;p&gt;So I re-validated all 4,000 stored outputs with strict mode switched on. 588 records failed. Here's what was hiding in them.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pydantic v2 validates in lax mode by default.&lt;/strong&gt; It turns &lt;code&gt;"1200.50"&lt;/code&gt; into a float, &lt;code&gt;"yes"&lt;/code&gt; into &lt;code&gt;True&lt;/code&gt;, &lt;code&gt;12.0&lt;/code&gt; into &lt;code&gt;12&lt;/code&gt;, and an integer like &lt;code&gt;2023&lt;/code&gt; in a &lt;code&gt;datetime&lt;/code&gt; field into a Unix timestamp.&lt;/li&gt;
&lt;li&gt;Re-running 4,000 stored Claude outputs with &lt;code&gt;model_validate_json(raw, strict=True)&lt;/code&gt; failed &lt;strong&gt;588 records (14.7%)&lt;/strong&gt; that lax mode had passed, with &lt;strong&gt;689 coerced fields&lt;/strong&gt; total.&lt;/li&gt;
&lt;li&gt;Most coercions were harmless. &lt;strong&gt;4 were silently wrong&lt;/strong&gt;: year-only dates became &lt;code&gt;1970-01-01T00:33:43Z&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The real damage came downstream: I stored the raw LLM JSON, so a non-Python consumer saw strings where Python saw floats.&lt;/li&gt;
&lt;li&gt;Fix: &lt;code&gt;ConfigDict(strict=True)&lt;/code&gt;, feed validation errors back for one repair call, and track the strict failure rate as a drift metric.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Pydantic lax mode?
&lt;/h2&gt;

&lt;p&gt;Pydantic lax mode is the default validation behavior in Pydantic v2: when an input doesn't match the declared type exactly, Pydantic tries a well-defined set of conversions before failing. A &lt;code&gt;float&lt;/code&gt; field accepts the string &lt;code&gt;"42.5"&lt;/code&gt;. A &lt;code&gt;bool&lt;/code&gt; field accepts &lt;code&gt;"yes"&lt;/code&gt;, &lt;code&gt;"no"&lt;/code&gt;, &lt;code&gt;"true"&lt;/code&gt;, &lt;code&gt;"1"&lt;/code&gt; and friends. An &lt;code&gt;int&lt;/code&gt; field accepts &lt;code&gt;12.0&lt;/code&gt; because it has no fractional part.&lt;/p&gt;

&lt;p&gt;For form data and query strings, this is exactly what you want. Everything arrives as a string, and lax mode saves you a hundred lines of &lt;code&gt;int(request.args["page"])&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For LLM output, it means you never find out whether the model followed your schema. You find out whether the model's output could be &lt;em&gt;bent&lt;/em&gt; into your schema. Those are different questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;The pipeline is boring on purpose:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pull a contract PDF, extract text.&lt;/li&gt;
&lt;li&gt;Send it to Claude with a prompt describing the fields and asking for JSON only.&lt;/li&gt;
&lt;li&gt;Parse with &lt;code&gt;Contract.model_validate_json(raw)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;On success, store the raw JSON string plus a few columns in Postgres.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The model looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BaseModel&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;counterparty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;total_value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;
    &lt;span class="n"&gt;term_months&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;auto_renews&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="n"&gt;signed_at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Over a few weeks it processed 4,000 documents. 13 failed validation, mostly amounts written as &lt;code&gt;"1,200.50"&lt;/code&gt; with a thousands separator (lax mode won't parse that). I retried those by hand and felt good about a 99.7% pass rate.&lt;/p&gt;

&lt;p&gt;Step 4 was the mistake. I stored the &lt;strong&gt;raw&lt;/strong&gt; string for audit purposes instead of &lt;code&gt;contract.model_dump_json()&lt;/code&gt;. The Python side never noticed, because Python only ever touched the validated object. The reporting script, written in Node, read the raw column directly.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I find out what Pydantic coerced?
&lt;/h2&gt;

&lt;p&gt;Re-validate the same input with &lt;code&gt;strict=True&lt;/code&gt; and diff the results. You don't need to change the model. &lt;code&gt;model_validate_json&lt;/code&gt; takes a &lt;code&gt;strict&lt;/code&gt; argument that overrides the model's config for one call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;ValidationError&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;

&lt;span class="n"&gt;coerced&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;failed_records&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# 4,000 stored raw outputs&lt;/span&gt;
    &lt;span class="nc"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_validate_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# lax: passes&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_validate_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;ValidationError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;failed_records&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;coerced&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"loc"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every record that passes lax and fails strict relied on at least one coercion. Every error in the strict run is one coerced field.&lt;/p&gt;

&lt;p&gt;The result from my 4,000 outputs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What lax mode did&lt;/th&gt;
&lt;th&gt;Fields&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Numeric string to float/int (&lt;code&gt;"1200.50"&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;452&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bool string to bool (&lt;code&gt;"yes"&lt;/code&gt;, &lt;code&gt;"no"&lt;/code&gt;, &lt;code&gt;"true"&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;163&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Whole float to int (&lt;code&gt;12.0&lt;/code&gt; to &lt;code&gt;12&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;70&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Integer to datetime as Unix timestamp&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;689&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;689 coerced fields across 588 records. 14.7% of my "valid" outputs weren't valid by the schema I'd written.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why did my dates turn into 1970?
&lt;/h2&gt;

&lt;p&gt;Because Pydantic lax mode reads an integer in a &lt;code&gt;datetime&lt;/code&gt; field as a Unix timestamp. When a contract only said "signed in 2023", Claude sometimes returned &lt;code&gt;"signed_at": 2023&lt;/code&gt; instead of &lt;code&gt;null&lt;/code&gt; or a full date. Pydantic took that as 2,023 seconds after the epoch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_validate_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'{..., "signed_at": 2023}'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signed_at&lt;/span&gt;
&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1970&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;43&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tzinfo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;TzInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;UTC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No error. No warning. A valid, timezone-aware datetime that is wrong by 53 years.&lt;/p&gt;

&lt;p&gt;Only 4 of 4,000 records hit this. But those 4 sorted to the top of every "oldest contracts" view, and one of them was flagged by my renewal reminder as overdue by half a century. That's the scary category of coercion: not a type the model got slightly wrong, but a value whose &lt;em&gt;meaning&lt;/em&gt; changed during conversion.&lt;/p&gt;

&lt;p&gt;The other 685 coercions were technically correct conversions. &lt;code&gt;"1200.50"&lt;/code&gt; really is 1200.5. &lt;code&gt;"yes"&lt;/code&gt; really means &lt;code&gt;True&lt;/code&gt;. Python was fine. The JSON on disk was not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is Pydantic lax mode wrong for LLM output?
&lt;/h2&gt;

&lt;p&gt;Lax mode isn't wrong, but it answers the wrong question for LLM output. It tells you "this can be made into a Contract." For a model you're prompting, you want to know "the model produced a Contract." The gap between those two is exactly where model drift hides: a prompt change, a model upgrade, or a weird document that nudges the model from numbers to strings.&lt;/p&gt;

&lt;p&gt;If you only ever consume the validated Python object, lax mode is mostly safe, apart from the timestamp trap. If anything else reads the raw output (a JS frontend, a data warehouse, a second service, an audit log someone will query later), lax mode lets inconsistent JSON leak out with a green checkmark on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I turn on Pydantic strict mode?
&lt;/h2&gt;

&lt;p&gt;Set &lt;code&gt;strict=True&lt;/code&gt; in the model config. Every field then rejects inputs of the wrong type instead of converting them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ConfigDict&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;model_config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConfigDict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;counterparty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;total_value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;
    &lt;span class="n"&gt;term_months&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;auto_renews&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="n"&gt;signed_at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One nuance worth knowing: strict mode with JSON input still accepts ISO 8601 strings for &lt;code&gt;datetime&lt;/code&gt;, because JSON has no native datetime type. So &lt;code&gt;"2023-04-12T00:00:00Z"&lt;/code&gt; passes, while &lt;code&gt;2023&lt;/code&gt; fails. That's the behavior you want.&lt;/p&gt;

&lt;p&gt;If you can't flip the whole model, use per-field strict types (&lt;code&gt;StrictInt&lt;/code&gt;, &lt;code&gt;StrictBool&lt;/code&gt;, or &lt;code&gt;Annotated[int, Strict()]&lt;/code&gt;) on the fields that matter most. For me that was &lt;code&gt;signed_at&lt;/code&gt; and &lt;code&gt;total_value&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What did fixing it actually cost?
&lt;/h2&gt;

&lt;p&gt;Turning on strict mode moved 588 records from "pass" to "fail," so I needed a recovery path. I added one repair call: send the original output plus Pydantic's error list back to Claude and ask for corrected JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{'.'.join(map(str, e['loc']))}: {e['msg']} (got {e['input']!r})"&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pydantic's error messages are good enough that the model rarely needs more context. Results on the 588 failures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;571 fixed&lt;/strong&gt; on the first repair call&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;17 sent to manual review&lt;/strong&gt;, mostly contracts with genuinely ambiguous dates&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;+14.7% more API calls&lt;/strong&gt; on that batch, the one honest cost of this whole fix&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then I fixed the prompt. I'd described the fields in prose ("the total value of the contract"). I rewrote it to state types explicitly ("&lt;code&gt;total_value&lt;/code&gt;: a JSON number, no quotes, no currency symbol, no thousands separator; &lt;code&gt;signed_at&lt;/code&gt;: ISO 8601 string or null, never a bare year"). On a fresh sample of 500 documents, the strict failure rate dropped to 12 of 500, or 2.4%.&lt;/p&gt;

&lt;p&gt;And I changed step 4 to store &lt;code&gt;contract.model_dump_json()&lt;/code&gt;. If you keep the raw output for audit, keep it in a separate column that nothing downstream reads.&lt;/p&gt;

&lt;h2&gt;
  
  
  The metric I wish I'd had from day one
&lt;/h2&gt;

&lt;p&gt;Lax pass rate is a vanity number. It measures Pydantic's flexibility, not your model's compliance.&lt;/p&gt;

&lt;p&gt;I now log two numbers per batch: strict pass rate on the first call and strict pass rate after repair. The first one is the drift alarm. When I swapped model versions last month, first-call strict pass dipped from 97.6% to 93% in a day, and I caught it before a single bad row landed. Lax mode would have shown 99.7% the whole time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does Pydantic lax mode hide LLM type errors?
&lt;/h2&gt;

&lt;p&gt;Yes. Pydantic v2 validates in lax mode by default, so it silently converts numeric strings to numbers, &lt;code&gt;"yes"&lt;/code&gt;/&lt;code&gt;"no"&lt;/code&gt; to booleans, whole floats to ints, and integers in &lt;code&gt;datetime&lt;/code&gt; fields to Unix timestamps. In my pipeline, re-validating 4,000 Claude JSON outputs with &lt;code&gt;model_validate_json(raw, strict=True)&lt;/code&gt; exposed 689 coerced fields across 588 records that lax mode had passed, including 4 year-only dates turned into &lt;code&gt;1970-01-01&lt;/code&gt;. Use &lt;code&gt;ConfigDict(strict=True)&lt;/code&gt; for LLM output, feed validation errors back for one repair call, serialize with &lt;code&gt;model_dump_json()&lt;/code&gt; instead of storing raw output, and track the strict pass rate as your real compliance metric.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>llm</category>
      <category>ai</category>
    </item>
    <item>
      <title>Speculative Decoding Acceptance Rate: Why 70% Caps You Near 2x</title>
      <dc:creator>jidonglab</dc:creator>
      <pubDate>Mon, 16 Nov 2026 20:42:47 +0000</pubDate>
      <link>https://dev.to/ji_ai/speculative-decoding-acceptance-rate-why-70-caps-you-near-2x-1p29</link>
      <guid>https://dev.to/ji_ai/speculative-decoding-acceptance-rate-why-70-caps-you-near-2x-1p29</guid>
      <description>&lt;p&gt;The dashboard said &lt;strong&gt;44% draft acceptance&lt;/strong&gt;. My first instinct was to rip speculative decoding out of the serving config. Less than half the draft tokens survived, so the draft model was clearly wasting GPU time, right?&lt;/p&gt;

&lt;p&gt;Wrong. A 44% speculative decoding acceptance rate on that dashboard meant the draft model agreed with the big model about &lt;strong&gt;70% of the time per token&lt;/strong&gt;. That number was fine. The thing actually limiting my speedup was something the dashboard didn't show at all: how many requests were sharing the GPU.&lt;/p&gt;

&lt;p&gt;This post is the math I wish I'd done before touching the config. It's short, it fits in a Python function, and it explains almost every confusing speculative decoding result you'll see in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Speculative decoding has a small &lt;strong&gt;draft model&lt;/strong&gt; guess &lt;code&gt;k&lt;/code&gt; tokens, then the big &lt;strong&gt;target model&lt;/strong&gt; checks all &lt;code&gt;k&lt;/code&gt; in one forward pass. Output quality is unchanged; only speed changes.&lt;/li&gt;
&lt;li&gt;With per-token acceptance &lt;code&gt;α&lt;/code&gt;, the expected tokens per target pass is &lt;code&gt;(1 - α^(k+1)) / (1 - α)&lt;/code&gt;. At &lt;code&gt;α = 0.7, k = 4&lt;/code&gt; that's &lt;strong&gt;2.77 tokens&lt;/strong&gt;, and after paying for the draft model (10% of target cost per step) the ceiling is about &lt;strong&gt;1.98x&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;"Acceptance rate" on dashboards is often &lt;strong&gt;accepted / drafted tokens&lt;/strong&gt;, which is much lower than &lt;code&gt;α&lt;/code&gt;. 44% accepted/drafted at &lt;code&gt;k = 4&lt;/code&gt; corresponds to &lt;code&gt;α ≈ 0.7&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;More draft tokens is not better. At &lt;code&gt;α = 0.7&lt;/code&gt;, &lt;code&gt;k = 4&lt;/code&gt; peaks at 1.98x; &lt;code&gt;k = 8&lt;/code&gt; drops to 1.78x.&lt;/li&gt;
&lt;li&gt;The speedup comes from idle compute in memory-bound decoding. Large batches use up that idle compute, and speculative decoding can become a net loss.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How does speculative decoding actually work?
&lt;/h2&gt;

&lt;p&gt;Speculative decoding works because a big model verifying 5 tokens costs about the same as generating 1. At small batch sizes, decoding is bound by memory bandwidth. The GPU spends its time streaming weights from HBM, and the math units mostly sit around waiting.&lt;/p&gt;

&lt;p&gt;So you let a cheap model run ahead:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The draft model generates &lt;code&gt;k&lt;/code&gt; tokens autoregressively (cheap, because it's small).&lt;/li&gt;
&lt;li&gt;The target model runs &lt;strong&gt;one&lt;/strong&gt; forward pass over all &lt;code&gt;k&lt;/code&gt; positions plus one extra.&lt;/li&gt;
&lt;li&gt;Walk left to right. Accept draft token &lt;code&gt;x&lt;/code&gt; with probability &lt;code&gt;min(1, p(x) / q(x))&lt;/code&gt;, where &lt;code&gt;p&lt;/code&gt; is the target's distribution and &lt;code&gt;q&lt;/code&gt; is the draft's.&lt;/li&gt;
&lt;li&gt;At the first rejection, resample that position from the leftover distribution &lt;code&gt;max(0, p - q)&lt;/code&gt; (normalized), and throw away everything after it.&lt;/li&gt;
&lt;li&gt;If all &lt;code&gt;k&lt;/code&gt; pass, you also get the target's next token for free.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That rejection-sampling step is the clever part. It guarantees the output follows &lt;strong&gt;exactly&lt;/strong&gt; the target model's distribution. Speculative decoding is lossless. If your outputs change in quality after enabling it, you have a bug somewhere else, not a tradeoff.&lt;/p&gt;

&lt;p&gt;It also guarantees every target pass produces at least one token. The worst case is roughly normal decoding plus the wasted draft work.&lt;/p&gt;

&lt;h2&gt;
  
  
  How many tokens does one verification pass produce?
&lt;/h2&gt;

&lt;p&gt;If each draft token is accepted independently with probability &lt;code&gt;α&lt;/code&gt;, the expected number of tokens per target forward pass is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;E[tokens] = (1 - α^(k+1)) / (1 - α)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This comes from the original speculative decoding analysis (Leviathan et al., 2023). The independence assumption isn't perfectly true, but it's close enough to reason with.&lt;/p&gt;

&lt;p&gt;Here's what it gives at &lt;code&gt;k = 4&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;per-token α&lt;/th&gt;
&lt;th&gt;tokens per target pass&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;1.94&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.6&lt;/td&gt;
&lt;td&gt;2.31&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.7&lt;/td&gt;
&lt;td&gt;2.77&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.8&lt;/td&gt;
&lt;td&gt;3.36&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.9&lt;/td&gt;
&lt;td&gt;4.10&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice how brutal the curve is. Acceptance is a chain: token 3 only counts if tokens 1 and 2 also survived. At &lt;code&gt;α = 0.7&lt;/code&gt;, the chance all four survive is &lt;code&gt;0.7^4 = 0.24&lt;/code&gt;. Most of the time you're throwing away the tail of the draft.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does 70% acceptance cap you near 2x?
&lt;/h2&gt;

&lt;p&gt;Because tokens per pass isn't speedup. You also pay for the draft model. If one draft step costs &lt;code&gt;c&lt;/code&gt; relative to one target step, the rough speedup is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;speedup ≈ E[tokens] / (k * c + 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;α = 0.7&lt;/code&gt;, &lt;code&gt;k = 4&lt;/code&gt;, &lt;code&gt;c = 0.1&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2.77 / (4 * 0.1 + 1) = 2.77 / 1.4 = 1.98x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the ceiling under ideal memory-bound conditions. Real kernels add overhead for the extra scheduling and the verification bookkeeping, so expect to land below it.&lt;/p&gt;

&lt;p&gt;Why &lt;code&gt;c = 0.1&lt;/code&gt; when a 1B draft against a 70B target is a parameter ratio closer to 0.014? Because a tiny model doesn't run 70x faster. Kernel launches, attention over the KV cache, and sampling all have fixed costs that don't shrink with parameter count. Measure &lt;code&gt;c&lt;/code&gt; on your own hardware: time one draft decode step and one target decode step at the same batch size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is my speculative decoding acceptance rate so low on the dashboard?
&lt;/h2&gt;

&lt;p&gt;Because the dashboard is probably reporting &lt;strong&gt;accepted draft tokens divided by drafted tokens&lt;/strong&gt;, and that number is mechanically lower than per-token &lt;code&gt;α&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Expected accepted draft tokens per step is &lt;code&gt;α + α² + ... + α^k&lt;/code&gt;. At &lt;code&gt;α = 0.7, k = 4&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.7 + 0.49 + 0.343 + 0.2401 = 1.77 accepted out of 4 drafted = 44%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So "44% acceptance" and "70% acceptance" can describe the exact same system. Going the other way, if your dashboard shows 70% accepted/drafted at &lt;code&gt;k = 4&lt;/code&gt;, the per-token &lt;code&gt;α&lt;/code&gt; is about &lt;strong&gt;0.86&lt;/strong&gt;, which is excellent and puts you around 3.6 tokens per pass.&lt;/p&gt;

&lt;p&gt;This is the metric artifact that nearly made me turn the feature off. Before you compare numbers across blog posts, papers, or your own experiments, check which definition you're looking at. Mean acceptance length (tokens emitted per target pass) is the least ambiguous one. Recent vLLM versions also log per-position acceptance rates, which show the chain decay directly: position 1 high, position 4 much lower.&lt;/p&gt;

&lt;p&gt;Here's the converter I keep around:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;tokens_per_pass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;alpha&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;speedup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;tokens_per_pass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;alpha_from_dashboard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;accepted_over_drafted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Invert accepted/drafted back to per-token alpha via bisection."""&lt;/span&gt;
    &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.9999&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
        &lt;span class="n"&gt;accepted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;accepted&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;accepted_over_drafted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;alpha_from_dashboard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# ~0.70&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;speedup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# 1.98&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How many draft tokens should I use?
&lt;/h2&gt;

&lt;p&gt;Fewer than you think. Each extra draft token costs a full draft step, but only pays off if every token before it was accepted. At &lt;code&gt;α = 0.7, c = 0.1&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;k&lt;/th&gt;
&lt;th&gt;tokens per pass&lt;/th&gt;
&lt;th&gt;speedup ceiling&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1.70&lt;/td&gt;
&lt;td&gt;1.55x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2.19&lt;/td&gt;
&lt;td&gt;1.83x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2.53&lt;/td&gt;
&lt;td&gt;1.95x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2.77&lt;/td&gt;
&lt;td&gt;1.98x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;2.94&lt;/td&gt;
&lt;td&gt;1.96x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;3.06&lt;/td&gt;
&lt;td&gt;1.91x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;3.20&lt;/td&gt;
&lt;td&gt;1.78x&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The peak is flat and early. Going from &lt;code&gt;k = 4&lt;/code&gt; to &lt;code&gt;k = 8&lt;/code&gt; doubles your draft work for a worse result. If your &lt;code&gt;α&lt;/code&gt; is lower, say 0.5, the optimum moves down to 2 or 3. Only push &lt;code&gt;k&lt;/code&gt; up when &lt;code&gt;α&lt;/code&gt; is genuinely high, like code completion or edits that copy large chunks of the prompt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does speculative decoding get slower under heavy load?
&lt;/h2&gt;

&lt;p&gt;Because the speedup is paid for with &lt;strong&gt;spare compute&lt;/strong&gt;, and batching spends that same spare compute.&lt;/p&gt;

&lt;p&gt;Rough numbers for an H100 SXM: about 990 TFLOPS of dense BF16 compute and about 3.35 TB/s of HBM bandwidth. That's roughly &lt;strong&gt;295 FLOPs per byte&lt;/strong&gt; before the GPU becomes compute-bound. A BF16 weight is 2 bytes and costs about 2 FLOPs per token that passes through it, so each token in the batch adds about 1 FLOP per byte of weight traffic. Ignoring attention and KV cache traffic, you need on the order of 300 tokens in flight per forward pass before the math units are actually busy.&lt;/p&gt;

&lt;p&gt;At batch size 1, a 70B model reads 140 GB of weights to do 140 GFLOPs of work. The compute units are idle more than 99% of the time. Verifying 5 tokens instead of 1 is basically free.&lt;/p&gt;

&lt;p&gt;Now run 60 concurrent sequences with &lt;code&gt;k = 4&lt;/code&gt;. Every verification pass pushes &lt;code&gt;60 × 5 = 300&lt;/code&gt; tokens through the target model. You're at the ridge point. Every rejected draft token is now real compute you paid for and threw away, and the draft model is competing for the same GPU. Past that point, plain continuous batching without speculation can beat you on throughput, and your tail latency gets worse too.&lt;/p&gt;

&lt;p&gt;This is why speculative decoding shines for single-user latency (chat, local inference, coding assistants) and gets disappointing on a saturated throughput server.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should I actually do?
&lt;/h2&gt;

&lt;p&gt;A checklist, in the order I'd do it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Find out which acceptance metric you have.&lt;/strong&gt; Convert accepted/drafted to per-token &lt;code&gt;α&lt;/code&gt;, or use mean acceptance length directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Measure &lt;code&gt;c&lt;/code&gt;.&lt;/strong&gt; Time a draft step versus a target step on your GPU at your typical batch size. Plug both into &lt;code&gt;speedup()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pick &lt;code&gt;k&lt;/code&gt; from the table, not from vibes.&lt;/strong&gt; For &lt;code&gt;α&lt;/code&gt; in the 0.6 to 0.75 range, start at 3 or 4.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gate speculation on load.&lt;/strong&gt; vLLM has exposed a disable-by-batch-size option in its speculative config; check the name in your version. Turn speculation off when concurrency crosses the point where your measured speedup falls below 1.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Match the drafter to the workload.&lt;/strong&gt; Acceptance drops at higher temperature and on open-ended creative text. For RAG answers and code edits that copy from the prompt, n-gram or prompt-lookup drafting has a draft cost near zero, which changes the math in your favor.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't evaluate quality.&lt;/strong&gt; It's lossless. Evaluate latency at p50 and p99 under realistic concurrency.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  So why does 70% acceptance only get you about 2x?
&lt;/h2&gt;

&lt;p&gt;Because a 70% per-token speculative decoding acceptance rate decays along the draft chain: with 4 draft tokens, you get about 2.77 tokens per target pass, and once you pay roughly 10% of a target step for each draft step, the ceiling is about 1.98x. If your dashboard shows 70% accepted/drafted, your real per-token rate is closer to 0.86 and you have more headroom. Either way, the speedup only exists while decoding is memory-bound, so at high batch sizes the spare compute disappears and speculative decoding can make your server slower. Measure the right acceptance metric, keep &lt;code&gt;k&lt;/code&gt; small, and turn speculation off under load.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by the developer behind &lt;a href="https://preterview.com/en" rel="noopener noreferrer"&gt;Preterview&lt;/a&gt;, an interview prep platform.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>llm</category>
      <category>machinelearning</category>
      <category>performance</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
