Context Slimming: Reducing Token Context Without Losing Quality
I’m Denis Shokhirev, an Enterprise AI architect based in Erlangen, Germany. At DennisCraft AI Studio, I deliver production AI agents for DACH B2B clients in logistics, fintech, and industrial automation—stacking Claude, Supabase, n8n, Doppler, and self-hosted Postgres. On at least 8 of my last 14 agents, I hit a recurring wall: context windows ballooning past LLM limits, leading to latency spikes and unpredictable outputs despite careful prompt design. Why Context Swells in Real Workloads For
I’m Denis Shokhirev, an Enterprise AI architect based in Erlangen, Germany. At DennisCraft AI Studio, I deliver production AI agents for DACH B2B clients in logistics, fintech, and industrial automation—stacking Claude, Supabase, n8n, Doppler, and self-hosted Postgres. On at least 8 of my last 14 agents, I hit a recurring wall: context windows ballooning past LLM limits, leading to latency spikes and unpredictable outputs despite careful prompt design.
Why Context Swells in Real Workloads
For every real-world agent handling regulated data, context bloat is a fact of life. Even models like Claude 3 Opus support up to 200,000 tokens (Anthropic docs, 2024), but in practice, pipelines often break long before hitting that ceiling. The pain points I see most:
- Logistics: Full shipment histories, technical logs, compliance checklists—one session can top 15K tokens.
- Fintech: Multi-year transaction chains, sometimes 5000+ lines per user, all needing real-time analysis.
- Industrial: Equipment logs, maintenance records, error traces—often 10K+ tokens per diagnostic run.
Context overload hits latency, cost, and reliability. Models degrade as prompts get crowded with irrelevant or old data, and token overflows cause silent truncation or outright errors.
Five Production Patterns for Context Slimming
| Pattern | When to Use | Tools |
|---|---|---|
| Semantic Chunking | Unstructured documents, legal text, large reports | Claude Code, OpenAI cookbook |
| Retrieval-Augmented Generation (RAG) | Knowledge bases, historic data search | Supabase (pgvector), Postgres |
| Sliding Window Pruning | Long-running dialogs, chatbots | n8n, custom Python |
| On-the-fly Summarization | Logs, event streams, transaction chains | Claude, OpenAI GPT-4, n8n |
| Schema-based Filtering | Structured data, API responses | Pydantic, custom scripts |
Semantic Chunking and RAG: Real-World Implementations
Semantic Chunking
I never split documents by “n tokens”—it always leads to broken sentences and context loss. Instead, I chunk by semantic units: sections, paragraphs, or even numbered clauses. Using Python with sentence-transformers and tiktoken, here’s a practical chunking approach:
from sentence_transformers import SentenceTransformer
import tiktoken
model = SentenceTransformer('all-MiniLM-L6-v2')
tokenizer = tiktoken.get_encoding('cl100k_base')
def semantic_chunks(text, max_tokens=1024):
sentences = text.split('. ')
current_chunk = []
current_len = 0
for sent in sentences:
token_len = len(tokenizer.encode(sent))
if current_len + token_len > max_tokens:
yield ' '.join(current_chunk)
current_chunk = [sent]
current_len = token_len
else:
current_chunk.append(sent)
current_len += token_len
if current_chunk:
yield ' '.join(current_chunk)
This keeps context coherent, especially on regulatory or technical documents.
RAG with Supabase and pgvector
For anything search-heavy, RAG is the only scalable option. I index documents or logs into Supabase with pgvector, then search for top-N relevant chunks before passing them to the LLM. Typical n8n pipeline:
// n8n RAG step
items = $input.all();
const query = items[0].json.query;
const results = await $supabase
.from('documents')
.select('content, embedding')
.order('embedding', { ascending: false })
.limit(5);
return results.data;
This consistently compresses context from 20K+ tokens to 1-2K without sacrificing relevance or accuracy.
Sliding Window Pruning and Summarization
Sliding Window Pruning
Dialog agents become unusable if the chat history grows unchecked. I always enforce a sliding window: last 5-7 messages plus a running summary for background. This keeps the prompt under 2-3K tokens, critical for stable latency. Example pruning logic:
def prune_context(messages, max_tokens=2048):
pruned = []
total = 0
for msg in reversed(messages):
tokens = len(tokenizer.encode(msg['content']))
if total + tokens > max_tokens:
break
pruned.insert(0, msg)
total += tokens
return pruned
On-the-fly Summarization
For logs or transaction flows, I run GPT-4 or Claude summaries every 1000 events—preserving anomalies and outliers, not just averages. This pattern is especially strong in fintech, where abnormal activity matters more than routine data.
Schema-based Filtering: Let Structure Do the Work
With structured data (JSON, API responses), I filter to only relevant fields before building prompts using Pydantic or custom code. This “cold” filtering drops noise and prevents LLMs from choking on useless data:
from pydantic import BaseModel
class Transaction(BaseModel):
id: str
amount: float
date: str
status: str
def filter_transactions(raw):
txs = [Transaction(**t) for t in raw if t['status'] == 'active']
return txs
FAQ
How much token savings can you expect?
In my own prod deployments, slimming patterns cut prompt size by 5–10x (20K to 2–4K tokens) with no quality loss. I measure with OpenAI’s tokenizer and track latency/cost over time.
Does Claude handle large context better than OpenAI?
Yes—Claude 3 Opus allows up to 200K tokens, but prompt slimming still matters. Latency and cost scale non-linearly as context grows.
When is RAG better than summarization?
RAG is best for document search and knowledge base lookups. For event streams or chat, summarization is more efficient and keeps context focused.
Any security risks with aggressive slimming?
Absolutely. You risk dropping sensitive fragments. I use semgrep, bandit, and gitleaks to scan for security-relevant patterns before and after slimming.
Should you test each slimming stage?
Yes. It’s easy to lose critical context if you skip validation. I always cover pipelines with unit tests (pytest) and manual reviews.
In your own LLM pipelines, where does context bloat hit hardest—during data gathering, agent handoffs, or logging? I’m interested in real-world pain points. I offer a free 30-min stack audit for DACH founders building AI in regulated sectors. DM me on LinkedIn or write to @ger_dennis_ai.
Turn your process into an AI system
Fixed price. Production quality. DACH B2B focus.