<?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: LeoLan</title>
    <description>The latest articles on DEV Community by LeoLan (@oar06g).</description>
    <link>https://dev.to/oar06g</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3571758%2F56c09a56-df9b-4b0f-bcfb-8ddbd515fabc.jpg</url>
      <title>DEV Community: LeoLan</title>
      <link>https://dev.to/oar06g</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oar06g"/>
    <language>en</language>
    <item>
      <title>Smarter Ai Agents with Caching, Building cache_utiles</title>
      <dc:creator>LeoLan</dc:creator>
      <pubDate>Thu, 13 Nov 2025 15:55:13 +0000</pubDate>
      <link>https://dev.to/oar06g/smarter-ai-agents-with-caching-building-cacheutiles-4cc0</link>
      <guid>https://dev.to/oar06g/smarter-ai-agents-with-caching-building-cacheutiles-4cc0</guid>
      <description>&lt;p&gt;When building &lt;strong&gt;AI agents&lt;/strong&gt; or &lt;strong&gt;LLM-powered systems&lt;/strong&gt;, one of the biggest performance challenges is managing &lt;strong&gt;expensive computations&lt;/strong&gt;, model loading, embeddings, and repeated API calls.  &lt;/p&gt;

&lt;p&gt;Each time your agent recomputes something it could’ve cached, you lose both &lt;strong&gt;time&lt;/strong&gt; and &lt;strong&gt;tokens&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;That’s why I built &lt;strong&gt;&lt;a href="https://github.com/oar06g/CodingStation/tree/main/cache_utiles" rel="noopener noreferrer"&gt;&lt;code&gt;cache_utiles&lt;/code&gt;&lt;/a&gt;&lt;/strong&gt;, a lightweight Python toolkit that brings &lt;strong&gt;LRU caching&lt;/strong&gt;, &lt;strong&gt;memory control&lt;/strong&gt;, and &lt;strong&gt;result persistence&lt;/strong&gt; to AI workflows.&lt;/p&gt;




&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;This module includes three key utilities that make AI pipelines faster and more efficient:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Class&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;MemoryCache&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Generic LRU cache with memory limit (in MB). Ideal for large LLM responses or embeddings.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ModelLRUStore&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Keeps a limited number of loaded model instances in memory, avoiding repeated reloading.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ResultCache&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Caches computed results with &lt;strong&gt;TTL&lt;/strong&gt; and &lt;strong&gt;LRU&lt;/strong&gt; eviction. Perfect for embeddings, API responses, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  1. &lt;code&gt;MemoryCache&lt;/code&gt;, Control Memory Like a Pro
&lt;/h2&gt;

&lt;p&gt;A flexible, thread-safe cache that automatically removes the least recently used items once the memory cap is reached.&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;cache_utils&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;MemoryCache&lt;/span&gt;

&lt;span class="c1"&gt;# Create a cache with a 100MB limit
&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MemoryCache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_size_mb&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="c1"&gt;# Store responses
&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;response:123&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;text&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;Hello, world!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;# Retrieve them later
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;response:123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
&lt;span class="c1"&gt;# {'text': 'Hello, world!'}
&lt;/span&gt;
&lt;span class="c1"&gt;# View cache stats
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;# {'entries': 1, 'used_MB': 0.01, 'max_MB': 100.0}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;Perfect for LLM results, embeddings, or any intermediate computation your agents reuse often.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why It Matters
&lt;/h2&gt;

&lt;p&gt;These caching layers make a real difference in AI and LLM systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce API &amp;amp; compute costs&lt;/li&gt;
&lt;li&gt;Speed up inference and responses&lt;/li&gt;
&lt;li&gt;Prevent memory leaks&lt;/li&gt;
&lt;li&gt;Thread-safe for concurrent agents&lt;/li&gt;
&lt;li&gt;Easy to integrate with LangChain, LlamaIndex, or any custom framework&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Tech Highlights
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Pure Python, no heavy dependencies&lt;/li&gt;
&lt;li&gt;Optional: &lt;code&gt;pympler&lt;/code&gt; for advanced memory tracking&lt;/li&gt;
&lt;li&gt;LRU + TTL eviction strategies&lt;/li&gt;
&lt;li&gt;MIT licensed and open source&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔗 Try It Yourself
&lt;/h2&gt;

&lt;p&gt;👉 Full project: &lt;a href="https://github.com/oar06g/CodingStation/tree/main/cache_utiles" rel="noopener noreferrer"&gt;&lt;strong&gt;CodingStation/cache_utiles&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;pip install pympler   # Optional dependency&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Start caching smarter today!&lt;/p&gt;




</description>
      <category>ai</category>
      <category>programming</category>
      <category>python</category>
      <category>agents</category>
    </item>
  </channel>
</rss>
