<?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: Maverick Y</title>
    <description>The latest articles on DEV Community by Maverick Y (@maverick_y_4e3300c63f2285).</description>
    <link>https://dev.to/maverick_y_4e3300c63f2285</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%2F4005246%2Facb385a5-0ea9-4f5b-833e-b905c77cd4a6.png</url>
      <title>DEV Community: Maverick Y</title>
      <link>https://dev.to/maverick_y_4e3300c63f2285</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maverick_y_4e3300c63f2285"/>
    <language>en</language>
    <item>
      <title>Cut LLM prompt tokens on structured data — losslessly</title>
      <dc:creator>Maverick Y</dc:creator>
      <pubDate>Sat, 27 Jun 2026 12:01:37 +0000</pubDate>
      <link>https://dev.to/maverick_y_4e3300c63f2285/cut-llm-prompt-tokens-on-structured-data-losslessly-op5</link>
      <guid>https://dev.to/maverick_y_4e3300c63f2285/cut-llm-prompt-tokens-on-structured-data-losslessly-op5</guid>
      <description>&lt;h1&gt;
  
  
  Cut LLM prompt tokens on structured data — losslessly
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;A small, dependency-free tool for shrinking logs, JSON, and CSV in prompts — without dropping a single byte.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Logs, JSON, and CSV are some of the bulkiest, most repetitive things we feed into LLMs. They're also where prompt-token costs quietly pile up.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trouble with lossy compression
&lt;/h2&gt;

&lt;p&gt;The usual fix is &lt;strong&gt;semantic compression&lt;/strong&gt;: have a model summarize the input and drop "low-information" tokens. It works — until the question needs the data that got dropped.&lt;/p&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"How many errors are in this log?"&lt;/em&gt;&lt;br&gt;
&lt;em&gt;"What's the total across these 400 rows?"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;…and a lossy compressor can hand back a &lt;strong&gt;confident, wrong answer&lt;/strong&gt; — because the rows it discarded were exactly the ones you needed. The compression looks great. The answer is broken.&lt;/p&gt;

&lt;h2&gt;
  
  
  A different bet: lossless or no-op
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;ctxfold&lt;/strong&gt; takes the opposite approach. Its single rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Lossless or no-op. Never lossy.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of summarizing, it re-encodes &lt;em&gt;structure&lt;/em&gt;. Logs, JSON arrays, and CSV are tables in disguise — the same keys, prefixes, and templates repeat on every line. ctxfold lifts those repeated parts into a one-time header and keeps only what varies per row, producing a compact, self-labeling table the model reads directly. &lt;strong&gt;Nothing is dropped.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The guarantee is enforced in code: every encoder ships with a decoder, and &lt;code&gt;compress()&lt;/code&gt; verifies that decoding its output reproduces the input &lt;em&gt;before&lt;/em&gt; returning it. If it can't, you get your original text back, untouched. It &lt;strong&gt;can't corrupt your data&lt;/strong&gt; — worst case, it does nothing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does the model still read it?
&lt;/h2&gt;

&lt;p&gt;Yes. On real data, ctxfold cuts &lt;strong&gt;~35–40% of tokens&lt;/strong&gt; on templated logs and JSON arrays, fully losslessly. And because the output is plain, labeled text, the model reads it as well as the raw input — in lookup tests against GPT-4o-mini, answers off the compressed form matched answers off the raw data, field for field.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Readability is validated on GPT-4o-mini; the lossless guarantee is model-independent.)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;ctxfold
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;compress&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ctxfold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;stats&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;compress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bigLogOrJsonOrCsv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// send `text` instead of the original&lt;/span&gt;
&lt;span class="nx"&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${(&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tokenRatio&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="nf"&gt;toFixed&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="s2"&gt;% fewer tokens, lossless: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lossless&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a pure text transform — no API calls, no model, zero dependencies — so it works with any LLM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not a replacement — the other half
&lt;/h2&gt;

&lt;p&gt;ctxfold isn't a competitor to semantic compression; it's the complement. &lt;strong&gt;Summarize to extract a subset; ctxfold to shrink repetition without losing anything.&lt;/strong&gt; It shines on structured data, not prose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I built it
&lt;/h2&gt;

&lt;p&gt;This started from a simple frustration: lossy prompt compressors gave impressive token savings, but on aggregate questions — counts, totals, "find this record" — the answers came back wrong, because the data needed to answer had been summarized away. Great compression, broken results. The fix wasn't a smarter summarizer; it was to stop dropping data at all. Repetitive structured text is compressible &lt;em&gt;losslessly&lt;/em&gt; — you just have to treat it as structure instead of prose.&lt;/p&gt;

&lt;p&gt;If you push a lot of logs, JSON, or CSV into prompts, I'd genuinely like to know what your payloads look like and whether the lossless tradeoff fits your use case. &lt;strong&gt;What's eating the most tokens in your prompts right now?&lt;/strong&gt; Questions, critique, and edge cases that break it are all welcome in the comments.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Repo &amp;amp; docs:&lt;/strong&gt; &lt;a href="https://github.com/antrixy/ctxfold" rel="noopener noreferrer"&gt;https://github.com/antrixy/ctxfold&lt;/a&gt; · &lt;strong&gt;npm:&lt;/strong&gt; &lt;code&gt;npm install ctxfold&lt;/code&gt; · MIT licensed.&lt;/p&gt;

</description>
      <category>llm</category>
      <category>ai</category>
      <category>javascript</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
