<?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: Dipankar Sarkar</title>
    <description>The latest articles on DEV Community by Dipankar Sarkar (@dipankar_sarkar).</description>
    <link>https://dev.to/dipankar_sarkar</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%2F4010138%2F11e8895b-f7f8-41d3-9b12-2720a266d472.jpg</url>
      <title>DEV Community: Dipankar Sarkar</title>
      <link>https://dev.to/dipankar_sarkar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dipankar_sarkar"/>
    <language>en</language>
    <item>
      <title>I put a Rust layer under LiteLLM. Here is where it actually helped (and where it did not)</title>
      <dc:creator>Dipankar Sarkar</dc:creator>
      <pubDate>Wed, 01 Jul 2026 22:42:18 +0000</pubDate>
      <link>https://dev.to/dipankar_sarkar/i-put-a-rust-layer-under-litellm-here-is-where-it-actually-helped-and-where-it-did-not-4pja</link>
      <guid>https://dev.to/dipankar_sarkar/i-put-a-rust-layer-under-litellm-here-is-where-it-actually-helped-and-where-it-did-not-4pja</guid>
      <description>&lt;p&gt;LiteLLM is the glue a lot of us reach for when an app has to talk to more than one&lt;br&gt;
model provider. One interface, dozens of backends. It is great. But once you run it&lt;br&gt;
under real load, the hot paths stop being the model call and start being the&lt;br&gt;
plumbing around it: connection pooling, rate limiting, token counting on big&lt;br&gt;
inputs. That plumbing is pure Python, and it shows.&lt;/p&gt;

&lt;p&gt;So I built &lt;code&gt;fast-litellm&lt;/code&gt;: a drop-in Rust acceleration layer that swaps the hot&lt;br&gt;
paths out for PyO3 extensions and falls back to Python everywhere else.&lt;/p&gt;
&lt;h2&gt;
  
  
  The honest benchmark table
&lt;/h2&gt;

&lt;p&gt;I am going to lead with the numbers, including the ones that did not go my way.&lt;br&gt;
These compare production-grade Python (thread-safe) against the Rust versions:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Connection pool&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;3.2x faster&lt;/strong&gt; (lock-free DashMap)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rate limiting&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;1.6x faster&lt;/strong&gt; (atomic ops)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Large-text token counting&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.5-1.7x faster&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;High-cardinality rate limits (1000+ keys)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;42x less memory&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Small-text token counting&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;0.5x — Python wins&lt;/strong&gt; (FFI overhead dominates)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Routing with complex Python objects&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.4x — Python wins&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That last block is the important part. Crossing the Python/Rust boundary is not&lt;br&gt;
free. For a 12-token chat message, the FFI overhead is bigger than the work you&lt;br&gt;
saved, so Rust loses. Anyone who tells you their native extension is faster at&lt;br&gt;
everything is not measuring the small cases.&lt;/p&gt;

&lt;p&gt;Where it wins, it wins because of &lt;em&gt;data structures&lt;/em&gt;, not because "Rust is fast":&lt;br&gt;
lock-free &lt;code&gt;DashMap&lt;/code&gt; for concurrent connection state, and a memory layout for&lt;br&gt;
high-cardinality rate limiting that holds 1000+ unique keys in a fraction of the&lt;br&gt;
Python footprint. 42x less memory is a data-structure story, not a language story.&lt;/p&gt;
&lt;h2&gt;
  
  
  Drop-in, or it does not get used
&lt;/h2&gt;

&lt;p&gt;The design constraint I cared about most: nobody rewrites their app to try 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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;fast_litellm&lt;/span&gt;  &lt;span class="c1"&gt;# accelerates LiteLLM automatically
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;litellm&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;litellm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;completion&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;gpt-3.5-turbo&lt;/span&gt;&lt;span class="sh"&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="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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello!&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;One import before &lt;code&gt;litellm&lt;/code&gt;. It monkeypatches the hot paths, and every accelerated&lt;br&gt;
component has an automatic fallback to the original Python if anything looks off.&lt;br&gt;
Feature flags let you roll it out to a percentage of traffic first. If you are&lt;br&gt;
running the proxy under gunicorn, a two-line &lt;code&gt;app.py&lt;/code&gt; with &lt;code&gt;--preload&lt;/code&gt; does it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would take from this
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Profile before you port. The win was in three specific hot paths, not "the code."&lt;/li&gt;
&lt;li&gt;Measure the small inputs too. FFI overhead is real and it will embarrass you.&lt;/li&gt;
&lt;li&gt;Make it a drop-in or it dies on the vine. Zero-config plus automatic fallback is
what makes a native accelerator safe to actually ship.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code, full benchmark breakdown, and the PyO3 architecture are here:&lt;br&gt;
&lt;a href="https://github.com/neul-labs/fast-litellm" rel="noopener noreferrer"&gt;https://github.com/neul-labs/fast-litellm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you run LiteLLM at any real volume, I would love to know which path is your&lt;br&gt;
bottleneck. Kick the tyres, issues welcome.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>python</category>
      <category>ai</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
