<?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: whitz scott</title>
    <description>The latest articles on DEV Community by whitz scott (@whitz_scott_95b51c5db48b1).</description>
    <link>https://dev.to/whitz_scott_95b51c5db48b1</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%2F4267093%2Fe18ed7f8-b6bf-4f63-b4ae-d2f321639099.png</url>
      <title>DEV Community: whitz scott</title>
      <link>https://dev.to/whitz_scott_95b51c5db48b1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/whitz_scott_95b51c5db48b1"/>
    <language>en</language>
    <item>
      <title>Reduce chat tokens by 70%</title>
      <dc:creator>whitz scott</dc:creator>
      <pubDate>Thu, 26 Nov 2026 15:06:20 +0000</pubDate>
      <link>https://dev.to/whitz_scott_95b51c5db48b1/reduce-chat-tokens-by-70-he9</link>
      <guid>https://dev.to/whitz_scott_95b51c5db48b1/reduce-chat-tokens-by-70-he9</guid>
      <description>&lt;h1&gt;
  
  
  Chat Reductions
&lt;/h1&gt;

&lt;p&gt;Long roleplay chats routinely grow into the tens of thousands of tokens or more. Every one of those tokens is sent back to the model on each request, which makes long-running conversations expensive, slow, and prone to context limits. Most hosted chat providers do not expose a retrieval layer, so the entire history is resent on every turn.&lt;/p&gt;

&lt;p&gt;My solution is to build a small RAG system locally. It runs as a thin proxy in front of any OpenAI-compatible API, requires no external vector database, and provisions itself, instead it automatically create everything from the first time it is used.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the RAG works
&lt;/h2&gt;

&lt;p&gt;At a high level, the pipeline follows three stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Summarize once.&lt;/strong&gt; When older messages fall outside a keep-window of recent turns, the proxy groups them into batches of chat exchanges, runs each batch through a summarizer model, and stores the result as a compact memory record with three fields: keypoints, events, and a summary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Embed once.&lt;/strong&gt; Each memory record is embedded with an embedding model and stored alongside its vector in a per-card SQLite table. A record is marked as seen so it is never re-summarized on a later turn.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retrieve later.&lt;/strong&gt; On every new request, the proxy embeds a query built from the latest user message and the tail of the last assistant reply, scores every stored memory against it using a hybrid of cosine similarity, lexical overlap, and recency, deduplicates the results with MMR, and injects the top matches into the request as a system message.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The important property is that summarization and embedding happen once per chunk, not once per request. A request whose history is already summarized only pays for a single query embedding, not a full re-summarization of the conversation.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the code works
&lt;/h2&gt;

&lt;p&gt;The proxy is designed to be dropped in front of any client that speaks the OpenAI chat completions protocol.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;You build the request normally.&lt;/strong&gt; Your client sends a standard POST to the chat completions endpoint with the usual model, messages, and optional parameters. The base URL simply points at the local proxy instead of the upstream provider.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You configure a model and an API.&lt;/strong&gt; At the top of the code, you supply an upstream API URL, a summarizer model name, and an embedding model name. The same API key your client sends is reused for the main model, the summarizer, and the embeddings, so no extra credentials are needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The proxy handles the rest.&lt;/strong&gt; It parses the request, detects a card command, decides what is old enough to summarize, groups the old messages into chunks, and calls the summarizer and embedding models through your configured API. Once a chunk is summarized and embedded, it is stored and marked as seen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retrieval and forwarding.&lt;/strong&gt; After compression, the proxy builds a retrieval query, scores the stored memories, and injects the most relevant ones into the outgoing message list. The final body is forwarded to the main model exactly as the client would have sent it, minus the messages that were summarized away.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No database is created by hand. On first run, the proxy creates its own SQLite file, provisions the shared tables, and adds per-card tables for vectors and seen-hashes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this gives you
&lt;/h2&gt;

&lt;p&gt;The end result is fast and simple in practice. Long chats stop growing linearly in cost: once history has been summarized, it contributes a fixed, bounded memory block instead of thousands of raw tokens. Nothing is lost permanently either, because summarized messages remain recoverable through their memory records, which are injected back into the conversation whenever they are relevant.&lt;/p&gt;

&lt;p&gt;The whole design was for roleplay and other long-context conversations&lt;/p&gt;

&lt;h2&gt;
  
  
  Before and after
&lt;/h2&gt;

&lt;p&gt;This shows the before and after of a prompt, when using the local rag vs not using it. &lt;/p&gt;

&lt;p&gt;Before:&lt;br&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%2Fh7jzwxulho4e8bkmsbun.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%2Fh7jzwxulho4e8bkmsbun.png" alt=" " width="794" height="49"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After: &lt;br&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%2Fvloh7ftzpvjsy6hdniqh.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%2Fvloh7ftzpvjsy6hdniqh.png" alt=" " width="800" height="35"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CODE
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://colab.research.google.com/drive/1JgbwhIpTpVaX1T5yt_chAIpkmzRKrRFG?usp=sharing" rel="noopener noreferrer"&gt;https://colab.research.google.com/drive/1JgbwhIpTpVaX1T5yt_chAIpkmzRKrRFG?usp=sharing&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>fastapi</category>
      <category>development</category>
    </item>
  </channel>
</rss>
