<?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: Ezekiel ADEJOBI</title>
    <description>The latest articles on DEV Community by Ezekiel ADEJOBI (@ezeko).</description>
    <link>https://dev.to/ezeko</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%2F4170730%2F736e985c-59fe-41e6-9d20-f1ed6b33b773.jpg</url>
      <title>DEV Community: Ezekiel ADEJOBI</title>
      <link>https://dev.to/ezeko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ezeko"/>
    <language>en</language>
    <item>
      <title>Zero-Infra Semantic Caching in Node.js: A Complete Guide to Caching LLM Responses</title>
      <dc:creator>Ezekiel ADEJOBI</dc:creator>
      <pubDate>Tue, 10 Nov 2026 13:33:36 +0000</pubDate>
      <link>https://dev.to/ezeko/zero-infra-semantic-caching-in-nodejs-a-complete-guide-to-caching-llm-responses-1b3e</link>
      <guid>https://dev.to/ezeko/zero-infra-semantic-caching-in-nodejs-a-complete-guide-to-caching-llm-responses-1b3e</guid>
      <description>&lt;p&gt;Building production-ready AI applications means battling two invisible enemies: soaring API costs and sluggish response latencies. While traditional web applications rely heavily on caching to solve these scaling issues, standard string-matching strategies completely fall apart when applied to Large Language Models (LLMs).&lt;br&gt;
In this comprehensive guide, we will implement zero-infrastructure semantic caching in a TypeScript/Node.js environment. By utilizing an embedded, vector-based semantic engine directly inside your runtime memory and local file system, you can instantly intercept duplicate user intents, serve cached hits in under 1ms, and retain type-safe autocompletion for your OpenAI SDK code.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. The Core Problem: Why Traditional Caching Fails LLMs
&lt;/h2&gt;

&lt;p&gt;Traditional key-value stores like Redis or standard in-memory objects look for exact string matches. If a user submits a query, it maps to a specific key. But natural language is infinitely variable.&lt;br&gt;
When two users ask the exact same question phrasing it differently, an exact-match cache treats them as entirely unique queries. This results in a cache miss, forcing an unnecessary, expensive round-trip to the upstream LLM API.&lt;/p&gt;
&lt;h2&gt;
  
  
  Exact Match vs. Semantic Intent Examples
&lt;/h2&gt;

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

&lt;p&gt;&lt;strong&gt;The Scale Penalty: Cost &amp;amp; Latency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At scale, routing every minor phrasing variation directly to the LLM API degrades application health in two ways:&lt;br&gt;
•** Latency Overhead:** A typical cloud LLM response takes anywhere from 800ms to 4000ms, depending on token depth and network load. A semantic cache hit processes locally, serving responses in &amp;lt;1ms.&lt;br&gt;
&lt;strong&gt;• Compounding Fees:&lt;/strong&gt; Paying per token for identical user intents drains your budget. Intercepting a high percentage of repetitive customer interactions drops your API bill significantly.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Architecture Overview
&lt;/h2&gt;

&lt;p&gt;Instead of comparing raw text strings, a semantic cache converts incoming queries into high-dimensional numerical vectors (embeddings). It compares the spatial vector distance between the new query and historically cached questions using a Cosine Similarity algorithm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dot"&gt;&lt;code&gt;&lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="no"&gt;TD&lt;/span&gt;
    &lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt; &lt;span class="nc"&gt;Vectorizer&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nc"&gt;Embeddings&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="no"&gt;B&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;Cosine&lt;/span&gt; &lt;span class="nc"&gt;Similarity&lt;/span&gt; &lt;span class="nc"&gt;Check&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;br&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Is&lt;/span&gt; &lt;span class="n"&gt;distance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;minSimilarity&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="no"&gt;C&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="nc"&gt;Yes&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Cache&lt;/span&gt; &lt;span class="nc"&gt;Hit&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;1ms&lt;/span&gt; &lt;span class="nc"&gt;Return&lt;/span&gt; &lt;span class="nc"&gt;Saved&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="no"&gt;C&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="nc"&gt;No&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Cache&lt;/span&gt; &lt;span class="nc"&gt;Miss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Call&lt;/span&gt; &lt;span class="nc"&gt;Upstream&lt;/span&gt; &lt;span class="no"&gt;LLM&lt;/span&gt; &lt;span class="no"&gt;API&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="no"&gt;E&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Save&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt; &lt;span class="nc"&gt;Vector&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="no"&gt;F&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;G&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Return&lt;/span&gt; &lt;span class="nc"&gt;Fresh&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Step-by-Step Implementation
&lt;/h2&gt;

&lt;p&gt;We will use local-semantic-cache, an embedded utility that handles local embedding vector generation, high-speed matrix similarity calculations, and automatic file serialization without requiring an external vector database setup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Installation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install the required core packages via your preferred package manager:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bash
npm install local-semantic-cache openai
npm install --save-dev typescript @types/node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2 &amp;amp; 3: Wrapping OpenAI and Configuring Sensitivity Thresholds&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a script file named aiService.ts. Here, we instantiate the base OpenAI SDK instance, configure our semantic validation thresholds, and attach lifecycle debugging hooks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'openai'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;wrapOpenAI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;LocalCacheStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'local-semantic-cache'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Initialize the standard OpenAI Client&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;baseOpenAI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&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="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;OPENAI_API_KEY&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;// Configure the intelligent semantic wrapper&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;wrapOpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;baseOpenAI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Define our similarity sensitivity (0.0 to 1.0)&lt;/span&gt;
  &lt;span class="c1"&gt;// 0.85 is ideal for catching identical intents with minor phrasing variations&lt;/span&gt;
  &lt;span class="n"&gt;minSimilarity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="c1"&gt;// Custom hooks for observability and metrics logging&lt;/span&gt;
  &lt;span class="n"&gt;onCacheHit&lt;/span&gt;&lt;span class="p"&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="n"&gt;executionTimeMs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`[🚀 Cache Hit] Internal Match Found in ${executionTimeMs}ms for: "${query}"`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;onCacheMiss&lt;/span&gt;&lt;span class="p"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`[🌐 Cache Miss] Routing intent to upstream OpenAI API: "${query}"`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Enabling Persistent Storage Across Restarts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In-memory storage vanishes whenever your server process recycles. To ensure your cache survives standard server deployments and reboots, configure the filePath parameter to automatically back up operational vectors to the local disk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'openai'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;wrapOpenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'local-semantic-cache'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'path'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;baseOpenAI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&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="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;OPENAI_API_KEY&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;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;persistentOpenAI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;wrapOpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;baseOpenAI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;minSimilarity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// Automatically write changes to disk reactively&lt;/span&gt;
  &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;path&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;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'data'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'llm-cache.json'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="n"&gt;onCacheHit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Served from disk-backed cache in ${time}ms`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;onCacheMiss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Fetching new tokens from provider for: ${q}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Handling Advanced Edge Cases
&lt;/h2&gt;

&lt;p&gt;Production AI features require more than single-prompt string lookups. The local-semantic-cache wrapper preserves the native signature of the underlying client while managing complex workflows under the hood.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edge Case A: Streaming Responses (stream: true)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When streaming responses chunk-by-chunk via Server-Sent Events (SSE), standard interceptors often break the data stream or cache corrupted chunks. The wrapper automatically detects stream: true, allows the chunks to pass through instantly without latency degradation, intercepts the text stream into memory behind the scenes, and securely caches the fully reconstructed string block once the stream finishes cleanly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;persistentOpenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./aiService'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleStreamingAssistant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;persistentOpenAI&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'gpt-4o'&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="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'user'&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="n"&gt;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="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Cache safely intercepts, stores, and plays back streams smoothly&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;for&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&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;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="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Edge Case B: Multi-Turn Conversations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The system flattens historical structural logs—evaluating combined array messages containing system, user, and assistant contexts—to match identical contextual moments accurately inside long chat threads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleUserSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chatHistory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'user'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s1"&gt;'assistant'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s1"&gt;'system'&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="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;persistentOpenAI&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'gpt-4o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// The engine vectorizes the final user message while using previous entries as context modifiers&lt;/span&gt;
    &lt;span class="n"&gt;messages&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;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'system'&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="s1"&gt;'You are an internal IT infrastructure operations assistant.'&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="n"&gt;chatHistory&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;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;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;&lt;strong&gt;Edge Case C: Complete Compile-Time Type Safety&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The package uses advanced TypeScript generics to guarantee that the returned object exactly maps to the original SDK type signature. Developers retain full code autocomplete, method navigation, and parameter inspection features.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// IDE instantly resolves types, options, and returns without casting hacks&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;persistentOpenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./aiService'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;executeTypedCall&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;completion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;persistentOpenAI&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'gpt-4o'&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="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'user'&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="s1"&gt;'Verify type signature performance metrics.'&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;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.2&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;// Clean, autocompleted data structures:&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;completion&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;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;h2&gt;
  
  
  Conclusion &amp;amp; Community Engagement
&lt;/h2&gt;

&lt;p&gt;Implementing a semantic cache doesn't require setting up heavy external enterprise databases or managing complex cloud infrastructure from day one. By dropping an embedded semantic layer directly into your Node.js code, you can build fast, cost-effective AI apps that scale smoothly.&lt;br&gt;
&lt;strong&gt;• View the Source Code &amp;amp; Examples:&lt;/strong&gt; Check out the full engine source, raise feature issues, or browse benchmarks over on GitHub at &lt;a href="https://github.com/Ezeko/semanticcache.git" rel="noopener noreferrer"&gt;&lt;/a&gt;.&lt;br&gt;
💬** Let’s discuss in the comments below:** How are you managing LLM latency overhead in production apps today? What similarity threshold balance works best for your specific application data models?&lt;br&gt;
If you want to tailor this implementation further, let me know:&lt;br&gt;
• What specific embedding model or provider do you plan to use?&lt;br&gt;
• Approximately how many unique queries does your application process per day?&lt;br&gt;
• Do you need to implement cache expiration (TTL) strategies for volatile real-time data?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>ai</category>
      <category>node</category>
    </item>
    <item>
      <title>Stop Over-Engineering LLM Caching: The 1-Line Local Semantic Cache for OpenAI</title>
      <dc:creator>Ezekiel ADEJOBI</dc:creator>
      <pubDate>Tue, 10 Nov 2026 10:58:05 +0000</pubDate>
      <link>https://dev.to/ezeko/stop-over-engineering-llm-caching-the-1-line-local-semantic-cache-for-openai-5hdc</link>
      <guid>https://dev.to/ezeko/stop-over-engineering-llm-caching-the-1-line-local-semantic-cache-for-openai-5hdc</guid>
      <description>&lt;p&gt;Every time an LLM application hits production, developers run into the same two wall-clapping metrics: latency and API cost.&lt;br&gt;
A standard call to gpt-4o or gpt-4-turbo takes anywhere from 1,200ms to 2,500ms. If your users repeat similar queries or refresh a dashboard, they are sitting around waiting for text to render while your OpenAI bill compounds in the background.&lt;br&gt;
The obvious answer is caching. But traditional caching is completely broken for natural language.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Exact-Match Caching Fails
&lt;/h2&gt;

&lt;p&gt;If you back your API with Redis, Memcached, or a standard JavaScript Map, you are caching based on exact string keys. Humans do not type like machines. Look at these two prompts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;"What is the capital of France?"&lt;/li&gt;
&lt;li&gt;"Tell me France's capital"
To a key-value store, these strings share almost nothing. It's a 100% cache miss. You still pay the API toll, and your user still waits two seconds for a response.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  The Over-Engineered Alternative
&lt;/h2&gt;

&lt;p&gt;To fix this, the current industry consensus tells you to build a semantic caching layer using vector databases:&lt;br&gt;
[Prompt] ──► Call Embedding API ──► Query Cloud Vector DB ──► Hit/Miss ──► Fallback to LLM API&lt;br&gt;
Suddenly, just to cache simple prompt responses, your clean architecture requires:&lt;br&gt;
• Spinning up a Docker container or signing up for a hosted provider (Pinecone, Qdrant, Milvus).&lt;br&gt;
• Managing a secondary API key and network domain.&lt;br&gt;
• Provisioning cloud clusters that add 50ms–120ms of pure network round-trip overhead.&lt;br&gt;
For 90% of applications, this is absolute infrastructure overkill. You shouldn’t need a distributed database cluster just to find out if two strings mean the same thing.&lt;/p&gt;
&lt;h2&gt;
  
  
  The 1-Line Solution: wrapOpenAI
&lt;/h2&gt;

&lt;p&gt;This is exactly why I built local-semantic-cache. It is a zero-dependency, ultra-fast local semantic cache that handles text vectorization, similarity search, and file persistence entirely in-process.&lt;br&gt;
If you are using the official Node.js openai SDK, you can drop it into your existing codebase with exactly one line of code without rewriting a single parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import OpenAI from 'openai';
import { wrapOpenAI } from 'local-semantic-cache/openai';

// Wrap your existing client instance seamlessly
const openai = wrapOpenAI(new OpenAI({ apiKey: process.env.OPENAI_API_KEY }));

// Everything else in your application remains 100% unchanged:
const completion = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'What is the capital of France?' }],
});

console.log(completion.choices[0].message.content);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Works Under the Hood
&lt;/h2&gt;

&lt;p&gt;The wrapOpenAI utility leverages a JavaScript Proxy pattern. It intercepts the underlying SDK methods transparently.&lt;br&gt;
When a chat completion request comes through, the proxy extracts the prompt content, evaluates it against your local cache vector pool in &amp;lt;1ms, and—if it crosses your semantic similarity threshold—instantly resolves the promise with the cached payload. If it's a miss, it forwards the call to OpenAI's servers and populates the cache behind the scenes. Best of all, streaming requests bypass the cache seamlessly so you don't break token-by-token UX.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Benchmarks
&lt;/h2&gt;

&lt;p&gt;Here is how local semantic caching stacks up against raw API calls and cloud-hosted vector alternatives:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvlknoyusojwqje8c5uou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvlknoyusojwqje8c5uou.png" alt=" " width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How It Works Without a Vector Database
&lt;/h2&gt;

&lt;p&gt;You might wonder how a semantic cache can run inside Node.js or the browser without spinning up an external database binary or native bindings.&lt;br&gt;
local-semantic-cache utilizes a lightweight Feature Hashing (TF-IDF) trick directly in JavaScript/TypeScript. It breaks incoming text strings into unigram and bigram tokens and projects them directly into a fixed-dimension vector space.&lt;br&gt;
Once the vectors are generated, they are automatically L2 unit-normalized. This means calculating the cosine similarity between two prompts simplifies mathematically down to a raw dot-product multiplication:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F20xx3q0f9warh7b25qra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F20xx3q0f9warh7b25qra.png" alt=" " width="799" height="96"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because it's pure, synchronous floating-point math executing across flat arrays, the engine can scan hundreds of cached entries in fractions of a millisecond.&lt;/p&gt;
&lt;h2&gt;
  
  
  Simple Disk Persistence
&lt;/h2&gt;

&lt;p&gt;You don't have to lose your cache when your server restarts. The package includes a native file-persistence configuration that writes directly to local storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { SemanticCache } from 'local-semantic-cache';

const cache = new SemanticCache({
  minSimilarity: 0.60, // Cutoff threshold (0.0 to 1.0)
  filePath: './cache.json' // Persists cache directly to disk automatically
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you prefer to use high-fidelity cloud vectors, you can easily plug in OpenAI's text-embedding-3-small or Google Gemini embeddings by passing an optional embeddingFunction directly into the constructor options.&lt;/p&gt;

&lt;p&gt;Move Faster, Spend Less&lt;/p&gt;

&lt;p&gt;Stop provisioning infrastructure for problems that can be solved with clean math in your application layer. Wrap your client, protect your API budget, and give your users sub-millisecond response speeds.&lt;br&gt;
• 📦 Install via npm: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm install local-semantic-cache&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;• 💻 Check out the source / Leave a Star: GitHub - &lt;a href="https://github.com/Ezeko/semanticcache.git" rel="noopener noreferrer"&gt;Ezeko/semanticcache&lt;/a&gt;&lt;br&gt;
Let's open up the comments:&lt;br&gt;
• How are you currently handling LLM caching in your staging or production pipelines?&lt;br&gt;
• Have you run into edge cases where semantic thresholds matched completely unrelated prompts, and how did you tune them?&lt;br&gt;
Drop your thoughts, questions, or edge cases below! If you want to see an example of this running with Next.js Route Handlers or Cloudflare Workers, let me know.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>productivity</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>you need to see this</title>
      <dc:creator>Ezekiel ADEJOBI</dc:creator>
      <pubDate>Tue, 10 Nov 2026 10:30:28 +0000</pubDate>
      <link>https://dev.to/ezeko/-143j</link>
      <guid>https://dev.to/ezeko/-143j</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/ezeko/how-to-verify-stripe-clerk-and-lemon-squeezy-webhooks-in-nextjs-15-app-router-without-breaking-2792" class="crayons-story__hidden-navigation-link"&gt;How to Verify Stripe, Clerk, and Lemon Squeezy Webhooks in Next.js 15 App Router without Breaking Raw-Body Parsing&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/ezeko" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4170730%2F736e985c-59fe-41e6-9d20-f1ed6b33b773.jpg" alt="ezeko profile" class="crayons-avatar__image" width="336" height="336"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/ezeko" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Ezekiel ADEJOBI
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Ezekiel ADEJOBI
                
                
              
              &lt;div id="story-author-preview-content-5004755" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/ezeko" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4170730%2F736e985c-59fe-41e6-9d20-f1ed6b33b773.jpg" class="crayons-avatar__image" alt="" width="336" height="336"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Ezekiel ADEJOBI&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/ezeko/how-to-verify-stripe-clerk-and-lemon-squeezy-webhooks-in-nextjs-15-app-router-without-breaking-2792" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Nov 9&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/ezeko/how-to-verify-stripe-clerk-and-lemon-squeezy-webhooks-in-nextjs-15-app-router-without-breaking-2792" id="article-link-5004755"&gt;
          How to Verify Stripe, Clerk, and Lemon Squeezy Webhooks in Next.js 15 App Router without Breaking Raw-Body Parsing
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/backend"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;backend&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/nextjs"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;nextjs&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/security"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;security&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
            &lt;a href="https://dev.to/ezeko/how-to-verify-stripe-clerk-and-lemon-squeezy-webhooks-in-nextjs-15-app-router-without-breaking-2792#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            3 min read
          &lt;/small&gt;
          
          &lt;div class="only-sidebar-menu-item trusted-visible-block print-hidden align-center"&gt;
                
                    
                
          &lt;/div&gt;

        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>api</category>
      <category>nextjs</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Verify Stripe, Clerk, and Lemon Squeezy Webhooks in Next.js 15 App Router without Breaking Raw-Body Parsing</title>
      <dc:creator>Ezekiel ADEJOBI</dc:creator>
      <pubDate>Mon, 09 Nov 2026 13:56:13 +0000</pubDate>
      <link>https://dev.to/ezeko/how-to-verify-stripe-clerk-and-lemon-squeezy-webhooks-in-nextjs-15-app-router-without-breaking-2792</link>
      <guid>https://dev.to/ezeko/how-to-verify-stripe-clerk-and-lemon-squeezy-webhooks-in-nextjs-15-app-router-without-breaking-2792</guid>
      <description>&lt;p&gt;If you are deploying webhooks in Next.js 15 (App Router), Cloudflare Workers, or Hono, you have likely run into the infamous cryptographic verification failure.&lt;br&gt;
On paper, the process is straightforward: your provider (Stripe, Clerk, or Lemon Squeezy) sends a POST request, you check the signature using your webhook secret, and you return a 200 OK.&lt;br&gt;
In reality, modern frameworks make it incredibly painful to access the unmodified, byte-for-byte raw request body required to validate signatures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The High-Pain Problem: Automated Parsing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Modern web frameworks love helper functions that automatically parse incoming JSON payloads into JavaScript objects.&lt;br&gt;
However, cryptographic signature validation breaks if even a single space, newline, or character is modified during that parsing step. The signature provided in the headers expects the exact string that left the provider's server.&lt;br&gt;
Every runtime has a completely different, often fragile, way of handling this:&lt;br&gt;
• Next.js App Router forces you to explicitly read req.text() and manually manage body streaming before any middleware or route layout configuration intercepts it.&lt;br&gt;
• Cloudflare Workers &amp;amp; V8 Isolates process request bodies as a ReadableStream, which completely crashes traditional Node.js crypto or Buffer dependencies.&lt;br&gt;
• Hono &amp;amp; Remix require specific runtime context wrappers that add boilerplate to every single endpoint.&lt;br&gt;
If you are tired of copying and pasting framework-specific hacks or dealing with random 401 Unauthorized errors in production, here is the clean, 2-line solution.&lt;/p&gt;

&lt;p&gt;The Solution: webhookshield&lt;/p&gt;

&lt;p&gt;Instead of writing individual validation blocks and stream-parsers for every platform and vendor, webhookshield abstracts away the runtime complexity. It is an open-source, ultra-lightweight utility designed specifically to normalize raw-body extraction and signature verification across any serverless or edge runtime.&lt;br&gt;
Here is how to lock down your endpoints seamlessly.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Next.js 15 App Router Implementation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Verifying both Stripe and Clerk hooks inside a Next.js route handler typically requires dozens of lines of unreadable middleware logic. With webhookshield, you pass the standard NextRequest object directly.&lt;/p&gt;

&lt;p&gt;`import { NextRequest, NextResponse } from 'next/server';&lt;br&gt;
import { WebhookShield } from 'webhookshield';&lt;/p&gt;

&lt;p&gt;export async function POST(req: NextRequest) {&lt;br&gt;
  try {&lt;br&gt;
    // 1. One unified line extracts the raw body and validates the signature&lt;br&gt;
    const { payload, isValid } = await WebhookShield.verify({&lt;br&gt;
      request: req,&lt;br&gt;
      secret: process.env.STRIPE_WEBHOOK_SECRET!,&lt;br&gt;
      provider: 'stripe' // Seamlessly switch to 'clerk' or 'lemonsqueezy'&lt;br&gt;
    });&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!isValid) {
  return NextResponse.json({ error: 'Signature verification failed' }, { status: 401 });
}

// 2. Process your fully typed payload safely
const event = payload;
if (event.type === 'checkout.session.completed') {
  // Execute your core fulfillment logic here
}

return NextResponse.json({ received: true }, { status: 200 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;} catch (error) {&lt;br&gt;
    return NextResponse.json({ error: 'Webhook processing error' }, { status: 400 });&lt;br&gt;
  }&lt;br&gt;
}`&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Unified Webhook Verification on Cloudflare Workers (Hono)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Deploying webhooks to the Edge often breaks native SDKs because Node.js global utilities are missing. webhookshield is entirely runtime-agnostic and runs perfectly on Cloudflare Workers and Hono.&lt;/p&gt;

&lt;p&gt;`import { Hono } from 'hono';&lt;br&gt;
import { WebhookShield } from 'webhookshield';&lt;/p&gt;

&lt;p&gt;const app = new Hono();&lt;/p&gt;

&lt;p&gt;app.post('/api/webhooks/clerk', async (c) =&amp;gt; {&lt;br&gt;
  // Pass the raw web-standard Request object directly&lt;br&gt;
  const { payload, isValid } = await WebhookShield.verify({&lt;br&gt;
    request: c.req.raw,&lt;br&gt;
    secret: c.env.CLERK_WEBHOOK_SECRET,&lt;br&gt;
    provider: 'clerk'&lt;br&gt;
  });&lt;/p&gt;

&lt;p&gt;if (!isValid) {&lt;br&gt;
    return c.json({ error: 'Unauthorized' }, { status: 401 });&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;// Handle fully verified Clerk database synchronization&lt;br&gt;
  const { type, data } = payload;&lt;br&gt;
  if (type === 'user.created') {&lt;br&gt;
    // Sync user metrics safely&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;return c.json({ success: true });&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;export default app;`&lt;/p&gt;

&lt;p&gt;Stop wasting time troubleshooting body-parsing bugs and signature drift. Secure your endpoints, handle your serverless webhooks reliably, and get back to building your product.&lt;br&gt;
• 📦 Get Started: npm install webhookshield&lt;br&gt;
• 💻 Contribute / View Source: github.com&lt;/p&gt;

</description>
      <category>backend</category>
      <category>nextjs</category>
      <category>security</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
