<?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: aiunplugged</title>
    <description>The latest articles on DEV Community by aiunplugged (@aiunplugged).</description>
    <link>https://dev.to/aiunplugged</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%2F4121134%2Fa133fa15-2287-4bbc-872a-1a0fabed3040.png</url>
      <title>DEV Community: aiunplugged</title>
      <link>https://dev.to/aiunplugged</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aiunplugged"/>
    <language>en</language>
    <item>
      <title>Prompt caching — the LLM discount most teams aren't using</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Sat, 21 Nov 2026 16:27:39 +0000</pubDate>
      <link>https://dev.to/aiunplugged/prompt-caching-the-llm-discount-most-teams-arent-using-49k2</link>
      <guid>https://dev.to/aiunplugged/prompt-caching-the-llm-discount-most-teams-arent-using-49k2</guid>
      <description>&lt;p&gt;Most LLM applications pay full input price on the same prefix tokens, request after request. A 20,000-token system prompt sent 100 times a day is 2 million tokens billed at the standard input rate — most of them repeat. Prompt caching is the API-level feature that stops the billing clock on those repeats. On the current pricing of every major provider, cached-prefix tokens cost between 5% and 50% of standard input tokens, which translates to 50-95% off the input bill for workloads with long stable prefixes.&lt;/p&gt;

&lt;p&gt;By late 2026, every major hosted LLM supports prompt caching in some form. Teams that haven't turned it on are paying 2-20x more per request than teams that have. This article covers what caching is, how each provider implements it, when it helps and when it doesn't, with the current (late 2026) pricing shape.&lt;/p&gt;

&lt;h2&gt;
  
  
  What prompt caching actually is
&lt;/h2&gt;

&lt;p&gt;An LLM processes a prompt token-by-token, building up an internal representation (the KV cache) as it goes. For a stable prefix — a long system prompt, a document pasted in before a question, a tool-schema preamble — that internal state is identical every time the prefix is sent. Recomputing it from scratch on every request wastes GPU time at the provider's side and bills the user for tokens the provider didn't really have to process.&lt;/p&gt;

&lt;p&gt;Prompt caching exposes that reuse. The provider stores the KV cache for recent long prefixes, and when the next request arrives with the same prefix, it skips the recompute. The savings get passed to the user as a steep discount on cached-input tokens, and the request also runs faster because the first N tokens of inference are already done.&lt;/p&gt;

&lt;p&gt;Two things determine whether caching helps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prefix stability&lt;/strong&gt; — the first K tokens of every request must be byte-identical. One changed character at position 50 invalidates everything cached after it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reuse frequency&lt;/strong&gt; — the prefix must be sent again before the cache expires. All providers have TTLs; some are 5 minutes, some are 24 hours.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything else is implementation detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pricing shape — late 2026
&lt;/h2&gt;

&lt;p&gt;Rough shape across the four major providers. Verify current numbers on each provider's live pricing page before budgeting; this landscape moves monthly.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Provider&lt;/th&gt;
&lt;th&gt;Cached-input discount&lt;/th&gt;
&lt;th&gt;Cache-write surcharge&lt;/th&gt;
&lt;th&gt;Minimum cacheable prefix&lt;/th&gt;
&lt;th&gt;Default TTL&lt;/th&gt;
&lt;th&gt;Model/mode&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Anthropic&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;90% off (0.1× base) on most models; 95% off (0.05×) on Opus 5.5 and Sonnet 5.5&lt;/td&gt;
&lt;td&gt;+25% on first write (1.25× base) for 5-min TTL; +100% (2× base) for 1-hour TTL&lt;/td&gt;
&lt;td&gt;1,024 tokens (varies by model)&lt;/td&gt;
&lt;td&gt;5 minutes (default), 1 hour (opt-in)&lt;/td&gt;
&lt;td&gt;Explicit &lt;code&gt;cache_control&lt;/code&gt; field&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;OpenAI&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;~90% off cached input (0.10× base) across current GPT-5.6 / GPT-6 families; 50% off on older GPT-4-era models&lt;/td&gt;
&lt;td&gt;None on current models (cache writes billed at uncached-input rate)&lt;/td&gt;
&lt;td&gt;1,024 tokens&lt;/td&gt;
&lt;td&gt;~30 minutes typical; up to 24 hours on extended-retention models&lt;/td&gt;
&lt;td&gt;Automatic + optional explicit breakpoints on newest models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Google Gemini&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;90% off cached input (Gemini 2.5+); 75% off (Gemini 2.0)&lt;/td&gt;
&lt;td&gt;Cache creation billed at standard input rate; &lt;strong&gt;plus a per-token-hour storage charge&lt;/strong&gt; on explicit caches&lt;/td&gt;
&lt;td&gt;2,048 tokens (Gemini 2.5), 4,096 tokens (Gemini 3.x), varies&lt;/td&gt;
&lt;td&gt;Implicit: automatic; explicit: user-chosen TTL with storage cost&lt;/td&gt;
&lt;td&gt;Implicit (automatic) + explicit API&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;DeepSeek&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;~90% off cached input&lt;/td&gt;
&lt;td&gt;None (automatic)&lt;/td&gt;
&lt;td&gt;Varies by model&lt;/td&gt;
&lt;td&gt;Hours (disk-based)&lt;/td&gt;
&lt;td&gt;Automatic&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Three practical takeaways from the table:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;OpenAI and DeepSeek are "turn it on by default"&lt;/strong&gt; — caching happens automatically, no code change required beyond keeping the prefix stable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anthropic requires an explicit &lt;code&gt;cache_control&lt;/code&gt; marker&lt;/strong&gt; — more work, but gives finer control over which parts of the prompt to cache and for how long.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gemini's explicit caching is unique in charging storage&lt;/strong&gt; — the cache is cheap to read, but you pay by the token-hour to keep it warm. Only worth it when reuse is high enough that storage pays for itself.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  When caching helps most
&lt;/h2&gt;

&lt;p&gt;Three workload shapes see the biggest gains:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Long system prompts reused across users.&lt;/strong&gt; A chatbot with a 10,000-token system prompt covering tone, policies, allowed tools, and example interactions. Every user turn sends that 10K prefix again. With Anthropic 5-minute caching, after the first request each subsequent request within 5 minutes pays 10% of standard for those 10K tokens — a 90% discount on the dominant input line.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document Q&amp;amp;A with the same document.&lt;/strong&gt; User uploads a 50-page PDF, then asks 20 follow-up questions over the next 10 minutes. Without caching: 20 × 50-page prefix = full price. With caching: 1 full-price request + 19 cached reads = ~95% off the document portion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code assistants over the same codebase.&lt;/strong&gt; Cursor, Claude Code, and similar tools re-send large chunks of the codebase on every turn as context. The codebase changes slowly within a session, so cache hit rates can be high. Both Anthropic's and OpenAI's APIs are built with this workload specifically in mind.&lt;/p&gt;

&lt;p&gt;Three workload shapes see little or no gain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One-shot single-turn requests&lt;/strong&gt; where no two requests share a prefix. There is nothing to cache.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Highly variable prompts&lt;/strong&gt; where the first 1000 tokens change on every request. Caching requires byte-identical prefixes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Short prompts&lt;/strong&gt; below the provider's minimum cacheable length. The 1,024-token floor cuts out most quick-chat use cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Anthropic — explicit &lt;code&gt;cache_control&lt;/code&gt; markers
&lt;/h2&gt;

&lt;p&gt;Anthropic's SDK requires an explicit marker on each content block the caller wants cached. Everything up to and including that marker becomes the cache boundary.&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;anthropic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&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="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;LONG_SYSTEM_PROMPT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"""\
You are an assistant for a legal document review platform.
Answer questions only using the attached contract.
Cite clause numbers.
...
(10,000 more tokens of policy, examples, allowed-tools, formatting rules)
"""&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;response&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="s2"&gt;"claude-sonnet-5-5"&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;1024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;system&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;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;LONG_SYSTEM_PROMPT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"cache_control"&lt;/span&gt;&lt;span class="p"&gt;:&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="s2"&gt;"ephemeral"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;# default 5-minute TTL&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;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="n"&gt;question&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;response&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first call writes the cache at 1.25× standard input price for the system prompt tokens. Every subsequent call within 5 minutes reads from the cache at 0.1× (or 0.05× on Sonnet 5.5 and Opus 5.5). For a 10,000-token prompt called 20 times in 5 minutes, that's roughly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Without caching: 20 × 10,000 = 200,000 input tokens at base price&lt;/li&gt;
&lt;li&gt;With caching: 1 × 10,000 × 1.25 + 19 × 10,000 × 0.1 = 12,500 + 19,000 = 31,500 tokens-equivalent — about &lt;strong&gt;85% off&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a 1-hour TTL (useful when reuse is sparser), add &lt;code&gt;"ttl": "1h"&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="n"&gt;cache_control&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;"ephemeral"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ttl"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"1h"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 1-hour cache costs 2× on write but still 0.1× on read, so it pays off after two reads instead of one. The 1-hour TTL became generally available in August 2025 and no longer requires a beta header.&lt;/p&gt;

&lt;h2&gt;
  
  
  OpenAI — automatic caching, optional breakpoints
&lt;/h2&gt;

&lt;p&gt;OpenAI's caching requires zero code change on current models. Any prompt prefix at least 1,024 tokens long that gets re-sent within the cache window (typically 30 minutes on GPT-5.6 and later; up to 24 hours on extended-retention models) gets charged at the cached-input rate automatically.&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;openai&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;

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

&lt;span class="no"&gt;LONG_SYSTEM_PROMPT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"..."&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;  &lt;span class="c1"&gt;# 10,000+ tokens&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;response&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;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&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="s2"&gt;"gpt-5.6-terra"&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;"system"&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;LONG_SYSTEM_PROMPT&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="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="n"&gt;question&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="c1"&gt;# Cache hit telemetry&lt;/span&gt;
    &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;input_tokens_details&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cached_tokens&lt;/span&gt;
    &lt;span class="k"&gt;print&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;"Cached tokens hit: {cached}/{response.usage.prompt_tokens}"&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&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;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things to notice:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Nothing in the request body signals caching.&lt;/strong&gt; OpenAI handles routing automatically. The only observable effect is the &lt;code&gt;cached_tokens&lt;/code&gt; field on &lt;code&gt;usage.input_tokens_details&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observability is on the response, not the request.&lt;/strong&gt; Always log &lt;code&gt;cached_tokens / prompt_tokens&lt;/code&gt; to catch cache misses early — a small change in the system prompt (a date rotated in, a dynamic value inserted at position 50) will silently drop the hit rate without any error.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On GPT-6 family models, developers can also set explicit cache breakpoints to force the cache boundary where they want it, in addition to the automatic behaviour. Useful for prompts where the stable prefix ends mid-paragraph and automatic detection might cache less than the full stable portion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Google Gemini — implicit (free) and explicit (storage-billed)
&lt;/h2&gt;

&lt;p&gt;Gemini supports two caching modes:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implicit caching&lt;/strong&gt; — automatic, no code change, 90% discount on hits (on Gemini 2.5 and 3.x models), no storage fee. The system detects repeated prefixes and routes to the cached version transparently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explicit caching&lt;/strong&gt; — the developer creates a named cache object, which stores the prefix on Google's side for a chosen TTL. Reads are 90% off standard input, but there is a per-token-hour storage charge while the cache is alive.&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;google&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;genai&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;types&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;genai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create an explicit cache from a 50-page PDF uploaded earlier&lt;/span&gt;
&lt;span class="n"&gt;cache&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;caches&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="s2"&gt;"models/gemini-3.1-pro-preview"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateCachedContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;display_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"legal-contract-123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;system_instruction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"You are a legal reviewer. Answer only from the attached contract."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;my_uploaded_pdf&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"3600s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# 1 hour; billed for each hour of storage&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="c1"&gt;# Use the cache in later requests&lt;/span&gt;
&lt;span class="n"&gt;response&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;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content&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="s2"&gt;"models/gemini-3.1-pro-preview"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"What is the termination clause?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cached_content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&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;The storage cost matters. As of October 2026, Flash-tier models bill around $1.00 per million tokens per hour (with Gemini 3.8 Flash discounted to $0.50 through December 31, 2026, then rising to $1.00 on January 1, 2027), and Pro-tier (both Gemini 2.5 Pro and 3.1 Pro Preview) is $4.50 per million tokens per hour. For a 100,000-token cached PDF held for a 1-hour conversation on Flash, storage adds $0.05-$0.10 — negligible relative to the ~90% savings on reads if there are more than one or two queries. For a 1-million-token cached corpus held for 24 hours on Gemini Pro, storage adds ~$108 — only worth it with substantial reuse. Verify current rates on Google's pricing page before planning.&lt;/p&gt;

&lt;p&gt;Implicit caching sidesteps the storage charge entirely. The trade-off: no guarantee the cache will be there when needed, since the system decides what to retain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common gotchas
&lt;/h2&gt;

&lt;p&gt;Six patterns that silently kill cache hit rates:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A timestamp or current date rendered into the system prompt.&lt;/strong&gt; &lt;code&gt;"Today is 2026-11-24"&lt;/code&gt; means every request has a unique prefix. Move the dynamic data into the user message, or round the date to the day/hour and accept slight staleness.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Streaming responses that join concatenated chat history differently each turn.&lt;/strong&gt; The join logic has to be byte-identical across calls. Different whitespace in message separators invalidates the cache.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tool/function definitions changing order between calls.&lt;/strong&gt; Even if the tools themselves are the same, re-ordering their position in the request changes the prefix bytes. Sort tool lists deterministically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A/B tests or feature flags that rotate parts of the prompt.&lt;/strong&gt; Each flag variant has its own cache, and total hit rate drops. Keep the flag variants above the cache boundary, not inside it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A userId or sessionId interpolated into the system prompt.&lt;/strong&gt; Every user has their own cache, hit rates stay poor across users. Move user-specific data to the user message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Model upgrades without re-planning cache budgets.&lt;/strong&gt; Each model has its own cache namespace. The day a service switches from Sonnet 5 to Sonnet 5.5, every cache resets.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  When caching doesn't help, and what to do instead
&lt;/h2&gt;

&lt;p&gt;If the workload has none of the three "helps most" shapes above, caching may not be the biggest cost lever. Three alternatives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shrink the prompt.&lt;/strong&gt; Most 10,000-token system prompts have 3,000 tokens of dead text that could be removed with no quality loss. Shrinking input beats caching 90% of input for one-shot workloads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Downgrade the model.&lt;/strong&gt; For retrieval-heavy or classification-style queries, Haiku-tier models at 1/15th the price of Opus-tier produce acceptable quality. Caching doesn't close that gap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Switch to batch API.&lt;/strong&gt; For non-latency-sensitive workloads, OpenAI's and Anthropic's batch endpoints halve the input price for processing within 24 hours. Caching + batch stacks for even bigger savings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Caching is one tool in the cost toolbox. Teams that treat it as the only lever often leave bigger savings elsewhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;Prompt caching is a near-free optimisation for any LLM workload with a stable prefix over 1,024 tokens that gets reused within minutes. On the current pricing of every major provider, it cuts input cost 50-95% for the cached portion. The engineering cost is roughly one hour: identify the stable prefix, add the right marker (or just let auto-caching work), log the hit rate, and move on.&lt;/p&gt;

&lt;p&gt;For teams that haven't turned it on, this is almost certainly the highest-ROI cost cut sitting in their LLM bill today. For teams that have turned it on but see poor hit rates, the six gotchas above cover 90% of the real-world misses.&lt;/p&gt;

&lt;p&gt;Verify current pricing on each provider's live docs — the specific percentages above are accurate as of late 2026 but the LLM pricing landscape moves monthly.&lt;/p&gt;

&lt;p&gt;Related reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://aiunplugged.in/blog/claude-api-vs-openai-api-developers/" rel="noopener noreferrer"&gt;Claude API vs OpenAI API — developer perspective&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aiunplugged.in/blog/mcp-model-context-protocol-explained/" rel="noopener noreferrer"&gt;MCP explained — how LLM apps talk to tools in 2026&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;Build a RAG pipeline from scratch&lt;/a&gt; — one workload where caching matters a lot&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
    <item>
      <title>MCP (Model Context Protocol) — the standard that connects every LLM app to tools in 2026</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Tue, 17 Nov 2026 19:49:56 +0000</pubDate>
      <link>https://dev.to/aiunplugged/mcp-model-context-protocol-the-standard-that-connects-every-llm-app-to-tools-in-2026-63f</link>
      <guid>https://dev.to/aiunplugged/mcp-model-context-protocol-the-standard-that-connects-every-llm-app-to-tools-in-2026-63f</guid>
      <description>&lt;p&gt;By late 2026, Model Context Protocol (MCP) is the standard every AI coding assistant, chat app, and agent framework has settled on for one problem: how an LLM application connects to the data and tools that live outside it. Claude Desktop, Cursor, Zed, VS Code, and most agent frameworks ship MCP support natively. If an engineering team is adding tool integrations to anything LLM-powered in 2026, MCP is the first thing on the shortlist — and often the only thing.&lt;/p&gt;

&lt;p&gt;This article covers what MCP actually is, the three roles that make it work, the two transports it uses, when to pick it over a direct integration, and the misconceptions that trip teams up on first adoption.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem MCP solves
&lt;/h2&gt;

&lt;p&gt;Before MCP (released by Anthropic in late 2024), every LLM app that needed external data or tool access invented its own integration layer. Claude Desktop had its own plugin shape. OpenAI's ChatGPT had GPTs with their own OpenAPI-flavoured spec. Cursor had its own tool configuration. Internal company AI apps built one-off clients against whatever API they needed.&lt;/p&gt;

&lt;p&gt;The result: a tool that wanted to be reachable from three different LLM apps got built three times. A team wanting to expose their internal docs to an AI assistant wrote a custom integration against each AI assistant they used. The ecosystem was fragmented in the exact way the web was before HTTP standardised client-server communication.&lt;/p&gt;

&lt;p&gt;MCP standardises that integration layer. One MCP server exposes capabilities once; any MCP-aware host application can connect to it. The analogy that gets used most often is "USB-C for AI": one plug shape, many devices.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three roles
&lt;/h2&gt;

&lt;p&gt;MCP has a strict three-role architecture. Getting these right in the mental model avoids most beginner confusion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Host&lt;/strong&gt; — the AI application the user interacts with. Claude Desktop, Cursor, Zed, VS Code with an MCP-aware extension. The host runs the LLM (or calls it over an API) and decides when the LLM should use an external tool or read external data. One host, zero or many clients inside it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client&lt;/strong&gt; — a connector that lives inside the host and manages one connection to one server. If a host is connected to five servers, it runs five client instances internally. The client handles the protocol-level details (JSON-RPC messages, transport, capability negotiation) so neither the host nor the server has to care about the mechanics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server&lt;/strong&gt; — a process that exposes capabilities. A filesystem server exposes "read this file" and "list this directory." A GitHub server exposes "search issues" and "create pull request." A company's internal docs server exposes "search the handbook" and "fetch the policy for X." Servers can run as a local subprocess on the user's machine or as a remote HTTP service.&lt;/p&gt;

&lt;p&gt;The data flow: user asks the host something, host decides which server(s) to involve, host's client talks to the server, server returns results, host feeds results back into the LLM prompt, LLM composes the answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three server primitives
&lt;/h2&gt;

&lt;p&gt;A server exposes capabilities as three kinds of thing:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tools&lt;/strong&gt; — functions the LLM can call to perform an action or compute a value. A &lt;code&gt;search_web(query)&lt;/code&gt; tool. A &lt;code&gt;send_email(to, subject, body)&lt;/code&gt; tool. A &lt;code&gt;run_sql(query)&lt;/code&gt; tool. Tools are the primitive that most people think of when they hear "MCP."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt; — read-only data the host can fetch and surface to the LLM as context. A specific file's contents. A database schema. A document from a knowledge base. Resources are addressed by URI and are passive — the host decides when to read them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prompts&lt;/strong&gt; — reusable prompt templates the host can offer to the user as pre-made actions. "Summarise this document." "Refactor this code." "Explain this error." Prompts are the primitive that lets servers suggest workflows, not just raw capabilities.&lt;/p&gt;

&lt;p&gt;Most servers in practice expose mostly tools, some resources, and few or no prompts. The three are complementary; a filesystem server might expose a &lt;code&gt;read_file&lt;/code&gt; tool AND a &lt;code&gt;file://&lt;/code&gt; resource type, with the resource being the way the host pre-loads a file and the tool being the way the LLM reads a file during a conversation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The two transports
&lt;/h2&gt;

&lt;p&gt;The MCP spec is transport-agnostic — any bidirectional message channel will do — but in practice two transports cover every real deployment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;stdio&lt;/strong&gt; — the server runs as a local subprocess, communicating with the client over standard input and output. Zero network, zero authentication, lowest friction. The right choice for local servers that access the user's own machine (filesystem, local git, local database). The host launches the server when needed and shuts it down when the host closes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Streamable HTTP&lt;/strong&gt; — the server runs as a remote HTTP service. The client sends messages via HTTP POST, optionally receives streaming responses via Server-Sent Events. Supports standard HTTP auth including bearer tokens, API keys, and OAuth. The right choice for servers that expose cloud data or company-wide infrastructure where many users share one server instance.&lt;/p&gt;

&lt;p&gt;An earlier HTTP+SSE transport existed in older spec revisions and was replaced by Streamable HTTP in the 2025-03-26 spec revision. New servers should use Streamable HTTP; older servers may still ship HTTP+SSE and work with older clients that haven't upgraded.&lt;/p&gt;

&lt;h2&gt;
  
  
  Underneath, it's just JSON-RPC
&lt;/h2&gt;

&lt;p&gt;MCP's wire protocol is JSON-RPC 2.0 on top of the chosen transport. Every message is a JSON object with &lt;code&gt;jsonrpc&lt;/code&gt;, &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;method&lt;/code&gt;, and &lt;code&gt;params&lt;/code&gt; (or &lt;code&gt;result&lt;/code&gt; / &lt;code&gt;error&lt;/code&gt; for responses). Reading the raw messages during debugging is useful; the official MCP Inspector prints them in a readable form.&lt;/p&gt;

&lt;p&gt;The implication: an MCP server is not a special kind of process — it's a program that reads JSON-RPC messages on stdin (or an HTTP endpoint) and writes JSON-RPC responses. Any language with a JSON parser can implement one; official SDKs ship in Python, TypeScript, Java/Kotlin, Swift, C#, with community implementations in Rust and others.&lt;/p&gt;

&lt;h2&gt;
  
  
  A minimal Python server
&lt;/h2&gt;

&lt;p&gt;The Python SDK ships at &lt;code&gt;pip install mcp&lt;/code&gt;. The exact import path can shift between major versions — always start from the current quickstart on modelcontextprotocol.io before copying older examples. The pattern that covers 90% of real servers (using the stable &lt;code&gt;FastMCP&lt;/code&gt; class):&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;# server.py&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;mcp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fastmcp&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastMCP&lt;/span&gt;

&lt;span class="n"&gt;mcp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastMCP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"my-first-server"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nd"&gt;@mcp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tool&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;add&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="kt"&gt;int&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="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;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Return the sum of two integers."""&lt;/span&gt;
    &lt;span class="k"&gt;return&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;b&lt;/span&gt;


&lt;span class="nd"&gt;@mcp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"greeting://{name}"&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;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Return a friendly greeting for the given name."""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Hello, {name}!"&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Default transport is stdio — the server waits on stdin for client messages.&lt;/span&gt;
    &lt;span class="n"&gt;mcp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&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;Three things to notice:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The decorators are the whole registration step.&lt;/strong&gt; &lt;code&gt;@mcp.tool()&lt;/code&gt; and &lt;code&gt;@mcp.resource()&lt;/code&gt; are all that's needed to make a function or resource template visible to clients. No manifest file, no JSON schema to hand-write — the SDK generates both from the Python signature + docstring.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type hints and docstrings matter.&lt;/strong&gt; The LLM sees them. A tool named &lt;code&gt;fetch&lt;/code&gt; with the docstring "returns stuff" is useless; the same function named &lt;code&gt;fetch_user_profile&lt;/code&gt; with "Returns the user's name, email, and plan tier given their user_id" is useful.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;stdio by default.&lt;/strong&gt; &lt;code&gt;mcp.run()&lt;/code&gt; with no arguments starts a stdio server. For Streamable HTTP, pass &lt;code&gt;transport="streamable-http"&lt;/code&gt; or use the &lt;code&gt;uv run mcp run server.py --transport streamable-http&lt;/code&gt; CLI.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Test the server locally with the official Inspector:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;uv run mcp dev server.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Inspector opens in a browser and lets you call tools and read resources without needing a full LLM host. First debugging stop whenever a tool isn't working as expected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Version pinning note:&lt;/strong&gt; the Python SDK has moved classes and import paths between major revisions. If adopting an older codebase, pin the &lt;code&gt;mcp&lt;/code&gt; package version that matches the imports the codebase uses (&lt;code&gt;pip show mcp&lt;/code&gt; to see the current version), and consult the SDK changelog before upgrading. New code should start from the current quickstart example on modelcontextprotocol.io.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting to a host
&lt;/h2&gt;

&lt;p&gt;Once a server is running, a host needs to be told about it. For Claude Desktop and most other local hosts, this is a JSON config file that lists each server by name, the command to run it, and any environment variables. Rough shape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"my-first-server"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"python"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/absolute/path/to/server.py"&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;The exact file path and schema vary by host (Claude Desktop's lives in a per-OS location, Cursor's lives inside its workspace settings, VS Code has an extension-specific config). The MCP spec defines the server; the host defines where it looks for servers.&lt;/p&gt;

&lt;p&gt;For remote (Streamable HTTP) servers, the config usually points to a URL and specifies auth headers instead of a command.&lt;/p&gt;

&lt;h2&gt;
  
  
  When MCP is the right pick
&lt;/h2&gt;

&lt;p&gt;MCP is the right pick when any of these are true:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The server exposes capabilities that more than one AI app should be able to use. Writing it once as an MCP server is cheaper than writing it as three different host-specific plugins.&lt;/li&gt;
&lt;li&gt;The capability is something a user installs on their own machine (filesystem, local git, personal notes). stdio keeps everything local and permissions clean.&lt;/li&gt;
&lt;li&gt;The server will be reused across teams or published to the ecosystem. An MCP server with a clear name has instant installability for anyone who runs an MCP-aware host.&lt;/li&gt;
&lt;li&gt;The capability needs to work from inside an agent framework (LangChain, LlamaIndex, custom), not just a chat UI. Most agent frameworks now consume MCP servers natively.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MCP is the wrong pick when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The capability is a one-off integration for exactly one in-house app that will never be reused. The MCP overhead is wasted.&lt;/li&gt;
&lt;li&gt;The capability needs sub-millisecond latency or has to run in-process with the LLM. MCP adds a JSON-RPC round-trip; direct function calls are faster for hot paths.&lt;/li&gt;
&lt;li&gt;The LLM app does not run tools at the user's request but as part of an agent loop where the host already has native tool support. For some setups, an in-process tool definition is still simpler than an external server.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Three misconceptions worth clearing
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;"MCP is a replacement for LangChain / LlamaIndex."&lt;/strong&gt; No. MCP is the layer below. LangChain still composes LLM calls, memory, and chains; now one of the ways it can gain tools is by consuming an MCP server, rather than requiring custom LangChain tool classes. The two complement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"MCP is Anthropic-only."&lt;/strong&gt; No. Anthropic open-sourced the spec and the ecosystem has clients and servers for multiple LLM vendors. The protocol doesn't care which model the host is running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"MCP is just OpenAPI for LLMs."&lt;/strong&gt; Related but distinct. OpenAPI is a REST contract description language. MCP is a bidirectional protocol with a stateful connection, with tool / resource / prompt primitives, specifically shaped for LLMs to use. An MCP server can expose an OpenAPI-described service — but the MCP layer is doing more than describing a REST surface.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's current and what's shifting
&lt;/h2&gt;

&lt;p&gt;The MCP spec continues to iterate on a quarterly rhythm. The core architecture (host/client/server, three primitives, two transports, JSON-RPC under the hood) has been stable since 2025. Primitive additions, authorisation hardening, and transport improvements land in each new spec revision — some older primitives also move toward deprecation as the spec consolidates. Before building against any specific primitive, verify its current status on the official changelog at &lt;code&gt;modelcontextprotocol.io&lt;/code&gt;; article-tier summaries of a fast-moving spec decay in weeks.&lt;/p&gt;

&lt;p&gt;Reference pages worth bookmarking: the official docs at &lt;code&gt;modelcontextprotocol.io&lt;/code&gt;, the Python SDK repository on GitHub (&lt;code&gt;modelcontextprotocol/python-sdk&lt;/code&gt;), and the server registry (community-maintained lists of published servers for popular services).&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;MCP solved a fragmentation problem that cost real engineering time in 2023 and 2024. By late 2026 it's close to table stakes for any serious LLM-powered application that connects to external systems. The learning curve to publish a basic server is roughly an hour. The pay-off is that any MCP-aware host can use the server with no further integration work — including hosts that don't exist yet.&lt;/p&gt;

&lt;p&gt;For teams shipping anything beyond pure prompt-in-answer-out LLM UX, MCP is now the default integration layer. The alternative is still writing per-host adapters forever.&lt;/p&gt;

&lt;p&gt;Related reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;Build a RAG pipeline from scratch&lt;/a&gt; — the retrieval pattern MCP servers often expose&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/claude-api-vs-openai-api-developers/" rel="noopener noreferrer"&gt;Claude API vs OpenAI API — developer perspective&lt;/a&gt; — API-level comparison&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/cursor-vs-windsurf-vs-claude-code/" rel="noopener noreferrer"&gt;Cursor vs Windsurf vs Claude Code&lt;/a&gt; — three major MCP-aware code editors in 2026&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>mcp</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Context engineering — what replaced prompt engineering</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Sat, 14 Nov 2026 19:46:29 +0000</pubDate>
      <link>https://dev.to/aiunplugged/context-engineering-what-replaced-prompt-engineering-i10</link>
      <guid>https://dev.to/aiunplugged/context-engineering-what-replaced-prompt-engineering-i10</guid>
      <description>&lt;p&gt;"Prompt engineering" peaked as a term in 2023, got mocked through 2024, and by mid-2025 was being quietly replaced in serious engineering conversations with a broader label: &lt;strong&gt;context engineering&lt;/strong&gt;. The shift is not just rebranding. The actual work of making an LLM reliably produce the right output turned out to be mostly about what information ends up in the context window — not the one magical system prompt line a prompt-engineering blog promised.&lt;/p&gt;

&lt;p&gt;This article covers what context engineering actually is as a discipline in late 2026, the four strategies that cover most agent workloads, and the parts where the "new discipline" framing is still mostly marketing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the term came from
&lt;/h2&gt;

&lt;p&gt;The specific label took off in June 2025. Tobi Lütke (CEO of Shopify) framed it on X as &lt;em&gt;"the art of providing all the context for the task to be plausibly solvable by the LLM."&lt;/em&gt; Andrej Karpathy amplified it roughly a week later, calling context engineering a &lt;em&gt;"delicate art and science of filling the context window with the right information for the next step"&lt;/em&gt; and arguing explicitly that it should replace "prompt engineering" as the operative term for serious LLM work.&lt;/p&gt;

&lt;p&gt;The point both were making: when a production LLM app fails, the fix is almost never a better prompt sentence. It is usually about what information was or was not placed in the context — the retrieved documents, the tool outputs, the conversation history, the system instructions, the examples, the user's intent. Prompt engineering treats the prompt as the unit of work; context engineering treats the entire context assembly as the unit of work.&lt;/p&gt;

&lt;h2&gt;
  
  
  What context actually contains
&lt;/h2&gt;

&lt;p&gt;A prompt sent to an LLM is only one visible layer of a larger construct. The full context, in a typical production app, contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;System instructions&lt;/strong&gt; — the role, constraints, tone, and output format directives.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retrieved information&lt;/strong&gt; — chunks from a vector database (RAG), rows from a SQL query, API responses from a tool call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conversation history&lt;/strong&gt; — the running record of previous user and assistant messages in a multi-turn session.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Working memory&lt;/strong&gt; — intermediate conclusions the agent has reached in the current loop that are not yet in the user-visible transcript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tool definitions&lt;/strong&gt; — the schemas of callable functions the LLM has access to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Examples&lt;/strong&gt; — few-shot demonstrations of the desired input-output pattern.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The current user turn&lt;/strong&gt; — the actual message that triggered this call.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each layer has a cost (tokens) and a benefit (better grounding). Context engineering is the discipline of deciding what goes in each layer, in what order, in what format, and when to drop or compress something to make room for something more important.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four strategies for context (agent-focused)
&lt;/h2&gt;

&lt;p&gt;For agent workloads specifically, four core strategies cover roughly 90% of production context engineering work:&lt;/p&gt;

&lt;h3&gt;
  
  
  Write
&lt;/h3&gt;

&lt;p&gt;Store context OUTSIDE the LLM's context window so it survives across turns and sessions. Options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scratch pads&lt;/strong&gt; — write intermediate reasoning to a text file or database, read it back on later turns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory stores&lt;/strong&gt; — write summaries of past conversations to a vector database, retrieve relevant ones on new turns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Checkpoints&lt;/strong&gt; — persist agent state (LangGraph's pattern) so a crashed workflow resumes without re-reading everything from scratch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The failure mode this avoids: trying to keep all context in the LLM's active window and running out of tokens 20 turns into a conversation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Select
&lt;/h3&gt;

&lt;p&gt;Pull in only the context relevant to the current step, not everything available. Options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Retrieval (RAG)&lt;/strong&gt; — embed the current question, select the top-K relevant chunks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tool filtering&lt;/strong&gt; — if an agent has 50 tools, show the LLM only the 5 that could plausibly apply to the current turn.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory filtering&lt;/strong&gt; — of 1,000 past conversation summaries, select the 3 most similar to the current query.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The failure mode this avoids: dumping all possible context on every call, paying token cost without quality gain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compress
&lt;/h3&gt;

&lt;p&gt;Reduce the token footprint of context already selected. Options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Summarisation&lt;/strong&gt; — replace a 10,000-token conversation history with a 500-token summary of what matters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extraction&lt;/strong&gt; — pull the specific fields relevant from a long tool response rather than including the full response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prompt caching&lt;/strong&gt; — mechanically distinct, but same spirit: pay less for the stable prefix. See the &lt;a href="https://aiunplugged.in/blog/prompt-caching-explained/" rel="noopener noreferrer"&gt;prompt caching article&lt;/a&gt; for the pricing side.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The failure mode this avoids: fitting but paying for giant context on every call, or spending latency processing information that was already extracted on a prior turn.&lt;/p&gt;

&lt;h3&gt;
  
  
  Isolate
&lt;/h3&gt;

&lt;p&gt;Split a complex task across multiple LLM calls or sub-agents, each with a focused context, rather than one call with everything. Options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Router + specialist pattern&lt;/strong&gt; — a lightweight model routes the query to one of several specialists; each specialist has a context focused on its domain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-agent decomposition&lt;/strong&gt; — one agent researches, another writes, another reviews. Each has only the context it needs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tool-use loops&lt;/strong&gt; — the LLM asks for data, gets it, incorporates it, continues. Each sub-turn has focused context.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The failure mode this avoids: trying to fit "research sources + draft content + style guide + reviewer feedback" into one prompt and watching quality collapse under cognitive load.&lt;/p&gt;

&lt;h2&gt;
  
  
  When context engineering is overkill
&lt;/h2&gt;

&lt;p&gt;Not every LLM workload needs this framing. For:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pure one-shot classification&lt;/strong&gt; — map an input to a label. Prompt + examples. No agent loop, no memory. Prompt engineering is fine as a term.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Short single-turn Q&amp;amp;A over a small corpus&lt;/strong&gt; — fits in a prompt, no retrieval needed. Prompt engineering is fine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Templated generation with fixed structure&lt;/strong&gt; — invoice copy, standard emails. Prompt plus variable interpolation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Context engineering becomes the right frame when any of these conditions hit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The task is multi-turn with state that must persist across turns.&lt;/li&gt;
&lt;li&gt;The information needed to answer is too large to fit entirely in the prompt.&lt;/li&gt;
&lt;li&gt;The system has tools, and which tool to show the LLM depends on the turn.&lt;/li&gt;
&lt;li&gt;The LLM is calling itself or another agent in a loop.&lt;/li&gt;
&lt;li&gt;Quality is driven more by what-information-is-present than by how-the-question-is-worded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If none of those apply, calling the work "context engineering" is a status upgrade on prompt engineering with no new content behind it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The practitioner workflow
&lt;/h2&gt;

&lt;p&gt;For a real agent system in late 2026, the context engineering loop looks like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Enumerate what COULD go in the context.&lt;/strong&gt; List every potential source: user turn, history, retrieval, tool outputs, memory, examples, system prompt.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decide what SHOULD go in for this specific step.&lt;/strong&gt; Not every source applies to every turn. A greeting message does not need retrieval; a technical question does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Set a token budget per source.&lt;/strong&gt; System: 500. Retrieval: 2000. History: 1000. Current turn: variable. Budgets prevent any one source from crowding out the others as the conversation grows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose a compression strategy for sources near their budget cap.&lt;/strong&gt; Full history → rolling summary + last 5 turns verbatim. Full tool output → extracted fields.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Order the context intentionally.&lt;/strong&gt; System instructions first, then retrieved information, then history, then current user turn. Order affects what the model attends to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log what was actually sent and what was dropped.&lt;/strong&gt; Instrument this before shipping. Debugging "why did the agent miss the last turn's context" is impossible without a log of what went in.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evaluate at the context level, not just output level.&lt;/strong&gt; Did the retrieved chunks actually contain the answer? Was the key tool definition included? These are context-side eval questions distinct from "was the final answer right."&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A system that does these seven things reliably is doing context engineering whether anyone on the team uses that label.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three failure modes to design around
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Context window overflow.&lt;/strong&gt; The conversation grows, context grows with it, eventually a single call exceeds the model's limit and the request fails outright. Fix: a hard budget on each source, with the "compress" strategies above kicking in when a budget is near.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The right information is present but buried.&lt;/strong&gt; The chunk with the answer is at position 15 in a 20-chunk retrieval, and the model does not attend to it strongly because models over-weight the top and bottom of long contexts (the "lost in the middle" effect, well-documented since 2023). Fix: tighter retrieval (fewer, better chunks) and reranking so the most relevant lands at the top.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Tool definitions outnumber tool-use capacity.&lt;/strong&gt; The LLM has 50 tool definitions in context but can only reasonably choose between a few at a time. Fix: route first (lightweight classifier chooses which 5 tools to expose), then let the model pick among those 5. See the &lt;a href="https://aiunplugged.in/blog/agent-frameworks-compared-2026/" rel="noopener noreferrer"&gt;agent frameworks article&lt;/a&gt; for how each framework handles routing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the term is still mostly marketing
&lt;/h2&gt;

&lt;p&gt;Three cases where "context engineering" is being used as rebrand with no new content:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A single prompt tweaked in a prompt-tuning tool.&lt;/strong&gt; Still prompt engineering. The tool renamed itself but the practice did not change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adding RAG to a chatbot without any new discipline around what-goes-in-context-when.&lt;/strong&gt; That is "we added RAG," not context engineering.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A conference talk titled "Context Engineering" with slides about system prompts.&lt;/strong&gt; The term was used to make 2023 content feel current.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The honest test: if the work genuinely involves deciding what information from multiple sources ends up in each LLM call, in what order, in what compression, with what budget — it is context engineering as a discipline. If not, calling it that is naming not practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;Context engineering is a real discipline, with four core strategies (write, select, compress, isolate) that cover most agent production work. The term is a reasonable upgrade from "prompt engineering" because the work genuinely shifted from "the sentence I write to the model" to "the whole bundle of information I assemble before each call."&lt;/p&gt;

&lt;p&gt;Teams building anything beyond single-turn Q&amp;amp;A should adopt the framing — not for the label, but for the mental model shift. Every production LLM failure is some form of "the model did not have the right context at the right moment." Fixing that systematically is what the discipline is for.&lt;/p&gt;




&lt;p&gt;If you're shipping a RAG or agent system to production, &lt;strong&gt;&lt;a href="https://aiunplugged.in/products/rag-production-handbook/" rel="noopener noreferrer"&gt;The RAG Production Handbook&lt;/a&gt;&lt;/strong&gt; goes deep on the context-engineering decisions that actually move quality numbers — chunking, retrieval, evaluation, observability, and the full 7-field production log schema. Early-bird pricing for the first 20 buyers.&lt;/p&gt;




&lt;p&gt;Related reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;Build a RAG pipeline from scratch&lt;/a&gt; — RAG is one way of selecting context&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/prompt-caching-explained/" rel="noopener noreferrer"&gt;Prompt caching — the LLM discount most teams aren't using&lt;/a&gt; — compression at the API-pricing layer&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/agent-frameworks-compared-2026/" rel="noopener noreferrer"&gt;Agent frameworks 2026 — LangGraph vs CrewAI vs AutoGen&lt;/a&gt; — each framework encodes different context-engineering opinions&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/why-rag-hallucinates-with-perfect-retrieval/" rel="noopener noreferrer"&gt;Why RAG hallucinates even with perfect retrieval&lt;/a&gt; — a context-engineering failure mode&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/mcp-model-context-protocol-explained/" rel="noopener noreferrer"&gt;MCP explained — how LLM apps talk to tools in 2026&lt;/a&gt; — tools are a context layer&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>rag</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Your RAG tutorial works on your laptop. The production version will break — here's what nobody teaches</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Mon, 09 Nov 2026 12:57:54 +0000</pubDate>
      <link>https://dev.to/aiunplugged/your-rag-tutorial-works-on-your-laptop-the-production-version-will-break-heres-what-nobody-58j</link>
      <guid>https://dev.to/aiunplugged/your-rag-tutorial-works-on-your-laptop-the-production-version-will-break-heres-what-nobody-58j</guid>
      <description>&lt;p&gt;Six months ago I shipped a RAG prototype that worked on my laptop. Two weeks later in production, it was returning stale answers, blowing through the LLM budget, and getting flagged for hallucinations the retrieval metrics said shouldn't be possible.&lt;/p&gt;

&lt;p&gt;Here's the gap between "RAG tutorial that works" and "RAG system in production" — because every tutorial stops at the first one.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Retrieval metrics lie in production
&lt;/h2&gt;

&lt;p&gt;Your test set is small, curated, and probably biased toward questions you already know have answers in the corpus. Production queries come in at a different angle: typos, missing context, questions the docs don't actually cover, questions that span multiple documents.&lt;/p&gt;

&lt;p&gt;Recall@5 on your test set doesn't predict production quality. What does: a 7-field query log (query, retrieved chunk IDs, generated answer, user action, latency, cost, timestamp) and the discipline to actually look at the bad rows weekly.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Chunking strategy quietly costs you 30-50% retrieval quality
&lt;/h2&gt;

&lt;p&gt;Most tutorials show fixed-size chunking because it's simplest. In production you need semantic chunking for narrative docs, structural chunking for code and markdown, parent-child retrieval for PDFs with tables, and a chunker version column so you can re-embed selectively instead of paying to re-embed the whole corpus every time you tweak the strategy.&lt;/p&gt;

&lt;p&gt;A small delta-reindex pattern (hash each chunk, only re-embed changed ones) cuts embedding cost 99% on small corpus edits.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Vector DB cost shape surprises everyone
&lt;/h2&gt;

&lt;p&gt;Pinecone's per-read-unit billing looks cheap at 100 queries/day. At 10,000 queries/day it crosses Qdrant Cloud's flat-rate. pgvector is free if you're already on Postgres — until you forget to create the HNSW index and every query becomes a sequential scan.&lt;/p&gt;

&lt;p&gt;The real decision isn't "which vendor is best." It's "what's my query volume in 6 months, and what's my ops capacity?"&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Evaluation is what separates "I shipped it" from "I can improve it"
&lt;/h2&gt;

&lt;p&gt;Without an eval harness, every prompt tweak is a vibe check. LLM-as-judge works if you pin the judge model, calibrate the rubric, and check inter-rater agreement (Wilcoxon for statistical significance, not just "looks better"). 50 queries in the test set is enough to get real signal if you built it with a 5-step human review protocol rather than scraping queries blind.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Observability on RAG is NOT the same as observability on APIs
&lt;/h2&gt;

&lt;p&gt;You don't want more alerts. You want three alerts: retrieval returned zero chunks, LLM cost per query crossed threshold, p95 latency broke SLO. Everything else goes on a Grafana dashboard you look at weekly, not in your pager rotation.&lt;/p&gt;

&lt;p&gt;The debug script that solves "the answer was wrong" complaints in 5 minutes takes the query, replays the retrieval, prints the chunks that came back, and shows which ones the LLM actually used. Without this, "the answer was wrong" takes an hour per incident.&lt;/p&gt;




&lt;p&gt;I spent six weeks compressing what I learned (plus three case studies from production RAG systems at different scales) into &lt;strong&gt;The RAG Production Handbook&lt;/strong&gt; — launching today.&lt;/p&gt;

&lt;p&gt;What's in it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;10 chapters, 5 appendices, ~95 pages&lt;/strong&gt; — chunking, vector DB selection, evaluation, deployment, cost modelling, observability, 3 real case studies, vendor matrix, migration recipes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Working code repo&lt;/strong&gt; — chunkers.py (all 10 strategies), retriever.py (vendor-agnostic abstraction), evaluator.py, logger.py, Dockerfile, deployment configs for Vercel/Fly.io/Kubernetes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vendor-agnostic&lt;/strong&gt; — patterns work with Claude, OpenAI, local models; pgvector, Qdrant, Pinecone, Chroma, Weaviate, LanceDB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Launch pricing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;First 20 buyers: ₹699 (~$8.50)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;After that: ₹999 through Nov 30&lt;/li&gt;
&lt;li&gt;From Dec 1: ₹1499&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7-day no-questions refund. Delivered as PDF + repo, emailed within seconds of payment.&lt;/p&gt;

&lt;p&gt;Link: &lt;a href="https://aiunplugged.in/products/rag-production-handbook/" rel="noopener noreferrer"&gt;https://aiunplugged.in/products/rag-production-handbook/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy to answer questions in the comments — what part of RAG production bit you hardest?&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>llm</category>
      <category>python</category>
    </item>
    <item>
      <title>Agent frameworks 2026 — LangGraph vs CrewAI vs AutoGen</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Thu, 05 Nov 2026 16:56:37 +0000</pubDate>
      <link>https://dev.to/aiunplugged/agent-frameworks-2026-langgraph-vs-crewai-vs-autogen-4d8h</link>
      <guid>https://dev.to/aiunplugged/agent-frameworks-2026-langgraph-vs-crewai-vs-autogen-4d8h</guid>
      <description>&lt;p&gt;Three AI agent frameworks have survived the shakeout of 2024-2025 and now dominate production builds in late 2026: LangGraph, CrewAI, and AutoGen. Each solves the same problem — turning an LLM into a system that reasons, calls tools, and iterates — but each takes a sharply different stance on how the agent should be structured, debugged, and scaled.&lt;/p&gt;

&lt;p&gt;Teams starting an agent project in 2026 pick between these three (and occasionally lower-weight entrants like Microsoft Agent Framework or OpenAgents). The choice shapes everything downstream: how a stuck agent gets debugged, how long-running workflows survive a server restart, how the team adds a new tool without rewriting the pipeline. This article walks each framework honestly, including the parts the vendor-marketing comparisons gloss over.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three in one sentence each
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LangGraph&lt;/strong&gt; — stateful graph of nodes with explicit edges and checkpointing. Highest control, most boilerplate, best for production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CrewAI&lt;/strong&gt; — role-based agents working as a crew with task assignments. Fastest to prototype, limited production control.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AutoGen&lt;/strong&gt; — multi-agent conversation framework. Best when the product itself is agents talking to agents. Microsoft moved it to maintenance mode in late 2025; the successor is Microsoft Agent Framework (GA April 2026), and the community fork is AG2 (details below).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  LangGraph
&lt;/h2&gt;

&lt;p&gt;LangGraph models an agent as a directed graph. Each node is a function (an LLM call, a tool invocation, a conditional router); each edge is a transition rule. State flows explicitly between nodes and is persisted at every "super-step" via a checkpointer, which means a crashed workflow resumes from the last checkpoint instead of restarting from scratch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where LangGraph wins:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deterministic execution + state persistence.&lt;/strong&gt; The checkpointer is the feature that makes LangGraph the production default. Long-running workflows (minutes or hours), human-in-the-loop approvals, and multi-day agents all survive restarts, deploys, and transient crashes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explicit control flow.&lt;/strong&gt; Every path through the agent is a defined edge. Debugging means tracing the graph, not reading agent transcripts hoping to find where the model went off the rails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Human-in-the-loop.&lt;/strong&gt; The &lt;code&gt;interrupt()&lt;/code&gt; primitive (stable since v1.0) pauses a workflow for human approval mid-run without hacks or external queues.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Where LangGraph loses:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Steep learning curve.&lt;/strong&gt; Defining every node, every edge, and every routing function is more boilerplate than CrewAI for simple workflows. A "research agent that searches and summarises" is maybe 20 lines in CrewAI and 80-100 in LangGraph.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LangChain lineage.&lt;/strong&gt; LangGraph lives under the LangChain umbrella. Teams that migrated away from LangChain for complexity reasons may inherit the same complexity here.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pick LangGraph when:&lt;/strong&gt; the workflow is longer than one LLM round-trip, has conditional branching, needs retries or approvals, or must survive process restarts.&lt;/p&gt;

&lt;h2&gt;
  
  
  CrewAI
&lt;/h2&gt;

&lt;p&gt;CrewAI models an agent as a role (a researcher, a writer, a reviewer) with a specific goal and backstory, plus a set of tools. Multiple roles together form a crew that completes tasks in sequence or in parallel. The abstraction is deliberately simple — the framework hides the orchestration mechanics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where CrewAI wins:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fast to prototype.&lt;/strong&gt; A functioning multi-agent demo is possible in under an hour. Role, goal, task — three concepts and the agent runs. For stakeholder demos or "will this even work" exploration, CrewAI is the fastest path.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Role-based thinking matches how teams explain workflows.&lt;/strong&gt; "A researcher agent that finds sources, a writer agent that drafts, a reviewer agent that checks" is easy to design and communicate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Growing ecosystem.&lt;/strong&gt; By mid-2026 the CrewAI community has shipped templates for common patterns (content review, research synthesis, customer support triage) that get a team from zero to running fast.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Where CrewAI loses:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No built-in checkpointing for long-running workflows.&lt;/strong&gt; A crewed task that runs 20 minutes and fails at minute 18 starts over.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited control over agent-to-agent communication.&lt;/strong&gt; The framework decides routing based on roles and task descriptions; teams needing fine-grained control over who-talks-to-whom-when hit the ceiling of the abstraction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging is harder.&lt;/strong&gt; When the writer agent disagrees with the reviewer and the final output is wrong, the only trail is the agent transcript. There is no explicit graph to inspect.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The migration path that shows up in multiple field reports: teams start with CrewAI for the demo, then migrate to LangGraph when the system needs production-grade state management and conditional routing. The CrewAI code does not translate directly, so plan for a rewrite if that migration happens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pick CrewAI when:&lt;/strong&gt; the goal is a prototype or an internal tool, the workflow fits a role-based mental model, and production reliability matters less than speed of iteration.&lt;/p&gt;

&lt;h2&gt;
  
  
  AutoGen — status caveat first
&lt;/h2&gt;

&lt;p&gt;Microsoft's AutoGen is where multi-agent conversation frameworks began. The model is simple: define several agents, let them message each other in a group chat, each contributing when relevant. The orchestration emerges from the conversation itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The status question that must be answered before picking it in 2026:&lt;/strong&gt; Microsoft moved AutoGen to community-managed maintenance mode in late 2025, with its last feature release in September 2025. Microsoft's intended successor, Microsoft Agent Framework (MAF), reached general availability on April 2, 2026, combining AutoGen's multi-agent orchestration with Semantic Kernel in a single SDK. A separate community fork, AG2, is independently developed by AutoGen's original authors and continues to ship new features. Before committing to AutoGen for a new production system, decide whether to track MAF, stay on AutoGen-maintenance, or switch to AG2 — each is a different long-term bet.&lt;/p&gt;

&lt;p&gt;Assuming AutoGen is still viable for the target workload:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where AutoGen wins:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Conversation-driven workflows.&lt;/strong&gt; When the product itself is agents debating, reviewing each other's work, or exploring a problem through dialogue, AutoGen's model fits natively. Code review agents, research-and-critique agents, multi-expert simulations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Group chat semantics.&lt;/strong&gt; AutoGen's &lt;code&gt;GroupChat&lt;/code&gt; abstraction captures messy, non-linear agent interaction cleanly. LangGraph can model this but with more effort; CrewAI's role model does not fit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Where AutoGen loses:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unbounded loops cost money.&lt;/strong&gt; Without hard termination conditions, open-ended agent debates can consume 5-10× the expected tokens. Every production AutoGen deployment needs a strict &lt;code&gt;max_turns&lt;/code&gt; or a budget-based terminator.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging a group chat gone wrong.&lt;/strong&gt; When four agents produced 30 messages and the final output is wrong, root-causing is harder than in a graph or role-based system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The status question.&lt;/strong&gt; Even if AutoGen is maintained, the ambiguity itself is a reason to think twice for a long-horizon production bet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pick AutoGen when:&lt;/strong&gt; multi-agent conversation IS the product (not a means), the organisation accepts the maintenance uncertainty, and strict termination conditions are built in from day one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The decision matrix
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Need&lt;/th&gt;
&lt;th&gt;Pick&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Long-running production workflow with retries + human approval&lt;/td&gt;
&lt;td&gt;LangGraph&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fast prototype of a multi-step agent for a stakeholder demo&lt;/td&gt;
&lt;td&gt;CrewAI&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Agents literally talking to each other as the product mechanic&lt;/td&gt;
&lt;td&gt;AutoGen (after verifying support status)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deterministic, debuggable, auditable control flow&lt;/td&gt;
&lt;td&gt;LangGraph&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Role-based task delegation, teams can explain the design to non-engineers&lt;/td&gt;
&lt;td&gt;CrewAI&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Code review / expert simulation / debate-driven workflows&lt;/td&gt;
&lt;td&gt;AutoGen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Must survive process restart mid-workflow&lt;/td&gt;
&lt;td&gt;LangGraph&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prototype first, migrate to LangGraph later when scaling&lt;/td&gt;
&lt;td&gt;CrewAI → LangGraph migration path&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Cost — be sceptical of per-framework numbers
&lt;/h2&gt;

&lt;p&gt;Several comparison articles publish "framework X uses N× more tokens than framework Y" tables without disclosing their test workload. These numbers are mostly noise:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model choice (GPT-5-mini vs Claude Opus) swings cost 10-20×. Framework contributes &amp;lt;2× at most.&lt;/li&gt;
&lt;li&gt;Prompt caching usage (see &lt;a href="https://aiunplugged.in/blog/prompt-caching-explained/" rel="noopener noreferrer"&gt;prompt caching article&lt;/a&gt;) swings cost 2-10×. Framework-level loops do not affect this.&lt;/li&gt;
&lt;li&gt;Loop termination logic matters more than framework. An AutoGen group chat with &lt;code&gt;max_turns=3&lt;/code&gt; costs less than a LangGraph loop with no bound.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The honest framing: framework token overhead is real but is almost always dominated by model choice and prompt design. Pick the framework for the control-flow fit; manage cost through model routing and caching at the LLM call layer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three failure modes to design around
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Unbounded loops.&lt;/strong&gt; Any agent that can call itself (or exchange messages with another agent) needs a strict loop termination condition. "Keep going until the task is done" is not safe. Pick either a &lt;code&gt;max_iterations&lt;/code&gt; cap, a token-budget cap, or a hard timeout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. State loss on deploy.&lt;/strong&gt; Agents that run minutes or hours break on every deploy without checkpointing. LangGraph's checkpointer solves this natively; CrewAI and AutoGen users must build their own state persistence or accept that long-running jobs will restart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Debugging without a trace.&lt;/strong&gt; All three frameworks support tracing (LangSmith for LangGraph/CrewAI; MLflow for AutoGen; OpenTelemetry everywhere). Enable tracing from day one — not day 100 when the first production incident lands. The cost of enabling it later is reproducing bugs you no longer have the state to reproduce.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three mistakes to avoid
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Mistake 1 — Picking by GitHub stars.&lt;/strong&gt; All three frameworks have &amp;gt;20K stars by late 2026. Popularity does not predict fit for a specific workload.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mistake 2 — Treating LangGraph as a drop-in CrewAI replacement.&lt;/strong&gt; The abstractions are different. Code does not port. Plan for a rewrite if a migration happens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mistake 3 — Expecting agents to be reliable without evals.&lt;/strong&gt; All three frameworks produce agents whose output quality depends on prompt engineering and model choice. Build an eval set BEFORE scaling, not after. The &lt;a href="https://aiunplugged.in/blog/rag-evaluation-metrics-and-tooling/" rel="noopener noreferrer"&gt;RAG evaluation article&lt;/a&gt; covers the pattern; agent evals are a close cousin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;For a 2026 agent project starting fresh:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;If production reliability matters and the workflow is non-trivial:&lt;/strong&gt; LangGraph. The boilerplate pays for itself the first time a workflow crashes and resumes cleanly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If the goal is a demo or an internal tool:&lt;/strong&gt; CrewAI. Fastest to running agents, with the understanding that a rewrite may follow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If multi-agent conversation IS the product:&lt;/strong&gt; Microsoft Agent Framework (the Microsoft-supported path) or AG2 (the community-maintained fork of AutoGen) — plain AutoGen is maintenance-only.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The frameworks will keep evolving. The underlying skills — defining state, bounding loops, instrumenting with traces, running eval sets — transfer across all three.&lt;/p&gt;

&lt;p&gt;Related reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/mcp-model-context-protocol-explained/" rel="noopener noreferrer"&gt;MCP explained — how LLM apps talk to tools in 2026&lt;/a&gt; — the standard for tools these agents call&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/prompt-caching-explained/" rel="noopener noreferrer"&gt;Prompt caching — the LLM discount most teams aren't using&lt;/a&gt; — single biggest cost lever for agent workloads&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;Build a RAG pipeline from scratch&lt;/a&gt; — RAG is one tool agents commonly call&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/rag-evaluation-metrics-and-tooling/" rel="noopener noreferrer"&gt;Evaluating RAG — retrieval + generation metrics&lt;/a&gt; — the eval harness pattern that extends to agent evals&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aiunplugged.in/blog/claude-api-vs-openai-api-developers/" rel="noopener noreferrer"&gt;Claude API vs OpenAI API — developer perspective&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally published on &lt;a href="https://aiunplugged.in/blog/agent-frameworks-compared-2026/" rel="noopener noreferrer"&gt;aiunplugged.in&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>agents</category>
      <category>llm</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Evaluating RAG — retrieval + generation metrics that matter</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Sat, 24 Oct 2026 19:05:54 +0000</pubDate>
      <link>https://dev.to/aiunplugged/evaluating-rag-retrieval-generation-metrics-that-matter-2ffp</link>
      <guid>https://dev.to/aiunplugged/evaluating-rag-retrieval-generation-metrics-that-matter-2ffp</guid>
      <description>&lt;p&gt;Most RAG systems that silently decay in production decay in the retrieval layer. The LLM output still looks fluent, so the surface tests pass. Users just notice that the answers get subtly wrong — citing the wrong clause, mixing up two customers, giving an outdated version of the policy. By the time anyone reports it, the regression has been live for weeks.&lt;/p&gt;

&lt;p&gt;Evaluation is the layer that catches those regressions before they ship. This article covers the metrics that actually tell you something, with runnable Python, and the minimum test-set recipe to get started. Prerequisite reading: the &lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;RAG pipeline walkthrough&lt;/a&gt; ships the four-step loop this article plugs into.&lt;/p&gt;

&lt;h2&gt;
  
  
  The two levels of RAG evaluation
&lt;/h2&gt;

&lt;p&gt;A RAG system has two independent failure modes, so it needs two independent sets of metrics:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Retrieval quality&lt;/strong&gt; — given a question, did the retriever return the chunks that actually contain the answer? If not, no amount of LLM cleverness can produce the right answer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generation quality&lt;/strong&gt; — given the retrieved chunks (whatever they are), did the LLM produce a correct, faithful answer? A fluent answer that invents facts the chunks don't support is a generation failure, not a retrieval failure.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Evaluating only one layer misses half the problem. If retrieval is bad but generation masks it with plausible-sounding filler, the user sees "correct-looking wrong answers" and loses trust before anyone notices the metric that would have surfaced it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retrieval metrics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Precision@k
&lt;/h3&gt;

&lt;p&gt;Of the top-K chunks the retriever returned for a question, how many are relevant?&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;precision_at_k&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retrieved_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;relevant_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&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="nb"&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="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;top_k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;retrieved_ids&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="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;top_k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="n"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&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;doc_id&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;top_k&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;doc_id&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;relevant_ids&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;hits&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top_k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: top-5 retrieved = &lt;code&gt;[A, B, C, D, E]&lt;/code&gt;, relevant = &lt;code&gt;{A, C}&lt;/code&gt;. Precision@5 = 2/5 = 0.4.&lt;/p&gt;

&lt;p&gt;Use it when the context window is limited and the LLM gets confused by irrelevant chunks. Higher precision means less noise in the generation prompt.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recall@k
&lt;/h3&gt;

&lt;p&gt;Of all the relevant chunks that exist for a question, how many did the retriever surface in the top-K?&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;recall_at_k&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retrieved_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;relevant_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&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="nb"&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="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;top_k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retrieved_ids&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="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;relevant_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;  &lt;span class="c1"&gt;# nothing to find — vacuously perfect
&lt;/span&gt;    &lt;span class="n"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top_k&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;relevant_ids&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;hits&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;relevant_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: top-5 retrieved = &lt;code&gt;[A, B, C, D, E]&lt;/code&gt;, relevant = &lt;code&gt;{A, C, F}&lt;/code&gt;. Recall@5 = 2/3 ≈ 0.67 (missed F).&lt;/p&gt;

&lt;p&gt;Use it when the answer requires combining multiple sources. If recall@k is low, the LLM is working with incomplete information and may fabricate the missing piece.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mean Reciprocal Rank (MRR)
&lt;/h3&gt;

&lt;p&gt;How high up the ranking does the first relevant chunk appear?&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;reciprocal_rank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retrieved_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;relevant_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;float&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc_id&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retrieved_ids&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;doc_id&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;relevant_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&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;i&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;return&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;mean_reciprocal_rank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evaluations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]]])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;float&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;evaluations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reciprocal_rank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rel&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;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rel&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;evaluations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evaluations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: first relevant chunk at rank 1 → RR = 1.0; at rank 3 → RR = 1/3 ≈ 0.33; not retrieved → RR = 0.&lt;/p&gt;

&lt;p&gt;MRR is the right metric when the application only shows the top result to the user (search autocomplete, "best match" chat answers). It's less useful for multi-document synthesis where several top chunks matter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Normalised Discounted Cumulative Gain (nDCG)
&lt;/h3&gt;

&lt;p&gt;The retrieval metric most forgiving of what order relevant chunks appear in — but rewards higher positions. Useful when some relevant chunks are more relevant than others (graded relevance, not binary).&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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dcg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;relevances&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&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="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Discounted Cumulative Gain. relevances[i] = graded relevance of result i.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rel&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rel&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;relevances&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;ndcg_at_k&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retrieved_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;relevance_grades&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&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="nb"&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="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Normalised DCG@k. relevance_grades = {doc_id: grade}, grade &amp;gt;= 0.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;relevance_grades&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;doc_id&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;retrieved_ids&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="n"&gt;ideal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;relevance_grades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&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="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;ideal&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ideal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;dcg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;dcg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ideal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;nDCG = 1.0 means the retrieved ranking matches the ideal ranking. nDCG = 0 means nothing relevant was retrieved.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which metric to pick
&lt;/h3&gt;

&lt;p&gt;A defensible default for most production RAG: track &lt;strong&gt;recall@k&lt;/strong&gt; (did we find all the needed context?) and &lt;strong&gt;precision@k&lt;/strong&gt; (how much noise in that context?). Add &lt;strong&gt;MRR&lt;/strong&gt; if the UI only shows the top result. Add &lt;strong&gt;nDCG&lt;/strong&gt; only if relevance is actually graded, not binary — which is rare in practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generation metrics
&lt;/h2&gt;

&lt;p&gt;Retrieval metrics judge the retriever. Generation metrics judge whether the LLM used the retrieved context correctly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Faithfulness
&lt;/h3&gt;

&lt;p&gt;Did the generated answer stick to the retrieved context, or did it invent claims the chunks don't support? Faithfulness is the metric that catches hallucination in a RAG system where retrieval was fine.&lt;/p&gt;

&lt;p&gt;The honest way to measure faithfulness at scale is LLM-as-judge: a second model inspects the generated answer and the retrieved context, and flags claims in the answer that aren't supported by the context. Lightweight proxy for small test sets:&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;claims_in_answer_covered_by_context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Trivial version — split the answer into sentences and check whether each
    sentence has a token-overlap with the context above a threshold.
    Not accurate enough for production; use an LLM judge for anything serious.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;
    &lt;span class="n"&gt;sentences&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="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(?&amp;lt;=[.!?])\s+&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;answer&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="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;

    &lt;span class="n"&gt;context_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&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="n"&gt;covered&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;s&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;s_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&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="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;s_tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="n"&gt;overlap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s_tokens&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;context_tokens&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s_tokens&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;overlap&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;covered&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;return&lt;/span&gt; &lt;span class="n"&gt;covered&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this proxy to spot the worst drift. For reliable faithfulness in production, use LLM-as-judge with a prompt tuned to your domain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer relevance
&lt;/h3&gt;

&lt;p&gt;Did the answer address the question that was asked, or did it address an adjacent question? A hallucinated answer that happens to match some retrieved context can still be irrelevant.&lt;/p&gt;

&lt;p&gt;Measured by prompting a judge model: "Given this question and this answer, rate how directly the answer responds, 1 to 5." Store the average per test query, track across releases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Context recall + context precision
&lt;/h3&gt;

&lt;p&gt;The two retrieval-adjacent metrics that look at how well the generation step used the context:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Context recall:&lt;/strong&gt; of all the relevant information in the retrieved context, how much appeared in the answer? Low context recall = the LLM retrieved the answer but didn't surface it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context precision:&lt;/strong&gt; of the retrieved chunks, how many were actually used in generating the answer? Low context precision = the retriever is noisy and the LLM is working harder than it should.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both are classically measured with LLM-as-judge over a test set, and are the metrics that Ragas and similar libraries report by default.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building a minimum test set
&lt;/h2&gt;

&lt;p&gt;A test set is a list of questions with the "correct" answer for each, plus (for retrieval metrics) a set of chunk IDs that should have been retrieved. The smallest useful test set has roughly 20-50 questions covering the obvious categories of query the production system will see.&lt;/p&gt;

&lt;p&gt;Minimum shape:&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;


&lt;span class="nd"&gt;@dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EvalQuery&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;expected_answer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;relevant_chunk_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# Chunks that contain the answer
&lt;/span&gt;    &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;  &lt;span class="c1"&gt;# Tag for slicing results — "pricing", "troubleshooting", etc.
&lt;/span&gt;

&lt;span class="n"&gt;TEST_SET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;EvalQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is the refund window after purchase?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;expected_answer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;7 days from the date of purchase.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;relevant_chunk_ids&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;chunk_42&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;policy&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nc"&gt;EvalQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Which Python version is required?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;expected_answer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Python 3.10 or later.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;relevant_chunk_ids&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;chunk_17&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;chunk_18&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;setup&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="c1"&gt;# ... 18 more
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where the questions come from matters more than how many. Three defensible sources:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Real user queries from logs&lt;/strong&gt; — if the system is already live, the top-50 most common actual questions are the right test set. Resolve "correct answer" by hand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Support ticket questions&lt;/strong&gt; — most teams have an archive of "customer asked X" questions with a resolved answer. Those are gold.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hand-written by whoever knows the corpus best&lt;/strong&gt; — fastest to start, but biased toward the writer's mental model of what users ask.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The handbook's Chapter 4 covers a five-step human review protocol for assembling the first 50 queries, and the pattern for growing the test set from production logs. For a free-article minimum, 20 hand-written queries covering 3-5 categories is enough to catch obvious regressions.&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple pytest harness
&lt;/h2&gt;

&lt;p&gt;Wire the metrics into pytest so the eval runs on every PR. Minimum version:&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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pytest&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;your_rag_app&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;rag_pipeline&lt;/span&gt;  &lt;span class="c1"&gt;# your retrieve + generate function
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;your_eval_set&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TEST_SET&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;your_metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;precision_at_k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recall_at_k&lt;/span&gt;


&lt;span class="nd"&gt;@pytest.fixture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;session&lt;/span&gt;&lt;span class="sh"&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;predictions&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&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;query&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;TEST_SET&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;retrieved_ids&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rag_pipeline&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;question&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;results&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;retrieved_ids&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;retrieved_ids&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;answer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;answer&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;results&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_retrieval_recall_above_threshold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;predictions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;recalls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nf"&gt;recall_at_k&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;retrieved_ids&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;relevant_chunk_ids&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;5&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;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;predictions&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;avg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recalls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recalls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;avg&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Average recall@5 = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;avg&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, below 0.75 threshold&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_retrieval_precision_above_threshold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;predictions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;precisions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nf"&gt;precision_at_k&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;retrieved_ids&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;relevant_chunk_ids&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;5&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;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;predictions&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;avg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;precisions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;precisions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;avg&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Average precision@5 = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;avg&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, below 0.30 threshold&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thresholds (0.75 recall, 0.30 precision) are starting points — tune to the system's current baseline, then enforce no regression below it. Run on every pull request so a chunking tweak or embedding model change can't silently break retrieval quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tooling landscape in 2026
&lt;/h2&gt;

&lt;p&gt;Three evaluation libraries dominate for RAG:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ragas&lt;/strong&gt; (&lt;code&gt;pip install ragas&lt;/code&gt;) — the de facto standard for RAG-specific metrics. Ships faithfulness, answer relevance, context recall, context precision out of the box, using an LLM judge. Lowest-friction way to add generation metrics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DeepEval&lt;/strong&gt; (&lt;code&gt;pip install deepeval&lt;/code&gt;) — pytest-native. Metrics look like unit tests, which is nice when CI integration is the priority. Covers RAG and non-RAG LLM apps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Promptfoo&lt;/strong&gt; (&lt;code&gt;npm install -g promptfoo&lt;/code&gt; or Python API) — config-driven YAML eval sets, side-by-side model comparison, caching built in. Good when the main thing being evaluated is prompt + model choice, not retrieval.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each has trade-offs around LLM-judge cost, metric calibration, and how it integrates with pytest. The handbook's Chapter 4 includes a decision table for picking between them based on team shape and existing CI.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this article deliberately does NOT cover
&lt;/h2&gt;

&lt;p&gt;Evaluation in production has a longer tail than the metrics above. The parts not covered here, and where to find them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The four tested LLM-as-judge prompts&lt;/strong&gt; that calibrate reliably against human review, with the measurement protocol for calibration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The full pytest + GitHub Actions CI harness&lt;/strong&gt; that runs on every PR without blowing up LLM API bills.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How to tell real metric movement from statistical noise&lt;/strong&gt; on a 50-query test set — the Wilcoxon signed-rank procedure that avoids false positives.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The five-step human review protocol&lt;/strong&gt; for building the first 50-query test set from scratch when there are no production logs yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Growing the test set from production logs&lt;/strong&gt; — the sampling strategy that keeps the test set representative over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ragas vs DeepEval vs Promptfoo&lt;/strong&gt; — the decision table based on team shape, LLM-judge cost, and existing CI.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those live in Chapter 4 of &lt;a href="https://aiunplugged.in/products/rag-production-handbook/" rel="noopener noreferrer"&gt;The RAG Production Handbook&lt;/a&gt; — launching November 9, 2026. First 20 buyers at ₹699 (regular launch ₹999, rising to ₹1,499 on Dec 1).&lt;/p&gt;

&lt;p&gt;Related reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;Build a RAG pipeline from scratch&lt;/a&gt; — the four-step loop in one Python file&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/rag-chunking-strategies/" rel="noopener noreferrer"&gt;RAG chunking — 5 strategies with runnable Python&lt;/a&gt; — step 1 of the loop in depth&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/vector-databases-compared/" rel="noopener noreferrer"&gt;Vector databases compared — pgvector, Qdrant, Pinecone, Chroma&lt;/a&gt; — the storage layer evaluation plugs into&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/when-not-to-use-rag/" rel="noopener noreferrer"&gt;When to skip RAG — 5 cases where it's the wrong tool&lt;/a&gt; — before you evaluate, confirm RAG is the right architecture&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/rag-production-faq/" rel="noopener noreferrer"&gt;RAG Production FAQ&lt;/a&gt; — 24 questions from real systems&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>llm</category>
      <category>rag</category>
      <category>testing</category>
      <category>analytics</category>
    </item>
    <item>
      <title>RAG chunking — 5 strategies with runnable Python</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Fri, 16 Oct 2026 17:51:51 +0000</pubDate>
      <link>https://dev.to/aiunplugged/rag-chunking-5-strategies-with-runnable-python-580h</link>
      <guid>https://dev.to/aiunplugged/rag-chunking-5-strategies-with-runnable-python-580h</guid>
      <description>&lt;p&gt;Originally published on &lt;a href="https://aiunplugged.in/blog/rag-chunking-strategies/" rel="noopener noreferrer"&gt;aiunplugged.in&lt;/a&gt; — cross-posting for the Dev.to community.&lt;/p&gt;

&lt;p&gt;Chunking is the step that most RAG tutorials gloss over — "just split your documents into 500-token chunks" — and most retrieval problems trace back to. The right chunk strategy depends on the shape of the source documents and the shape of the queries, not on a blog post's default. This article walks the five strategies that cover ~95% of production RAG pipelines in 2026, with runnable Python for each.&lt;/p&gt;

&lt;p&gt;Prerequisite: the &lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;RAG pipeline walkthrough&lt;/a&gt; ships the four-step loop in one file. This article drops into step 1 of that loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why chunking matters
&lt;/h2&gt;

&lt;p&gt;A chunk is the unit of retrieval. When a user asks a question, the retriever returns the top-K chunks whose embeddings sit closest to the question's embedding. Three things can go wrong, all driven by chunk shape:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Chunks too large&lt;/strong&gt; — the embedding averages away specificity. The chunk gets retrieved for too many loosely-related queries, pushing truly relevant chunks out of the top-K.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunks too small&lt;/strong&gt; — the chunk loses context. The retrieved passage reads fine on its own but the LLM can't answer the question because the resolution of the answer sits in neighbouring passages that weren't retrieved.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunks split across semantic boundaries&lt;/strong&gt; — a definition is in one chunk, the example is in another, and only one gets retrieved. The LLM gets half the answer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The five strategies below address those three failure modes in different ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup — shared across all strategies
&lt;/h2&gt;

&lt;p&gt;Every strategy below uses this scaffolding. Install once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;langchain-text-splitters sentence-transformers numpy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shared helper for displaying chunks:&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Callable&lt;/span&gt;


&lt;span class="nd"&gt;@dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Chunk&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="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&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;c&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;preview&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; #&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;] (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&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="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; chars) &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample input used in every example below:&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;SAMPLE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
# Production RAG — chunking

Chunking is the step that determines retrieval quality more than any other.
A chunk too large averages away specificity. A chunk too small loses context.

## Fixed-size chunking

The simplest strategy. Split the document into pieces of exactly N characters
or N tokens, with M units of overlap between adjacent chunks. Works when the
corpus is uniform prose with no strong structural cues.

## Recursive splitting

Recursive splitting walks a hierarchy of separators — paragraphs, then
sentences, then words — and splits at the highest level that keeps chunks
within the size limit. Preserves structure better than fixed-size.
&lt;/span&gt;&lt;span class="sh"&gt;"""&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Strategy 1 — Fixed-size chunking
&lt;/h2&gt;

&lt;p&gt;Split at N characters (or tokens) with M characters of overlap. No awareness of structure, sentences, or semantics. Fast, deterministic, and often the right default when the corpus is uniform.&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;fixed_size_chunk&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&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="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;i&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;while&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;len&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="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;min&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;size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&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="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="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&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;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fixed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;end&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;end&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;end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;overlap&lt;/span&gt;
        &lt;span class="n"&gt;i&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;return&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;


&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fixed_size_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SAMPLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When it wins:&lt;/strong&gt; uniform prose, logs, chat transcripts, source code where line boundaries don't carry meaning. Also the right default when the corpus is small enough that chunking strategy doesn't move retrieval quality much.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it loses:&lt;/strong&gt; structured documents where cutting at an arbitrary character boundary breaks a heading away from its content, or splits a code block mid-function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strategy 2 — Recursive character splitting
&lt;/h2&gt;

&lt;p&gt;Walks a priority list of separators — paragraph breaks, then line breaks, then sentences, then words, then characters — and splits at the highest-priority separator that keeps chunks under the size limit. This is LangChain's &lt;code&gt;RecursiveCharacterTextSplitter&lt;/code&gt;, and it's the right default for mixed structured content.&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_text_splitters&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RecursiveCharacterTextSplitter&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;recursive_chunk&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;splitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RecursiveCharacterTextSplitter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;chunk_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;chunk_overlap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;overlap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;# Priority order — try to split at paragraph break first, then line, etc.
&lt;/span&gt;        &lt;span class="n"&gt;separators&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&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;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;splitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split_text&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&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;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;recursive&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="o"&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&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="p"&gt;]&lt;/span&gt;


&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recursive_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SAMPLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When it wins:&lt;/strong&gt; mixed-structure prose — documentation, articles, Notion pages, Markdown with occasional lists and code blocks. The default for most team docs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it loses:&lt;/strong&gt; very long paragraphs without internal breaks (the splitter falls back to character-level and loses the benefit), or when the separators-list doesn't match the document format (for example, source code where the hierarchy is &lt;code&gt;class → method → statement&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Strategy 3 — Sentence-boundary chunking
&lt;/h2&gt;

&lt;p&gt;Split the document into sentences first, then greedily group consecutive sentences into chunks until a size budget is hit. Chunks never cut mid-sentence, which keeps embeddings 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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sentence_chunk&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="c1"&gt;# Split on sentence terminators. Not perfect — abbreviations like "e.g."
&lt;/span&gt;    &lt;span class="c1"&gt;# and "Dr." will trip a naive regex. For production, use spaCy or NLTK.
&lt;/span&gt;    &lt;span class="n"&gt;sentences&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="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(?&amp;lt;=[.!?])\s+&lt;/span&gt;&lt;span class="sh"&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="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&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;current&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&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;current_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;i&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;s&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sentences&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;current_len&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&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;current&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&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;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sentence&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;n_sentences&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&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="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;current_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&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;current&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;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;current_len&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;# +1 for the joining space
&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&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;current&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&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;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sentence&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;n_sentences&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&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;chunks&lt;/span&gt;


&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sentence_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SAMPLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When it wins:&lt;/strong&gt; Q&amp;amp;A corpora, FAQ documents, interview transcripts, dialogue. Any corpus where the natural unit of meaning is a sentence or two.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it loses:&lt;/strong&gt; code (semicolons terminate statements but the function is the meaningful unit), heavily-abbreviated text that confuses sentence tokenisers, non-English scripts without clear sentence terminators.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strategy 4 — Document-structure chunking
&lt;/h2&gt;

&lt;p&gt;Use the document's own structural markers — markdown headings, HTML &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;/&lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt;, docstring sections — to define chunk boundaries. Each chunk is a self-contained section. Chunk sizes end up uneven, which is fine as long as no single section blows past the embedding model's context window.&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;markdown_section_chunk&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min_size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="c1"&gt;# Split at any level of markdown heading. A chunk starts at a heading and
&lt;/span&gt;    &lt;span class="c1"&gt;# ends at the next heading of equal or higher level.
&lt;/span&gt;    &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&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;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&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="nb"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&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;current_heading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;
    &lt;span class="n"&gt;i&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;def&lt;/span&gt; &lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;nonlocal&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;buffer&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;section&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&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="nb"&gt;buffer&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="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;section&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;min_size&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;section&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&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;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;structure&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;heading&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;current_heading&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="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;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;lines&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^#{1,6}\s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&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;flush&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nb"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;current_heading&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="nf"&gt;lstrip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;# &lt;/span&gt;&lt;span class="sh"&gt;"&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="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;buffer&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;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;flush&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;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;markdown_section_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SAMPLE&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When it wins:&lt;/strong&gt; technical documentation, markdown knowledge bases, structured reports, policy documents, textbook chapters. Any corpus where the writer deliberately organised ideas under headings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it loses:&lt;/strong&gt; unstructured prose (novels, email, chat), documents where sections are too large to embed as single chunks, documents where section titles are generic ("Overview", "Introduction") and don't help the embedding carry context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strategy 5 — Semantic chunking
&lt;/h2&gt;

&lt;p&gt;Embed every sentence, then look at the cosine similarity between adjacent sentence embeddings. Where the similarity drops below a threshold, that's a topic boundary — split there. Chunks end up grouped by semantic coherence rather than character count.&lt;/p&gt;

&lt;p&gt;More expensive than the other four (one embedding call per sentence at index time), but produces the cleanest chunks when the corpus is dense and topics shift smoothly within sections.&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="kn"&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sentence_transformers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SentenceTransformer&lt;/span&gt;


&lt;span class="n"&gt;_model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_embedder&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;_model&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_model&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&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;SentenceTransformer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;all-MiniLM-L6-v2&lt;/span&gt;&lt;span class="sh"&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;_model&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;semantic_chunk&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;similarity_threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;sentences&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="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(?&amp;lt;=[.!?])\s+&lt;/span&gt;&lt;span class="sh"&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="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&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="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;semantic&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;n_sentences&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt;
        &lt;span class="p"&gt;)]&lt;/span&gt;

    &lt;span class="n"&gt;embeddings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_embedder&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;sentences&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Cosine similarity between each pair of adjacent sentences
&lt;/span&gt;    &lt;span class="n"&gt;sims&lt;/span&gt; &lt;span class="o"&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="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentences&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;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;embeddings&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="n"&gt;embeddings&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&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;sims&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="nf"&gt;float&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="nf"&gt;dot&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="o"&gt;/&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="n"&gt;linalg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;norm&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="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="n"&gt;linalg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;norm&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="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&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="nb"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sentences&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="n"&gt;i&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;idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sim&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sims&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;sim&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;similarity_threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Topic shift detected — close the current chunk
&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="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&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="nb"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&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;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;semantic&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;split_similarity&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sim&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="nb"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&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;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;buffer&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;sentences&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&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;if&lt;/span&gt; &lt;span class="nb"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&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="nb"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&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;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;semantic&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;metadata&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;return&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;


&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;semantic_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SAMPLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;similarity_threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The similarity threshold is a tunable parameter — lower values produce fewer, larger chunks (more tolerant of topic drift); higher values produce more, smaller chunks (stricter about topic coherence). Start at 0.5 and adjust against an evaluation set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it wins:&lt;/strong&gt; long essays, academic papers, dense technical writing where topics shift gradually without clear structural markers. News archives, podcast transcripts, meeting notes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it loses:&lt;/strong&gt; short documents (not enough sentences to form clusters), already-structured content (document-structure chunking is cheaper and matches reality), heterogeneous corpora where no single similarity threshold works across documents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking a strategy — the rough rule
&lt;/h2&gt;

&lt;p&gt;Match the strategy to the dominant shape of the source:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Uniform prose, logs, chat transcripts&lt;/strong&gt; → fixed-size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mixed Markdown / HTML / Notion-style docs&lt;/strong&gt; → recursive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Q&amp;amp;A, FAQs, dialogue, interviews&lt;/strong&gt; → sentence-boundary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Technical docs, textbooks, policy, specs with headings&lt;/strong&gt; → document-structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long essays, academic papers, meeting notes without structural markers&lt;/strong&gt; → semantic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Benchmark two or three candidates on a held-out evaluation set before committing. In practice, recursive-with-markdown-separators is a defensible default for 70% of production corpora in 2026, and the other four are specialised wins for the remaining 30%.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this article deliberately does NOT cover
&lt;/h2&gt;

&lt;p&gt;Chunking in production has a longer tail than the five strategies above. The parts not covered here, and where to find them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A one-page decision table&lt;/strong&gt; that maps corpus shape to strategy in 30 seconds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge cases:&lt;/strong&gt; multilingual text with mixed scripts, PDFs with tables that get destroyed by naive extractors, legal contracts with nested clause structures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunk metadata schema&lt;/strong&gt; — what fields to store alongside each chunk so retrieval can filter and the generator can cite.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunker versioning&lt;/strong&gt; — rolling out a new strategy without re-embedding the entire corpus from scratch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delta reindex strategy&lt;/strong&gt; — detecting which chunks actually changed when source documents are edited, so only those get re-embedded. Cuts embedding cost on small edits by ~99%.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those live in Chapter 2 of &lt;a href="https://aiunplugged.in/products/rag-production-handbook/" rel="noopener noreferrer"&gt;The RAG Production Handbook&lt;/a&gt; — launching November 9, 2026. First 20 buyers at ₹699 (regular launch ₹999, rising to ₹1,499 on Dec 1).&lt;/p&gt;

&lt;p&gt;Related reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;Build a RAG pipeline from scratch&lt;/a&gt; — the 4-step loop in one Python file&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/vector-databases-compared/" rel="noopener noreferrer"&gt;Vector databases compared — pgvector, Qdrant, Pinecone, Chroma&lt;/a&gt; — where the chunks live after step 1&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/when-not-to-use-rag/" rel="noopener noreferrer"&gt;When to skip RAG — 5 cases where it's the wrong tool&lt;/a&gt; — the honest version&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aiunplugged.in/blog/rag-production-faq/" rel="noopener noreferrer"&gt;RAG Production FAQ&lt;/a&gt; — 24 questions from real systems&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rag</category>
      <category>llm</category>
      <category>python</category>
      <category>embedding</category>
    </item>
    <item>
      <title>Build a RAG pipeline from scratch — the actually simple version</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Sat, 10 Oct 2026 17:10:17 +0000</pubDate>
      <link>https://dev.to/aiunplugged/build-a-rag-pipeline-from-scratch-the-actually-simple-version-gen</link>
      <guid>https://dev.to/aiunplugged/build-a-rag-pipeline-from-scratch-the-actually-simple-version-gen</guid>
      <description>&lt;p&gt;Originally published on &lt;a href="https://aiunplugged.in/blog/build-rag-pipeline-from-scratch/" rel="noopener noreferrer"&gt;aiunplugged.in&lt;/a&gt; — cross-posting for the Dev.to community.&lt;/p&gt;

&lt;p&gt;Every RAG tutorial online starts with LangChain, LlamaIndex, or a hosted vector database signup. None of that is necessary to understand what a RAG pipeline actually is. This walkthrough builds one in a single Python file with three libraries — no framework, no cloud accounts, no config files.&lt;/p&gt;

&lt;h2&gt;
  
  
  What RAG actually is
&lt;/h2&gt;

&lt;p&gt;RAG (Retrieval-Augmented Generation) is a pattern where an LLM answers a question using text pulled from a private document store at query time, not from what the model memorized during training. It exists because pretrained LLMs don't know a team's docs, a product's changelog, or last week's meeting notes, and fine-tuning every time the source changes is impractical.&lt;/p&gt;

&lt;h2&gt;
  
  
  When RAG is worth adding
&lt;/h2&gt;

&lt;p&gt;RAG makes sense when at least one of these is true:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Source data changes often (docs, tickets, chat logs, code)&lt;/li&gt;
&lt;li&gt;Source data is private (company internals, customer records)&lt;/li&gt;
&lt;li&gt;Source data is large — thousands of pages — and answers must cite specific passages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the entire dataset fits in a single 200K-token context window (roughly 500 pages) and doesn't change between requests, RAG is often overkill — sending the whole corpus with the question is simpler and sometimes cheaper. That trade-off is worth its own walkthrough, coming later in this series.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 4-step pipeline
&lt;/h2&gt;

&lt;p&gt;Every RAG system, regardless of framework or vendor, does some version of these four steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Chunk&lt;/strong&gt; — split source documents into smaller passages&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Embed&lt;/strong&gt; — convert each chunk into a vector (a list of floats)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store&lt;/strong&gt; — save chunks + vectors in a vector database&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retrieve + generate&lt;/strong&gt; — embed the user's question, find the closest chunks, hand them to an LLM&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything else — reranking, hybrid search, query rewriting, evaluation — is optional refinement on top of this loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Three libraries, one pip install:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;chromadb sentence-transformers anthropic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;chromadb&lt;/code&gt; — a small local vector database (no server, no signup, in-memory or disk-backed)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sentence-transformers&lt;/code&gt; — the embedding model, downloaded once on first run&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;anthropic&lt;/code&gt; — client for Claude, used for the answer step&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The embedding model (&lt;code&gt;all-MiniLM-L6-v2&lt;/code&gt;) has 22 million parameters (~90 MB download) and runs on CPU in milliseconds. The Claude client needs an API key from &lt;code&gt;console.anthropic.com&lt;/code&gt; — Haiku 4.5 is $1 per million input tokens, so a full test session costs cents.&lt;/p&gt;

&lt;p&gt;Set the key as an env var:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ANTHROPIC_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;sk-ant-...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1 — Chunk the documents
&lt;/h2&gt;

&lt;p&gt;A "chunk" is a small passage the embedding model can turn into one vector. Chunks that are too big (thousands of tokens) blur meaning across topics. Too small (single sentences) lose surrounding context. A good starting point: 200-400 words per chunk with a 40-word overlap between adjacent chunks.&lt;/p&gt;

&lt;p&gt;For this tutorial the source is a short list of imaginary product-doc passages held in a Python list:&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;docs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora API rate limits: free tier is 60 requests per minute. Paid tiers start at 600 rpm.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora API authentication uses a bearer token in the Authorization header.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora supports Python, Go, and Node.js SDKs. Community SDKs exist for Ruby and Rust.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora billing is per-request. Failed requests (5xx) are not charged. 4xx requests are charged.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora response latency: p50 is 120ms, p99 is 800ms across all regions.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora data residency: EU customers can pin storage to Frankfurt. US customers use us-east-1 by default.&lt;/span&gt;&lt;span class="sh"&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;Each entry is already a self-contained chunk, so no splitting is needed here. For real documents (Markdown files, PDFs, HTML pages), a character-based splitter works for a first pass:&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;chunk_text&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Split text into overlapping character windows. Cheap and predictable.&lt;/span&gt;&lt;span class="sh"&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="n"&gt;start&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;while&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;len&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="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;text&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&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;size&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;size&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;overlap&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;Character splitting is imperfect — it can cut mid-sentence — but for a first pipeline it beats spending an hour picking the "right" splitter. Better strategies (heading-aware, semantic, sentence-boundary) get their own deep-dive later in this series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 — Embed the chunks
&lt;/h2&gt;

&lt;p&gt;An embedding is a fixed-length vector that represents the meaning of a text passage. Chunks with similar meanings sit close together in vector space; unrelated chunks are far apart. That distance is the entire mechanism behind retrieval.&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sentence_transformers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SentenceTransformer&lt;/span&gt;

&lt;span class="n"&gt;embedder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SentenceTransformer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;all-MiniLM-L6-v2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;embeddings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;embedder&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;docs&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;tolist&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;all-MiniLM-L6-v2&lt;/code&gt; outputs 384-dimensional vectors. The &lt;code&gt;.tolist()&lt;/code&gt; call converts the NumPy array into plain Python lists so Chroma can serialize them.&lt;/p&gt;

&lt;p&gt;First run downloads the model to &lt;code&gt;~/.cache/huggingface/&lt;/code&gt; — a one-time 22 MB download.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3 — Store chunks and embeddings in a vector database
&lt;/h2&gt;

&lt;p&gt;Chroma runs in-process. &lt;code&gt;EphemeralClient()&lt;/code&gt; keeps everything in RAM (fine for tutorials and tests). &lt;code&gt;PersistentClient(path="./chroma_data")&lt;/code&gt; writes to disk and survives restarts.&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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;chromadb&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;chromadb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EphemeralClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;collection&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="nf"&gt;create_collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;aurora_docs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&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="p"&gt;[&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;doc_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&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="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;))],&lt;/span&gt;
    &lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;embeddings&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;embeddings&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;Every stored item needs a unique &lt;code&gt;id&lt;/code&gt;. Chroma keeps the raw document text alongside the embedding, so retrieval later returns both the vector match and the original text to feed to the LLM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4 — Retrieve, then generate the answer
&lt;/h2&gt;

&lt;p&gt;Retrieval is symmetric with storage: embed the user's question with the same model, ask Chroma for the closest N chunks, and hand them to the LLM as context.&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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;anthropic&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&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="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;q_embedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;embedder&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;question&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;tolist&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;query_embeddings&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;q_embedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;n_results&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;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&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;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;documents&lt;/span&gt;&lt;span class="sh"&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="n"&gt;llm&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="n"&gt;reply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llm&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;claude-haiku-4-5-20251001&lt;/span&gt;&lt;span class="sh"&gt;"&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;400&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Answer the question using ONLY the context below. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;If the answer is not in the context, say so.&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Context:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Question: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&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;reply&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="n"&gt;text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details worth pinning down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use the same embedding model for storage and retrieval.&lt;/strong&gt; Mixing models silently breaks similarity math — vectors from two different models are not directly comparable, and search results become noise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instruct the model to say "I don't know."&lt;/strong&gt; Without that clause, LLMs will invent an answer from prior training when retrieval misses. It is one of the cheapest reliability wins in RAG.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The full script
&lt;/h2&gt;

&lt;p&gt;Everything above, in one file (&lt;code&gt;rag.py&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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;chromadb&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;anthropic&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sentence_transformers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SentenceTransformer&lt;/span&gt;

&lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora API rate limits: free tier is 60 requests per minute. Paid tiers start at 600 rpm.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora API authentication uses a bearer token in the Authorization header.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora supports Python, Go, and Node.js SDKs. Community SDKs exist for Ruby and Rust.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora billing is per-request. Failed requests (5xx) are not charged. 4xx requests are charged.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora response latency: p50 is 120ms, p99 is 800ms across all regions.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Aurora data residency: EU customers can pin storage to Frankfurt. US customers use us-east-1 by default.&lt;/span&gt;&lt;span class="sh"&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;embedder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SentenceTransformer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;all-MiniLM-L6-v2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;embeddings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;embedder&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;docs&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;tolist&lt;/span&gt;&lt;span class="p"&gt;()&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;chromadb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EphemeralClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;collection&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="nf"&gt;create_collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;aurora_docs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&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="p"&gt;[&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;doc_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&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="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;))],&lt;/span&gt;
    &lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;embeddings&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;embeddings&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;answer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&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="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;q_embedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;embedder&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;question&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;tolist&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query_embeddings&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;q_embedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_results&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="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&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;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;documents&lt;/span&gt;&lt;span class="sh"&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="n"&gt;llm&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="n"&gt;reply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llm&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;claude-haiku-4-5-20251001&lt;/span&gt;&lt;span class="sh"&gt;"&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;400&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Answer the question using ONLY the context below. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;If the answer is not in the context, say so.&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Context:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s"&gt;Question: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&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;reply&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="n"&gt;text&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Are failed requests charged?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;No — failed requests that return a 5xx status are not charged. 4xx requests are charged.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is a complete RAG loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's still missing before this is production-grade
&lt;/h2&gt;

&lt;p&gt;The pipeline above works, but a production RAG system adds five things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A persistent vector store.&lt;/strong&gt; &lt;code&gt;EphemeralClient()&lt;/code&gt; loses data on restart. Swap for &lt;code&gt;PersistentClient(path=...)&lt;/code&gt;, or graduate to Qdrant, pgvector, or Pinecone when the collection grows past a few hundred thousand chunks. A comparison of vector databases is coming next in this series.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A smarter chunker.&lt;/strong&gt; Markdown-aware, heading-aware, or semantic splitters preserve context better than fixed-size character windows. A deep-dive on chunking strategies follows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metadata filtering.&lt;/strong&gt; Attaching &lt;code&gt;{"source": "changelog", "date": "2026-08-01"}&lt;/code&gt; to each chunk lets retrieval filter by tag or date range, not similarity alone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reranking.&lt;/strong&gt; Pull 20 candidates from the vector DB, then rerank them with a cross-encoder before sending the top 3 to the LLM. Cuts hallucinations and improves precision.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evaluation.&lt;/strong&gt; Something that measures whether the pipeline actually returned the right answer, not just "an" answer. Also getting its own article in this series.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything past those is optimization. The four-step loop above is the whole idea.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes at this stage
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Storing the whole document as one chunk.&lt;/strong&gt; Retrieval then returns the entire document every time, and the LLM ignores the middle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunking on newlines only.&lt;/strong&gt; A single paragraph in a well-formatted document can be 2000 words. Fix chunk size in characters or tokens, not lines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Using different embedding models for storage and query.&lt;/strong&gt; All similarity math becomes noise; results feel random.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sending the retrieved chunks without an instruction to only use them.&lt;/strong&gt; The LLM blends prior training with the new context and hallucinates confidently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Skipping the "I don't know" clause.&lt;/strong&gt; Guarantees a plausible-sounding wrong answer whenever retrieval misses.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;RAG is four steps: chunk, embed, store, retrieve+answer. Everything else — LangChain, LlamaIndex, hosted vector DBs, hybrid search, reranking, query rewriting — is scaffolding built on top of this loop. Once the version above works on real documents, layering the extras one at a time reveals exactly what each one changes and why.&lt;/p&gt;




&lt;p&gt;If you've worked through this and are now wrestling with the production side (chunking that doesn't lose context, picking vector DBs, modeling costs at scale), I'm writing a handbook on exactly that — ships next month. &lt;a href="https://aiunplugged.in/products/rag-production-handbook/" rel="noopener noreferrer"&gt;More here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>rag</category>
      <category>ai</category>
      <category>python</category>
      <category>llm</category>
    </item>
    <item>
      <title>Kubernetes Pods stuck in Pending — 8 causes and exact fixes</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Fri, 02 Oct 2026 18:45:41 +0000</pubDate>
      <link>https://dev.to/aiunplugged/kubernetes-pods-stuck-in-pending-8-causes-and-exact-fixes-23cg</link>
      <guid>https://dev.to/aiunplugged/kubernetes-pods-stuck-in-pending-8-causes-and-exact-fixes-23cg</guid>
      <description>&lt;p&gt;A Pod stuck in &lt;code&gt;Pending&lt;/code&gt; means Kubernetes cannot schedule it onto any node. The Pod is created but no container is running. Here are the 8 real causes ranked by frequency, with exact diagnostic commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  First — always run this
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl describe pod &amp;lt;pod-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scroll to the &lt;code&gt;Events:&lt;/code&gt; section at the bottom. The scheduler explains WHY it can't place the Pod. Every fix below starts from this event message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1 — Insufficient CPU or memory on nodes
&lt;/h2&gt;

&lt;p&gt;Event message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0/3 nodes are available: 3 Insufficient cpu.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Pod requests more resources than any node has free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl top nodes
kubectl describe nodes | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-A&lt;/span&gt; 5 &lt;span class="s2"&gt;"Allocated resources"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce Pod's &lt;code&gt;resources.requests.cpu&lt;/code&gt; / &lt;code&gt;memory&lt;/code&gt; if over-requested&lt;/li&gt;
&lt;li&gt;Scale cluster (add nodes)&lt;/li&gt;
&lt;li&gt;Free capacity by removing unused Deployments&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cause 2 — Node selector or affinity doesn't match
&lt;/h2&gt;

&lt;p&gt;Event message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0/3 nodes are available: 3 node(s) didn't match Pod's node affinity/selector.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pod requires nodes with specific labels but none have them. (The scheduler uses the same NodeAffinity plugin for both &lt;code&gt;nodeSelector&lt;/code&gt; and &lt;code&gt;nodeAffinity&lt;/code&gt;, hence the combined message.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl get nodes &lt;span class="nt"&gt;--show-labels&lt;/span&gt;
kubectl get pod &amp;lt;pod-name&amp;gt; &lt;span class="nt"&gt;-o&lt;/span&gt; yaml | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-A&lt;/span&gt; 5 nodeSelector
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add the required label to a node: &lt;code&gt;kubectl label node &amp;lt;node&amp;gt; disktype=ssd&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Or remove the &lt;code&gt;nodeSelector&lt;/code&gt; from Pod spec if not needed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cause 3 — Taints without matching tolerations
&lt;/h2&gt;

&lt;p&gt;Event message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0/3 nodes are available: 3 node(s) had untolerated taint.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nodes have taints (e.g. &lt;code&gt;NoSchedule&lt;/code&gt;) that Pod doesn't tolerate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl describe nodes | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-A&lt;/span&gt; 2 Taints
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add matching toleration to Pod spec:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;tolerations&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;special"&lt;/span&gt;
  &lt;span class="na"&gt;operator&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Equal"&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;true"&lt;/span&gt;
  &lt;span class="na"&gt;effect&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NoSchedule"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Or remove the taint from node: &lt;code&gt;kubectl taint node &amp;lt;node&amp;gt; special-&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cause 4 — PersistentVolumeClaim not bound
&lt;/h2&gt;

&lt;p&gt;Event message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;persistentvolumeclaim "data-pvc" not found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0/3 nodes are available: 3 pod has unbound immediate PersistentVolumeClaims.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pod references a PVC that doesn't exist or has no matching PV.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl get pvc
kubectl describe pvc &amp;lt;pvc-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create the missing PVC&lt;/li&gt;
&lt;li&gt;Check StorageClass exists: &lt;code&gt;kubectl get storageclass&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Verify dynamic provisioning is working (CSI driver installed)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cause 5 — Image pull secret missing (private registry)
&lt;/h2&gt;

&lt;p&gt;Event message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Failed to pull image: unauthorized
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pod's image is in a private registry, no credentials configured. &lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;kubectl get pods&lt;/code&gt; will show STATUS as &lt;code&gt;ErrImagePull&lt;/code&gt; or &lt;code&gt;ImagePullBackOff&lt;/code&gt; for this case — the Pod's &lt;code&gt;.status.phase&lt;/code&gt; is technically still &lt;code&gt;Pending&lt;/code&gt;, but readers watching the STATUS column won't see the word "Pending." Include this in your triage when a Pod visibly appears not Pending but is stuck.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl get pod &amp;lt;pod-name&amp;gt; &lt;span class="nt"&gt;-o&lt;/span&gt; yaml | &lt;span class="nb"&gt;grep &lt;/span&gt;imagePullSecrets
kubectl get secrets | &lt;span class="nb"&gt;grep &lt;/span&gt;regcred
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl create secret docker-registry regcred &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--docker-server&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;registry-url&amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--docker-username&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;user&amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--docker-password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;pass&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add to Pod spec:&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;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;imagePullSecrets&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;regcred&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cause 6 — Pod anti-affinity conflicts
&lt;/h2&gt;

&lt;p&gt;Event message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0/3 nodes are available: 3 node(s) didn't match pod anti-affinity rules.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pod has anti-affinity requiring separation from other Pods, but no eligible node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl get pod &amp;lt;pod-name&amp;gt; &lt;span class="nt"&gt;-o&lt;/span&gt; yaml | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-A&lt;/span&gt; 20 affinity
kubectl get pods &lt;span class="nt"&gt;-o&lt;/span&gt; wide
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add more nodes (so anti-affinity has room to spread)&lt;/li&gt;
&lt;li&gt;Relax anti-affinity from &lt;code&gt;requiredDuringScheduling&lt;/code&gt; to &lt;code&gt;preferredDuringScheduling&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cause 7 — Namespace ResourceQuota rejects Pod creation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Important distinction:&lt;/strong&gt; unlike causes 1-6 and 8, a ResourceQuota violation &lt;strong&gt;prevents the Pod object from being created at all&lt;/strong&gt;. The API server's admission controller rejects the create request with &lt;code&gt;403 Forbidden&lt;/code&gt;, so there's no Pod stuck in Pending — the Pod never exists. Include this in your triage when &lt;code&gt;kubectl create&lt;/code&gt; / &lt;code&gt;kubectl apply&lt;/code&gt; returns an error and no Pod appears in &lt;code&gt;kubectl get pods&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Error message (from the CLI, not the Pod events):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error from server (Forbidden): pods "foo" is forbidden: exceeded quota: &amp;lt;quota-name&amp;gt;, requested: ..., used: ..., limited: ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl describe quota &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;namespace&amp;gt;
kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; pod.yaml   &lt;span class="c"&gt;# observe the Forbidden error text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delete unused Pods in namespace to free quota&lt;/li&gt;
&lt;li&gt;Increase quota: &lt;code&gt;kubectl edit quota &amp;lt;quota-name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Deploy to a different namespace with headroom&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cause 8 — Scheduler broken or overloaded
&lt;/h2&gt;

&lt;p&gt;Rare, but happens on stressed clusters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl get pods &lt;span class="nt"&gt;-n&lt;/span&gt; kube-system | &lt;span class="nb"&gt;grep &lt;/span&gt;scheduler
kubectl logs &lt;span class="nt"&gt;-n&lt;/span&gt; kube-system &amp;lt;scheduler-pod-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Restart scheduler pod&lt;/li&gt;
&lt;li&gt;Check for scheduler configuration issues&lt;/li&gt;
&lt;li&gt;On managed K8s (EKS/GKE), this usually resolves itself&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The universal debug checklist
&lt;/h2&gt;

&lt;p&gt;Copy this and run through it every time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Basic status&lt;/span&gt;
kubectl get pod &amp;lt;pod-name&amp;gt;

&lt;span class="c"&gt;# Detailed events (THE most important)&lt;/span&gt;
kubectl describe pod &amp;lt;pod-name&amp;gt;

&lt;span class="c"&gt;# Node status&lt;/span&gt;
kubectl get nodes
kubectl describe nodes

&lt;span class="c"&gt;# Recent events across cluster&lt;/span&gt;
kubectl get events &lt;span class="nt"&gt;--sort-by&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'.lastTimestamp'&lt;/span&gt; | &lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-20&lt;/span&gt;

&lt;span class="c"&gt;# Related resources&lt;/span&gt;
kubectl get pvc
kubectl get storageclass
kubectl get secrets
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reproduce this yourself (browser sandbox)
&lt;/h2&gt;

&lt;p&gt;Free Kubernetes playground: &lt;a href="https://killercoda.com/playgrounds/scenario/kubernetes" rel="noopener noreferrer"&gt;https://killercoda.com/playgrounds/scenario/kubernetes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a pod that will be Pending:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl run stuck-pod &lt;span class="nt"&gt;--image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;nginx &lt;span class="nt"&gt;--overrides&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'{"apiVersion":"v1","spec":{"nodeSelector":{"nonexistent":"label"}}}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl describe pod stuck-pod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Screenshot the Events section. Note the "didn't match node selector" message.&lt;/p&gt;

&lt;p&gt;Clean up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl delete pod stuck-pod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Prevention
&lt;/h2&gt;

&lt;p&gt;Set sensible defaults from the start:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Resource requests&lt;/strong&gt; — not too high, not too low. Start with 100m CPU, 128Mi memory. Adjust based on actual usage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No &lt;code&gt;nodeSelector&lt;/code&gt;&lt;/strong&gt; unless truly needed. Default schedulers work well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test PVCs before deploying real workloads&lt;/strong&gt; — verify storage class works.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;kubectl top&lt;/code&gt;&lt;/strong&gt; regularly to see node capacity trends.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;Every Pending pod has a specific reason. The scheduler always tells you what it is via &lt;code&gt;kubectl describe&lt;/code&gt;. Match the event message to one of the 8 causes above. Apply the corresponding fix. Done in 5 minutes.&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>debugging</category>
      <category>devops</category>
      <category>k8</category>
    </item>
    <item>
      <title>Fix: kubectl exec fails with 'connection refused' — 5 real causes</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Sat, 26 Sep 2026 18:56:13 +0000</pubDate>
      <link>https://dev.to/aiunplugged/fix-kubectl-exec-fails-with-connection-refused-5-real-causes-1fim</link>
      <guid>https://dev.to/aiunplugged/fix-kubectl-exec-fails-with-connection-refused-5-real-causes-1fim</guid>
      <description>

&lt;p&gt;Trying to exec into a running pod and Kubernetes refuses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error: unable to upgrade connection: pod does not exist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error from server: error dialing backend: dial tcp 10.42.0.7:10250: connect: connection refused
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pod shows &lt;code&gt;Running&lt;/code&gt; in &lt;code&gt;kubectl get pods&lt;/code&gt;, yet exec doesn't work. Here are the real causes and how to fix each.&lt;/p&gt;

&lt;h2&gt;
  
  
  Diagnose first
&lt;/h2&gt;

&lt;p&gt;Before guessing, gather facts:&lt;/p&gt;

&lt;p&gt;For RBAC-related issues, also check what permissions the current user has:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Confirm pod is actually running&lt;/span&gt;
kubectl get pod &amp;lt;pod-name&amp;gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;namespace&amp;gt;

&lt;span class="c"&gt;# Check node the pod is on&lt;/span&gt;
kubectl get pod &amp;lt;pod-name&amp;gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;namespace&amp;gt; &lt;span class="nt"&gt;-o&lt;/span&gt; wide

&lt;span class="c"&gt;# Check the node's status&lt;/span&gt;
kubectl get node &amp;lt;node-name&amp;gt;

&lt;span class="c"&gt;# Check kubelet logs on the node (if you have SSH access)&lt;/span&gt;
journalctl &lt;span class="nt"&gt;-u&lt;/span&gt; kubelet &lt;span class="nt"&gt;-n&lt;/span&gt; 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact error message narrows down which of the 5 causes below applies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1 — kubelet not reachable from API server
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;kubectl exec&lt;/code&gt; requires the API server to open a tunnel to the kubelet on port 10250. If a firewall or security group blocks that port, exec fails while everything else works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symptom:&lt;/strong&gt; error like &lt;code&gt;dial tcp &amp;lt;node-ip&amp;gt;:10250: connect: connection refused&lt;/code&gt; or &lt;code&gt;i/o timeout&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cloud clusters (EKS/GKE/AKS): check that the control plane security group can reach worker nodes on 10250/TCP. On EKS, this is the &lt;code&gt;eksClusterSecurityGroup&lt;/code&gt; inbound rule on the node security group.&lt;/li&gt;
&lt;li&gt;Self-hosted: verify &lt;code&gt;iptables&lt;/code&gt; or &lt;code&gt;ufw&lt;/code&gt; allows 10250 from control plane nodes.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# On the node, verify kubelet is listening&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;ss &lt;span class="nt"&gt;-tlnp&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;10250

&lt;span class="c"&gt;# From the control plane, test connectivity&lt;/span&gt;
nc &lt;span class="nt"&gt;-zv&lt;/span&gt; &amp;lt;node-internal-ip&amp;gt; 10250
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the port is closed, add the firewall rule. If open but still failing, jump to cause 2.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2 — kubelet is running but crashed the streaming server
&lt;/h2&gt;

&lt;p&gt;kubelet has a separate HTTPS server for exec/attach/logs. It sometimes crashes without taking down the whole kubelet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symptom:&lt;/strong&gt; &lt;code&gt;logs&lt;/code&gt; works but &lt;code&gt;exec&lt;/code&gt; and &lt;code&gt;attach&lt;/code&gt; fail. Or intermittent failures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix — restart kubelet:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart kubelet
&lt;span class="nb"&gt;sudo &lt;/span&gt;journalctl &lt;span class="nt"&gt;-u&lt;/span&gt; kubelet &lt;span class="nt"&gt;-f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Watch for errors during startup. If kubelet keeps crashing, the underlying issue is often disk pressure, memory pressure, or a stale container runtime socket.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3 — pod is Running but container isn't ready for TTY
&lt;/h2&gt;

&lt;p&gt;Distroless containers, scratch images, or containers without a shell installed have no &lt;code&gt;/bin/sh&lt;/code&gt; — exec fails immediately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symptom:&lt;/strong&gt; &lt;code&gt;OCI runtime exec failed: exec failed: unable to start container process: exec: "sh": executable file not found in $PATH&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix — use an ephemeral debug container (Kubernetes 1.23+):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl debug &lt;span class="nt"&gt;-it&lt;/span&gt; &amp;lt;pod-name&amp;gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;namespace&amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;busybox:1.36 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;container-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This attaches a debug container to the same pod's namespace, giving you a shell that CAN inspect the target container's processes, network, and mounts. Works even for distroless.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4 — RBAC blocking your user from exec
&lt;/h2&gt;

&lt;p&gt;You can list pods but not exec into them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symptom:&lt;/strong&gt; &lt;code&gt;Error from server (Forbidden): pods "&amp;lt;name&amp;gt;" is forbidden: User "&amp;lt;you&amp;gt;" cannot create resource "pods/exec"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl auth can-i create pods/exec &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;namespace&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it returns &lt;code&gt;no&lt;/code&gt;, your Role or ClusterRole is missing the &lt;code&gt;pods/exec&lt;/code&gt; sub-resource.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix — grant exec permission:&lt;/strong&gt;&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;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rbac.authorization.k8s.io/v1&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Role&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pod-exec&lt;/span&gt;
  &lt;span class="na"&gt;namespace&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;YOUR_NAMESPACE&lt;/span&gt;
&lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;apiGroups&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pods/exec"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;verbs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;create"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;apiGroups&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pods"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;verbs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;get"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;list"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bind it to your user with a &lt;code&gt;RoleBinding&lt;/code&gt; and retry.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 5 — kubectl version mismatch with cluster
&lt;/h2&gt;

&lt;p&gt;Very old kubectl talking to a much newer cluster (or vice versa) can hit exec-protocol incompatibilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symptom:&lt;/strong&gt; exec starts, prints a partial banner, then disconnects. Or &lt;code&gt;error: unable to upgrade connection: unauthorized&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default output shows both client and server versions. Kubernetes officially supports client/server skew of +/- 1 minor version. Beyond that, exec is unreliable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix — align kubectl version:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# macOS&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;kubernetes-cli
&lt;span class="c"&gt;# or pinned to a specific minor version:&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;kubernetes-cli@1.29

&lt;span class="c"&gt;# Linux — pinned install&lt;/span&gt;
curl &lt;span class="nt"&gt;-LO&lt;/span&gt; &lt;span class="s2"&gt;"https://dl.k8s.io/release/v1.29.0/bin/linux/amd64/kubectl"&lt;/span&gt;
&lt;span class="nb"&gt;sudo install&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; root &lt;span class="nt"&gt;-g&lt;/span&gt; root &lt;span class="nt"&gt;-m&lt;/span&gt; 0755 kubectl /usr/local/bin/kubectl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Match kubectl to your cluster's minor version and retry.&lt;/p&gt;

&lt;h2&gt;
  
  
  The universal exec debug flow
&lt;/h2&gt;

&lt;p&gt;Every stuck-exec case, run in order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Pod actually running?&lt;/span&gt;
kubectl get pod &amp;lt;pod&amp;gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;ns&amp;gt;

&lt;span class="c"&gt;# 2. Node healthy?&lt;/span&gt;
kubectl get node &lt;span class="nt"&gt;-o&lt;/span&gt; wide

&lt;span class="c"&gt;# 3. Can I even see logs? (rules out kubelet-total-failure)&lt;/span&gt;
kubectl logs &amp;lt;pod&amp;gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;ns&amp;gt; &lt;span class="nt"&gt;--tail&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5

&lt;span class="c"&gt;# 4. RBAC permission for exec?&lt;/span&gt;
kubectl auth can-i create pods/exec &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;ns&amp;gt;

&lt;span class="c"&gt;# 5. Try debug container (bypasses distroless issue)&lt;/span&gt;
kubectl debug &lt;span class="nt"&gt;-it&lt;/span&gt; &amp;lt;pod&amp;gt; &lt;span class="nt"&gt;--image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;busybox &lt;span class="nt"&gt;--target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;container&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ninety percent of exec failures resolve after step 5.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prevention
&lt;/h2&gt;

&lt;p&gt;Bake these into every cluster setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explicit security group rules&lt;/strong&gt; for control-plane to node on 10250/TCP; documented in Terraform, not vibes-based&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ephemeral containers enabled&lt;/strong&gt; on all managed clusters (default in K8s 1.25+; verify with &lt;code&gt;kubectl api-resources | grep ephemeral&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standardized debug image&lt;/strong&gt; in a private registry — a common &lt;code&gt;debug:latest&lt;/code&gt; with curl, bind-utils, netcat, iputils pre-installed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RBAC role called &lt;code&gt;debugger&lt;/code&gt;&lt;/strong&gt; with pods/exec permission, granted temporarily via &lt;code&gt;kubectl auth reconcile&lt;/code&gt; for incident response&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regular kubectl version audits&lt;/strong&gt; across the team — pin to cluster version in your dotfiles&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;kubectl exec&lt;/code&gt; failing usually points at one of five layers: network to kubelet, kubelet streaming server, missing shell in the container, RBAC, or version skew. Diagnose with &lt;code&gt;kubectl get pod -o wide&lt;/code&gt;, &lt;code&gt;kubectl auth can-i create pods/exec&lt;/code&gt;, and &lt;code&gt;kubectl debug&lt;/code&gt;. The debug container is a strong Swiss army knife — reach for it early rather than fighting distroless exec issues directly.&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
      <category>kubectl</category>
      <category>troubleshooting</category>
    </item>
    <item>
      <title>Claude API vs OpenAI API for developers — 2026 practical comparison</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Fri, 18 Sep 2026 12:54:37 +0000</pubDate>
      <link>https://dev.to/aiunplugged/claude-api-vs-openai-api-for-developers-2026-practical-comparison-22c0</link>
      <guid>https://dev.to/aiunplugged/claude-api-vs-openai-api-for-developers-2026-practical-comparison-22c0</guid>
      <description>&lt;p&gt;This post was originally published on &lt;a href="https://aiunplugged.in/blog/claude-api-vs-openai-api-developers/" rel="noopener noreferrer"&gt;aiunplugged.in&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Two dominant APIs power most 2026 AI apps: Anthropic's Claude and OpenAI's GPT. Both are excellent. The right choice depends on specific use cases. Here is a practical comparison based on daily developer use, with current 2026 model families and pricing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 30-second summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Claude&lt;/strong&gt; — better at long-form reasoning, careful code, safety-critical outputs, honest limits&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenAI&lt;/strong&gt; — better at general-purpose tasks, mature ecosystem, plugins, image generation&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Both&lt;/strong&gt; — production-ready, well-documented, actively improved&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For code + reasoning tasks, Claude tends to win in 2026. For general apps, either works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model comparison (2026)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Claude models (Anthropic)
&lt;/h3&gt;

&lt;p&gt;The current Claude 5 family plus Haiku 4.5:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Claude Fable 5.1&lt;/strong&gt; — newest release, high-capability with faster response times&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Claude Opus 5&lt;/strong&gt; — most capable for deep reasoning, slower, most expensive&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Claude Sonnet 5&lt;/strong&gt; — balanced default recommendation, strong on code and reasoning&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Claude Haiku 4.5&lt;/strong&gt; — fast, cheap, still highly capable for lighter tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  OpenAI models
&lt;/h3&gt;

&lt;p&gt;The GPT-5.6 family is OpenAI's current flagship, with older GPT-5 and GPT-5.4 still available:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GPT-5.6-sol&lt;/strong&gt; — flagship, most capable&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT-5.6-terra&lt;/strong&gt; — mid-tier, balanced cost/quality&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT-5.6-luna&lt;/strong&gt; — cheapest of the flagship family, high volume&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT-5.4&lt;/strong&gt; — previous generation, still supported&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT-5&lt;/strong&gt; — original GPT-5, cheapest input pricing in the modern lineup&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pricing per million tokens (verified Sep 2026)
&lt;/h2&gt;

&lt;p&gt;Input / Output pricing (USD per million tokens):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Claude Opus 5&lt;/td&gt;
&lt;td&gt;$5&lt;/td&gt;
&lt;td&gt;$25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Sonnet 5&lt;/td&gt;
&lt;td&gt;$2 - $3&lt;/td&gt;
&lt;td&gt;$10 - $15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Haiku 4.5&lt;/td&gt;
&lt;td&gt;$1&lt;/td&gt;
&lt;td&gt;$5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-5.6-sol&lt;/td&gt;
&lt;td&gt;$5&lt;/td&gt;
&lt;td&gt;$30&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-5.6-terra&lt;/td&gt;
&lt;td&gt;$2&lt;/td&gt;
&lt;td&gt;$12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-5.6-luna&lt;/td&gt;
&lt;td&gt;$0.20&lt;/td&gt;
&lt;td&gt;$1.20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-5.4&lt;/td&gt;
&lt;td&gt;$2.50&lt;/td&gt;
&lt;td&gt;$15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-5&lt;/td&gt;
&lt;td&gt;$0.625&lt;/td&gt;
&lt;td&gt;$5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sonnet 5 launched at $2/$10 as introductory pricing; sources report the standard rate as either $2/$10 or $3/$15 depending on when tracked&lt;/li&gt;
&lt;li&gt;OpenAI: batch mode halves rates, fast mode doubles them, long-context (&amp;gt;272K tokens) bills roughly double&lt;/li&gt;
&lt;li&gt;Cached input gets ~90% discount on GPT-5.6 and GPT-5; 50-75% on legacy models&lt;/li&gt;
&lt;li&gt;Prices change frequently. Always verify current pricing at anthropic.com/pricing and openai.com/pricing before committing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cheapest quality option:&lt;/strong&gt; GPT-5.6-luna for high volume, Claude Haiku 4.5 for balance. GPT-5 at $0.625 input is the cheapest input-heavy option in the modern lineup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where Claude wins
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Long context handling
&lt;/h3&gt;

&lt;p&gt;Claude Sonnet 5 and Opus 5 handle 200K tokens (~500 pages) effectively. Their recall over long context is measurably better than comparable OpenAI models at similar sizes.&lt;/p&gt;

&lt;p&gt;Use for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Analyzing entire codebases&lt;/li&gt;
&lt;li&gt;Processing long documents&lt;/li&gt;
&lt;li&gt;Multi-turn conversations that accumulate&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Careful code output
&lt;/h3&gt;

&lt;p&gt;Claude tends to be more precise on code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fewer hallucinated APIs&lt;/li&gt;
&lt;li&gt;Better adherence to instructions&lt;/li&gt;
&lt;li&gt;More self-aware ("I'm not sure about X" instead of confident wrong answers)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refactoring&lt;/li&gt;
&lt;li&gt;Debugging&lt;/li&gt;
&lt;li&gt;Code review&lt;/li&gt;
&lt;li&gt;Complex logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Following complex instructions
&lt;/h3&gt;

&lt;p&gt;Claude is more likely to follow detailed multi-step instructions accurately.&lt;/p&gt;

&lt;p&gt;Use for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Agentic workflows&lt;/li&gt;
&lt;li&gt;Multi-step pipelines&lt;/li&gt;
&lt;li&gt;Structured output requirements&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Honest uncertainty
&lt;/h3&gt;

&lt;p&gt;When Claude doesn't know something, it more often says so. OpenAI models tend to answer confidently even when wrong.&lt;/p&gt;

&lt;p&gt;Use for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fact-critical outputs&lt;/li&gt;
&lt;li&gt;User-facing apps where wrong answers cause harm&lt;/li&gt;
&lt;li&gt;Safety-important use cases&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where OpenAI wins
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Multimodal (image + audio)
&lt;/h3&gt;

&lt;p&gt;The GPT-5.6 family handles images, audio, and text in a single call. Voice mode is polished.&lt;/p&gt;

&lt;p&gt;Use for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image analysis apps&lt;/li&gt;
&lt;li&gt;Voice-driven apps&lt;/li&gt;
&lt;li&gt;Screenshot understanding&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Ecosystem maturity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;More libraries, examples, SDKs&lt;/li&gt;
&lt;li&gt;More Stack Overflow answers&lt;/li&gt;
&lt;li&gt;More third-party tools built on OpenAI first&lt;/li&gt;
&lt;li&gt;Assistants API (built-in state, tools, threads)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prototyping with existing tools&lt;/li&gt;
&lt;li&gt;When leveraging community examples matters&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Image generation (DALL-E)
&lt;/h3&gt;

&lt;p&gt;Native image generation via API. Claude doesn't offer image generation.&lt;/p&gt;

&lt;p&gt;Use for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image gen features&lt;/li&gt;
&lt;li&gt;Combined text + image workflows&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Streaming reliability
&lt;/h3&gt;

&lt;p&gt;Both stream. OpenAI's streaming has slightly better tooling support (more libraries).&lt;/p&gt;

&lt;p&gt;Use for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chat UIs needing token-by-token streaming&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practical patterns
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Building a chat interface
&lt;/h3&gt;

&lt;p&gt;Both work. Choice comes down to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model quality per dollar (Claude Sonnet 5 currently strong for reasoning quality)&lt;/li&gt;
&lt;li&gt;Streaming library support (both fine)&lt;/li&gt;
&lt;li&gt;Tool use / function calling (both mature)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code generation for developer tools
&lt;/h3&gt;

&lt;p&gt;Claude Sonnet 5 or Opus 5. Consistently better code quality with fewer hallucinated APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summarization
&lt;/h3&gt;

&lt;p&gt;Both. GPT-5.6-luna is cheapest. Claude Haiku 4.5 is competitive.&lt;/p&gt;

&lt;h3&gt;
  
  
  RAG (Retrieval-Augmented Generation)
&lt;/h3&gt;

&lt;p&gt;Claude's long context wins for RAG where you stuff many documents into context. Better recall.&lt;/p&gt;

&lt;p&gt;For traditional RAG (small chunks retrieved, then generation), both work equally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Agentic workflows
&lt;/h3&gt;

&lt;p&gt;Claude's better instruction-following makes it easier to build reliable agents. OpenAI Assistants API provides more built-in scaffolding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Safety-critical applications
&lt;/h3&gt;

&lt;p&gt;Claude has stronger built-in safety training. Refuses harmful requests more reliably. Better for consumer-facing products.&lt;/p&gt;

&lt;h2&gt;
  
  
  API differences that matter
&lt;/h2&gt;

&lt;h3&gt;
  
  
  System prompts
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Claude:&lt;/strong&gt; dedicated &lt;code&gt;system&lt;/code&gt; parameter separate from messages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"model"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"claude-sonnet-5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"system"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"You are a helpful assistant."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"messages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="nl"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OpenAI:&lt;/strong&gt; system message inside messages array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"model"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"gpt-5.6-sol"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"messages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"system"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"You are a helpful assistant."&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Tool use / function calling
&lt;/h3&gt;

&lt;p&gt;Both support tool calling with similar patterns. Claude's tool use is more forgiving of schema variations. OpenAI's is stricter but has better docs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Streaming
&lt;/h3&gt;

&lt;p&gt;Both use Server-Sent Events. Response formats differ slightly. All popular SDKs abstract this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prompt caching
&lt;/h3&gt;

&lt;p&gt;Both offer prompt caching to reduce cost on repeated inputs. Different implementations:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Claude:&lt;/strong&gt; &lt;code&gt;cache_control&lt;/code&gt; on message blocks, 5-minute default TTL&lt;br&gt;
&lt;strong&gt;OpenAI:&lt;/strong&gt; automatic caching on longer prompts (~500+ tokens); ~90% discount on GPT-5.6 cached input&lt;/p&gt;

&lt;p&gt;For repeated system prompts, both cut costs significantly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which SDK to use
&lt;/h2&gt;

&lt;p&gt;Both providers offer official SDKs:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Claude:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python: &lt;code&gt;pip install anthropic&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;JavaScript: &lt;code&gt;npm install @anthropic-ai/sdk&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;OpenAI:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python: &lt;code&gt;pip install openai&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;JavaScript: &lt;code&gt;npm install openai&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both are well-maintained, well-documented, and stable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rate limits
&lt;/h2&gt;

&lt;p&gt;Both providers have tier-based rate limits that scale with spend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tier 1&lt;/strong&gt; (new accounts): low limits, may hit quickly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Higher tiers:&lt;/strong&gt; unlock automatically as you spend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Neither is friendlier than the other on rate limits. If building high-volume, budget for tier progression.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use both
&lt;/h2&gt;

&lt;p&gt;Some applications benefit from using both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Primary:&lt;/strong&gt; Claude for reasoning-heavy tasks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fallback:&lt;/strong&gt; OpenAI if Claude is rate-limited or down&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specialized:&lt;/strong&gt; OpenAI DALL-E for images, Claude for text&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A/B testing:&lt;/strong&gt; compare quality on your specific tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;LiteLLM&lt;/strong&gt; or &lt;strong&gt;OpenRouter&lt;/strong&gt; provide unified interfaces to both (and other providers). Useful for multi-model apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing quality for your use case
&lt;/h2&gt;

&lt;p&gt;Do not rely on benchmarks. Test on YOUR actual prompts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pick 10 real prompts from your use case&lt;/li&gt;
&lt;li&gt;Run each in Claude Sonnet 5, GPT-5.6-sol, and a cheaper option (Haiku 4.5 or GPT-5.6-luna)&lt;/li&gt;
&lt;li&gt;Grade output quality subjectively&lt;/li&gt;
&lt;li&gt;Note cost and latency&lt;/li&gt;
&lt;li&gt;Pick winner based on quality/cost balance&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This 30-minute test beats reading 100 comparison articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproduce this yourself
&lt;/h2&gt;

&lt;p&gt;Get free credits to try both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Anthropic&lt;/strong&gt; — sign up at console.anthropic.com; check current signup credit offers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenAI&lt;/strong&gt; — sign up at platform.openai.com; check current signup credit offers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or use browser interfaces (no API required for testing):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Claude.ai&lt;/strong&gt; — free tier available&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ChatGPT&lt;/strong&gt; — free tier available&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Test the same prompt in both. Note which handles your specific case better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recommendation by profile
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Building a code assistant / copilot:&lt;/strong&gt; Claude Sonnet 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chat app for general users:&lt;/strong&gt; either (OpenAI edges on ecosystem)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAG on long documents:&lt;/strong&gt; Claude Sonnet 5 (200K context)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-modal image analysis app:&lt;/strong&gt; GPT-5.6-sol&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image generation feature:&lt;/strong&gt; OpenAI (Claude doesn't offer)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Safety-critical consumer product:&lt;/strong&gt; Claude (better refusals)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Voice AI:&lt;/strong&gt; OpenAI (native voice mode)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agentic workflow:&lt;/strong&gt; Claude (better instruction-following)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cheapest possible for high volume:&lt;/strong&gt; GPT-5.6-luna or GPT-5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Best free-tier interface:&lt;/strong&gt; both are good&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;Neither Claude nor OpenAI is universally better. Both are excellent APIs in 2026.&lt;/p&gt;

&lt;p&gt;For most developer-focused apps, Claude Sonnet 5 is the current best-in-class for quality-per-cost. For general apps needing multimodal or image gen, OpenAI wins.&lt;/p&gt;

&lt;p&gt;Test on your specific use case. Do not choose based on marketing or benchmarks. And re-verify pricing before committing to production — this space moves monthly.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>claude</category>
      <category>openai</category>
      <category>api</category>
    </item>
    <item>
      <title>AWS Free Tier in 2026 — what actually stays free</title>
      <dc:creator>aiunplugged</dc:creator>
      <pubDate>Sat, 12 Sep 2026 16:35:21 +0000</pubDate>
      <link>https://dev.to/aiunplugged/aws-free-tier-in-2026-what-actually-stays-free-5bcp</link>
      <guid>https://dev.to/aiunplugged/aws-free-tier-in-2026-what-actually-stays-free-5bcp</guid>
      <description>&lt;p&gt;AWS Free Tier changed significantly on July 15, 2025 — the old "12-month free trial" model was replaced for new accounts with a 6-month plan plus credits. Existing accounts kept the legacy tier. On top of that, AWS began charging for public IPv4 addresses in Feb 2024. If your article on AWS Free Tier is older than that, its numbers are probably outdated. This guide covers the 2026 reality.&lt;/p&gt;

&lt;h2&gt;
  
  
  The two versions of AWS Free Tier that exist today
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Legacy (accounts created before July 15, 2025):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traditional 12-month free tier still applies&lt;/li&gt;
&lt;li&gt;Access to Always Free services with monthly limits&lt;/li&gt;
&lt;li&gt;Nothing changed for these accounts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;New AWS Free Plan (accounts created on or after July 15, 2025):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;100 USD credits at signup, up to 200 USD through onboarding activities&lt;/li&gt;
&lt;li&gt;Plan lasts 6 months OR until credits are exhausted, whichever comes first&lt;/li&gt;
&lt;li&gt;Account auto-closes when the plan ends&lt;/li&gt;
&lt;li&gt;Also includes Always Free services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Always Free services (both plans):&lt;/strong&gt; 30+ AWS services stay free forever within monthly usage limits, regardless of which plan you're on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Always Free services — free forever within limits
&lt;/h2&gt;

&lt;p&gt;These services stay free regardless of account age or plan type, subject to monthly usage limits.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compute
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lambda&lt;/strong&gt; — 1 million requests + 400,000 GB-seconds compute per month&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DynamoDB&lt;/strong&gt; — 25 GB storage + 25 read/write capacity units&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SNS / SQS&lt;/strong&gt; — 1 million requests per month&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Functions&lt;/strong&gt; — 4,000 state transitions per month&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Networking
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CloudFront&lt;/strong&gt; — 1 TB data transfer + 10 million requests per month&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  AI / ML
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rekognition&lt;/strong&gt; — 5,000 image analyses per month (first 12 months)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comprehend&lt;/strong&gt; — 50,000 units of text per month (first 12 months)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Polly&lt;/strong&gt; — 5 million standard characters per month (first 12 months)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Monitoring
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CloudWatch&lt;/strong&gt; — 10 custom metrics, 5 GB log ingestion, 1 million API requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;X-Ray&lt;/strong&gt; — 100,000 traces per month&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storage
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Glacier&lt;/strong&gt; — 10 GB retrieval per month (data storage costs extra)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Always Free service limits are per AWS account, not per region. Once you exceed the monthly quota, standard billing applies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Legacy 12-month tier (pre-July 2025 accounts only)
&lt;/h2&gt;

&lt;p&gt;If your account was created before July 15, 2025, the traditional 12-month free tier still applies to these services. After 12 months, standard rates kick in.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;EC2&lt;/strong&gt; — 750 hours/month of t2.micro or t3.micro&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;S3&lt;/strong&gt; — 5 GB standard storage + 20,000 GET + 2,000 PUT requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RDS&lt;/strong&gt; — 750 hours/month of db.t2.micro / db.t3.micro&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EBS&lt;/strong&gt; — 30 GB storage + 2 million I/O&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Elastic Load Balancer&lt;/strong&gt; — 750 hours/month&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ElastiCache&lt;/strong&gt; — 750 hours/month of cache.t3.micro&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Transfer&lt;/strong&gt; — 15 GB out per month&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Route 53&lt;/strong&gt; — first hosted zone free for 12 months (0.50 USD/month per zone after)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Gateway&lt;/strong&gt; — 1 million API calls per month (first 12 months)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Real cost after 12 months:&lt;/strong&gt; if leaving these running, a small always-on EC2 + RDS + ELB stack costs around 40 to 80 USD per month.&lt;/p&gt;

&lt;h2&gt;
  
  
  New AWS Free Plan (post-July 2025 accounts)
&lt;/h2&gt;

&lt;p&gt;New accounts don't get a 12-month allowance. Instead:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with 100 USD credits&lt;/li&gt;
&lt;li&gt;Complete onboarding activities (up to 100 USD more)&lt;/li&gt;
&lt;li&gt;Credits expire 12 months from account creation&lt;/li&gt;
&lt;li&gt;Free Plan status ends after 6 months or when credits hit zero, whichever comes first&lt;/li&gt;
&lt;li&gt;Account auto-closes at that point unless you upgrade to a Paid plan&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can use those credits on any AWS service, so it's more flexible than the old fixed 750-hour allocations. But the runway is shorter — most experimentation projects that used to fit in 12 months now need to fit in 6.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hidden costs that surprise everyone
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Public IPv4 addresses — charged since Feb 2024
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;This is the biggest change most guides miss.&lt;/strong&gt; Since February 1, 2024, AWS charges 0.005 USD/hour (~3.60 USD/month) for every public IPv4 address, whether attached to a running EC2 instance or sitting idle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Free tier exception:&lt;/strong&gt; legacy free tier accounts (pre-July 2025) get 750 hours/month of public IPv4 usage free for the first 12 months.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What this means practically:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every running EC2 with a public IP costs at least 3.60 USD/month beyond free tier&lt;/li&gt;
&lt;li&gt;NAT Gateways, Load Balancers, and VPN connections with public IPs — all charged&lt;/li&gt;
&lt;li&gt;New accounts on the AWS Free Plan don't get the 750-hour IPv4 exemption&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Data transfer OUT
&lt;/h3&gt;

&lt;p&gt;The second most common surprise. Free tier includes limited outbound bandwidth. Beyond that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Internet egress: 0.09 USD/GB (US regions), 0.12 USD/GB (many others)&lt;/li&gt;
&lt;li&gt;Cross-region: 0.02 USD/GB&lt;/li&gt;
&lt;li&gt;Cross-AZ within same region: 0.01 USD/GB per direction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A hobbyist blog serving 1 TB/month costs 90 USD or more just in bandwidth.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. RDS storage
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;RDS free tier includes 20 GB storage (legacy accounts only)&lt;/li&gt;
&lt;li&gt;Beyond that: 0.115 USD/GB/month for gp2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backup storage&lt;/strong&gt; — first 100% of allocated is free, additional charged&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. NAT Gateway
&lt;/h3&gt;

&lt;p&gt;Popular for private subnets. Costs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0.045 USD/hour (~32 USD/month per gateway)&lt;/li&gt;
&lt;li&gt;0.045 USD/GB data processed&lt;/li&gt;
&lt;li&gt;Plus 0.005 USD/hour for its public IPv4 address&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not in free tier at all. Common surprise.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Support plans
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Basic support: free&lt;/li&gt;
&lt;li&gt;Developer support: 29 USD/month minimum&lt;/li&gt;
&lt;li&gt;Business support: 100 USD/month or 3-10% of AWS bill&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Free tier includes only Basic support.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up billing alerts (critical)
&lt;/h2&gt;

&lt;p&gt;Never skip this step. Set alerts BEFORE deploying anything.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AWS Console → Billing → Billing preferences&lt;/li&gt;
&lt;li&gt;Enable "Receive Free Tier Usage Alerts"&lt;/li&gt;
&lt;li&gt;Enter your email&lt;/li&gt;
&lt;li&gt;Go to CloudWatch → Alarms → Create Alarm&lt;/li&gt;
&lt;li&gt;Metric: EstimatedCharges (in USD)&lt;/li&gt;
&lt;li&gt;Set threshold: 5, 10, 25 USD (three alarms recommended)&lt;/li&gt;
&lt;li&gt;Send notification to your email&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now if usage spikes, you get emails before the bill arrives.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to avoid on free tier
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Don't run these on always-on free tier:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NAT Gateway&lt;/strong&gt; — expensive, not free&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple availability zone deployments&lt;/strong&gt; — cross-AZ transfer charges&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CloudTrail data events&lt;/strong&gt; — logging every S3 object access&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Detailed monitoring&lt;/strong&gt; — 1-minute EC2 metrics cost extra&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;VPC endpoints&lt;/strong&gt; (7.30 USD/month per endpoint)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple public IPv4 addresses&lt;/strong&gt; — each one is 3.60 USD/month post-2024&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Realistic free/cheap stack for a personal project
&lt;/h2&gt;

&lt;p&gt;Given the 2024 IPv4 charges and the 2025 Free Tier restructure, here's what actually stays cheap for a personal project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option A — Static Website (essentially free)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;S3 for storage&lt;/li&gt;
&lt;li&gt;CloudFront for CDN&lt;/li&gt;
&lt;li&gt;Route 53 for DNS (0.50 USD/month after 12 months)&lt;/li&gt;
&lt;li&gt;Certificate Manager for SSL (free)&lt;/li&gt;
&lt;li&gt;Total: essentially 0 USD for personal traffic (CloudFront's 1 TB always-free covers most personal blogs)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Option B — Full Stack Web App (legacy accounts only)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1x t3.micro EC2 (running Docker/Node) — free for 12 months, includes 750 hrs public IPv4&lt;/li&gt;
&lt;li&gt;1x db.t3.micro RDS — free for 12 months&lt;/li&gt;
&lt;li&gt;5 GB S3&lt;/li&gt;
&lt;li&gt;CloudFront in front&lt;/li&gt;
&lt;li&gt;Total: around 0 USD for first 12 months, then 40 to 80 USD/month + IPv4 charges&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Option C — Serverless (works on both plan types)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lambda for compute (1M requests always free)&lt;/li&gt;
&lt;li&gt;DynamoDB for storage (25 GB always free)&lt;/li&gt;
&lt;li&gt;API Gateway for HTTP endpoints (first 12 months free, then 3.50 USD per million calls)&lt;/li&gt;
&lt;li&gt;CloudFront for CDN (1 TB always free)&lt;/li&gt;
&lt;li&gt;Total: often 0 USD for hobbyist traffic, indefinitely&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Serverless is the best free-tier fit today because it avoids the always-on EC2 costs and the IPv4 charge entirely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternatives worth comparing
&lt;/h2&gt;

&lt;p&gt;For personal projects, AWS free tier is not always the best deal. Verified 2026 limits:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Provider&lt;/th&gt;
&lt;th&gt;Free Compute&lt;/th&gt;
&lt;th&gt;Free Bandwidth&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AWS (Always Free)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Lambda 1M req/month&lt;/td&gt;
&lt;td&gt;15 GB egress + CloudFront 1 TB&lt;/td&gt;
&lt;td&gt;Always Free services never expire&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AWS (Legacy 12-mo)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;750 hrs t3.micro&lt;/td&gt;
&lt;td&gt;15 GB egress&lt;/td&gt;
&lt;td&gt;Pre-July 2025 accounts only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AWS (New Free Plan)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;100 to 200 USD credits&lt;/td&gt;
&lt;td&gt;Included in credits&lt;/td&gt;
&lt;td&gt;6 months or credit exhaustion&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Cloudflare Pages&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Unlimited static requests&lt;/td&gt;
&lt;td&gt;Unlimited (unmetered)&lt;/td&gt;
&lt;td&gt;500 builds/month; Functions billed via Workers plan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Vercel Hobby&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1M function invocations&lt;/td&gt;
&lt;td&gt;100 GB/month&lt;/td&gt;
&lt;td&gt;Also 1M edge requests, 4 CPU hours&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Netlify (legacy)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;300 build minutes&lt;/td&gt;
&lt;td&gt;100 GB/month&lt;/td&gt;
&lt;td&gt;Pre-Sep 2025 accounts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Netlify (new)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;300 credits/month&lt;/td&gt;
&lt;td&gt;Credits shared with bandwidth&lt;/td&gt;
&lt;td&gt;Post-Sep 2025 accounts, unified pool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Oracle Cloud&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;4 vCPU + 24 GB RAM ARM&lt;/td&gt;
&lt;td&gt;10 TB egress&lt;/td&gt;
&lt;td&gt;Always Free tier (verify with Oracle)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Google Cloud&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1 e2-micro&lt;/td&gt;
&lt;td&gt;200 GB egress&lt;/td&gt;
&lt;td&gt;Always Free, region-restricted&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Fly.io was previously listed as having a free tier. As of late 2024, Fly.io no longer offers a free tier — new users pay from usage.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For blogs and small static sites in 2026, Cloudflare Pages or Vercel Hobby typically beat AWS on both cost and simplicity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproduce this yourself (safely)
&lt;/h2&gt;

&lt;p&gt;Sign up for AWS free tier: &lt;a href="https://aws.amazon.com/free" rel="noopener noreferrer"&gt;https://aws.amazon.com/free&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before deploying anything:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Complete the billing alert setup above&lt;/li&gt;
&lt;li&gt;Never enter production credit card if just experimenting — use a low-limit card&lt;/li&gt;
&lt;li&gt;Delete resources after testing to avoid accidental charges&lt;/li&gt;
&lt;li&gt;Note whether your account is legacy or new — the free tier you get depends on this&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;For learning AWS without risk:&lt;/strong&gt; use AWS Skill Builder (free courses) or Amazon-provided sandboxes at re:Invent events. These have hard spending limits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common surprise-bill scenarios
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Left an EC2 instance running for 8 months → around 60 to 100 USD bill (plus IPv4 charges)&lt;/li&gt;
&lt;li&gt;CloudFront hit by scraping bot → 500 GB egress = 45 USD&lt;/li&gt;
&lt;li&gt;Forgot to delete RDS → around 15 USD/month × months&lt;/li&gt;
&lt;li&gt;Set up NAT Gateway "just to try" → 32 USD/month + IPv4&lt;/li&gt;
&lt;li&gt;Multiple EIPs allocated but not attached → 3.60 USD each per month&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Real AWS bills for someone experimenting without alerts typically range 20 to 200 USD/month unexpectedly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;AWS Free Tier is real but requires attention, and the rules changed twice recently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Feb 2024:&lt;/strong&gt; public IPv4 addresses became a paid resource&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;July 2025:&lt;/strong&gt; the 12-month free tier was replaced (for new accounts) with a 6-month credit-based plan&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Always Free services remain the safest bet&lt;/strong&gt; — they never expire and cover Lambda, DynamoDB, CloudFront, and 30+ others&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data transfer OUT is still the biggest hidden cost&lt;/strong&gt; across everything&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Billing alerts are mandatory, not optional&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For most personal projects in 2026, Cloudflare Pages or Vercel Hobby offer more predictable cost profiles than AWS. Use AWS when you specifically need its services (Lambda, DynamoDB, ML APIs), not for basic web hosting.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://aiunplugged.in/blog/aws-free-tier-2026-reality/" rel="noopener noreferrer"&gt;aiunplugged.in&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>cloud</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
