cd /news/large-language-models/cut-llm-prompt-tokens-on-structured-… · home topics large-language-models article
[ARTICLE · art-41788] src=dev.to ↗ pub= topic=large-language-models verified=true sentiment=↑ positive

Cut LLM prompt tokens on structured data — losslessly

A developer released ctxfold, a dependency-free tool that losslessly compresses structured data like logs, JSON, and CSV in LLM prompts by re-encoding repetitive structure into a compact table format. It achieves ~35-40% token reduction on templated data without dropping any information, verified by built-in encode-decode checks. The tool is available on npm and works with any LLM as a pure text transform.

read3 min views1 publishedJun 27, 2026

A small, dependency-free tool for shrinking logs, JSON, and CSV in prompts — without dropping a single byte.

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.

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

Ask:

"How many errors are in this log?"

"What's the total across these 400 rows?"

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

ctxfold takes the opposite approach. Its single rule:

Lossless or no-op. Never lossy.

Instead of summarizing, it re-encodes structure. 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. Nothing is dropped.

The guarantee is enforced in code: every encoder ships with a decoder, and compress()

verifies that decoding its output reproduces the input before returning it. If it can't, you get your original text back, untouched. It can't corrupt your data — worst case, it does nothing.

Yes. On real data, ctxfold cuts ~35–40% of tokens 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.

(Readability is validated on GPT-4o-mini; the lossless guarantee is model-independent.)

npm install ctxfold
js
const { compress } = require("ctxfold");

const { text, stats } = compress(bigLogOrJsonOrCsv);
// send `text` instead of the original
console.log(`${(stats.tokenRatio * 100).toFixed(0)}% fewer tokens, lossless: ${stats.lossless}`);

It's a pure text transform — no API calls, no model, zero dependencies — so it works with any LLM.

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

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 losslessly — you just have to treat it as structure instead of prose.

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. What's eating the most tokens in your prompts right now? Questions, critique, and edge cases that break it are all welcome in the comments.

Repo & docs: https://github.com/antrixy/ctxfold · npm: npm install ctxfold

· MIT licensed.

── more in #large-language-models 4 stories · sorted by recency
── more on @ctxfold 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/cut-llm-prompt-token…] indexed:0 read:3min 2026-06-27 ·