<?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: Aditya Agrawal</title>
    <description>The latest articles on DEV Community by Aditya Agrawal (@adityagrawal45).</description>
    <link>https://dev.to/adityagrawal45</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%2F4051968%2F37f69a2e-8c71-448f-b4cd-3d2cb4d6f003.jpeg</url>
      <title>DEV Community: Aditya Agrawal</title>
      <link>https://dev.to/adityagrawal45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adityagrawal45"/>
    <language>en</language>
    <item>
      <title>I Stopped Treating AI as a Black Box and Started Building a Semantic Caching System from Scratch</title>
      <dc:creator>Aditya Agrawal</dc:creator>
      <pubDate>Wed, 29 Jul 2026 20:01:43 +0000</pubDate>
      <link>https://dev.to/adityagrawal45/i-stopped-treating-ai-as-a-black-box-and-started-building-a-semantic-caching-system-from-scratch-33kb</link>
      <guid>https://dev.to/adityagrawal45/i-stopped-treating-ai-as-a-black-box-and-started-building-a-semantic-caching-system-from-scratch-33kb</guid>
      <description>&lt;p&gt;Most AI projects today look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;response&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="nf"&gt;invoke&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works.&lt;/p&gt;

&lt;p&gt;It's powerful.&lt;/p&gt;

&lt;p&gt;But one question kept bothering me:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do I actually understand what's happening behind the scenes?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's what led me to start building a &lt;strong&gt;Semantic Caching System from scratch&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Not by copying an existing GitHub repository.&lt;/p&gt;

&lt;p&gt;Not by wrapping an LLM in another API.&lt;/p&gt;

&lt;p&gt;But by implementing every component myself and understanding why it exists.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Semantic Caching?
&lt;/h1&gt;

&lt;p&gt;Imagine you're building an AI chatbot.&lt;/p&gt;

&lt;p&gt;A user asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What is Semantic Caching?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A few seconds later another user asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Can you explain semantic caching?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The wording is different.&lt;/p&gt;

&lt;p&gt;But the intent is almost identical.&lt;/p&gt;

&lt;p&gt;Traditional caching won't help because the strings aren't exactly the same.&lt;/p&gt;

&lt;p&gt;Semantic caching solves this problem by understanding that two different sentences can have the same meaning.&lt;/p&gt;

&lt;p&gt;Instead of recomputing the answer every time, we can reuse a previous response when two queries are semantically similar.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster responses&lt;/li&gt;
&lt;li&gt;Lower compute costs&lt;/li&gt;
&lt;li&gt;Better scalability&lt;/li&gt;
&lt;li&gt;Reduced load on AI models&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  My Goal
&lt;/h1&gt;

&lt;p&gt;Instead of building another AI application, I wanted to understand how one of its important building blocks actually works.&lt;/p&gt;

&lt;p&gt;So I decided to build everything myself.&lt;/p&gt;

&lt;p&gt;My learning rule is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If I can't explain why a class exists, I shouldn't use it.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  What I've Built So Far
&lt;/h1&gt;

&lt;p&gt;The project is divided into independent modules instead of one large codebase.&lt;/p&gt;

&lt;p&gt;Current components include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Query preprocessing&lt;/li&gt;
&lt;li&gt;Text normalization&lt;/li&gt;
&lt;li&gt;Tokenization&lt;/li&gt;
&lt;li&gt;Embedding generation&lt;/li&gt;
&lt;li&gt;Cosine similarity engine&lt;/li&gt;
&lt;li&gt;Semantic cache manager&lt;/li&gt;
&lt;li&gt;Cache hit and miss detection&lt;/li&gt;
&lt;li&gt;Cache eviction strategies&lt;/li&gt;
&lt;li&gt;Logging&lt;/li&gt;
&lt;li&gt;Performance metrics&lt;/li&gt;
&lt;li&gt;FastAPI endpoints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each module has a single responsibility, making the architecture easier to understand and extend.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Development Process
&lt;/h1&gt;

&lt;p&gt;Rather than writing thousands of lines of code at once, I'm building one class at a time.&lt;/p&gt;

&lt;p&gt;For every class, I follow the same workflow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understand the problem it solves&lt;/li&gt;
&lt;li&gt;Design the class&lt;/li&gt;
&lt;li&gt;Implement it&lt;/li&gt;
&lt;li&gt;Explain every line of code&lt;/li&gt;
&lt;li&gt;Test it&lt;/li&gt;
&lt;li&gt;Improve it&lt;/li&gt;
&lt;li&gt;Move to the next component&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach is slower than following a tutorial, but I've learned far more from it.&lt;/p&gt;




&lt;h1&gt;
  
  
  What I'm Learning
&lt;/h1&gt;

&lt;p&gt;Working on this project has already helped me understand concepts that are often hidden behind libraries:&lt;/p&gt;

&lt;h2&gt;
  
  
  Semantic Search
&lt;/h2&gt;

&lt;p&gt;How can two different sentences be recognized as having similar meanings?&lt;/p&gt;

&lt;h2&gt;
  
  
  Embeddings
&lt;/h2&gt;

&lt;p&gt;How can text be represented as mathematical vectors?&lt;/p&gt;

&lt;h2&gt;
  
  
  Cosine Similarity
&lt;/h2&gt;

&lt;p&gt;How do we measure the similarity between two vectors?&lt;/p&gt;

&lt;h2&gt;
  
  
  Cache Management
&lt;/h2&gt;

&lt;p&gt;How do we decide whether to reuse an existing response or compute a new one?&lt;/p&gt;

&lt;h2&gt;
  
  
  Cache Eviction
&lt;/h2&gt;

&lt;p&gt;When memory becomes full, which cached items should be removed first?&lt;/p&gt;




&lt;h1&gt;
  
  
  Why I'm Avoiding the "Magic"
&lt;/h1&gt;

&lt;p&gt;Modern AI libraries are incredible.&lt;/p&gt;

&lt;p&gt;But they also make it very easy to skip understanding the fundamentals.&lt;/p&gt;

&lt;p&gt;I wanted to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How embeddings work&lt;/li&gt;
&lt;li&gt;Why cosine similarity works&lt;/li&gt;
&lt;li&gt;Why semantic search works&lt;/li&gt;
&lt;li&gt;Why caching improves performance&lt;/li&gt;
&lt;li&gt;How these components communicate inside a real system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Building everything myself has answered many of those questions.&lt;/p&gt;




&lt;h1&gt;
  
  
  What's Next?
&lt;/h1&gt;

&lt;p&gt;This project is still evolving.&lt;/p&gt;

&lt;p&gt;The next steps include exploring how to make the architecture more production-oriented by integrating technologies such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redis&lt;/li&gt;
&lt;li&gt;Vector databases&lt;/li&gt;
&lt;li&gt;Distributed caching&lt;/li&gt;
&lt;li&gt;Kubernetes&lt;/li&gt;
&lt;li&gt;Monitoring and metrics&lt;/li&gt;
&lt;li&gt;Horizontal scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea isn't to build the biggest project.&lt;/p&gt;

&lt;p&gt;It's to build one that teaches me something new every day.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;One thing I've realized is that it's easy to build software that works.&lt;/p&gt;

&lt;p&gt;It's much harder—and far more rewarding—to build software that you truly understand.&lt;/p&gt;

&lt;p&gt;This project has reminded me that the best way to learn isn't by collecting frameworks.&lt;/p&gt;

&lt;p&gt;It's by asking:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Could I build this myself?"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's the question I'm trying to answer, one class at a time.&lt;/p&gt;




&lt;p&gt;If you're working on Backend Engineering, AI Infrastructure, or Distributed Systems, I'd love to hear how you approach learning complex systems from first principles.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>architecture</category>
      <category>llm</category>
      <category>python</category>
    </item>
  </channel>
</rss>
