snchimata/tokenfold

Private, provider-neutral context compression for LLMs. Reversibly compress schemas and logs inside your perimeter with zero third-party calls.

What it solves

LLM context windows fill up quickly, especially when the payload is structured data: JSON records, tool schemas, or grouped context blocks. Tokenfold shrinks those payloads so more material fits within a chosen token ceiling. The core path is deterministic and model-free -- no GPU, no model downloads, no network calls -- while an optional, experimental component (Tokenfold Select) ranks context groups by relevance to a query for an extra layer of selection.

How it works

Tokenfold has two cooperating parts:

Core (structural compression, model-free). Core removes structural redundancy instead of prose selected by a language model. For uniform JSON arrays, it detects repeated keys and folds them into a columnar shape: a compact representation with column names and rows of values. Every lossless transform is verified by an exact in-memory decode round trip before anything is emitted, and a never-larger guard means small inputs pass through unchanged. Fitting is offline: the SDK's fit() accepts text, JSON, grouped context, or full provider-shaped requests, targets an explicit token count, and returns a machine-readable receipt. If the budget cannot be met, checked access raises an error alongside the receipt instead of silently returning overflow. Opt-in, recoverable pruning can rank and omit eligible rows, storing the omissions locally behind retrieval markers ($tf_ref handles), and the pruned payload is used only if it beats the lossless baseline.

Select (optional relevance ranking, experimental). When explicitly provisioned and consented to per fit, a 149.6M-parameter joint-group scorer ranks context groups by relevance to a query; dependent content can be kept in one group or marked as required. Scoring is deliberately unqualified: it preserves caller-declared dependencies, not proof of answer quality. The core never loads Select weights unless they are deliberately configured.

Integration surfaces. Python is the primary SDK, with TypeScript alongside it and Rust at the core. There is also a CLI for files and stdin, an MCP server for coding agents (with pre-configured filters for git status, diffs, build output, and test logs), and a drop-in proxy for OpenAI-compatible clients: point an existing client at 127.0.0.1:8787, and the proxy applies Core's lossless transforms to chat-shaped requests while forwarding everything else untouched and passing the authorization header through unchanged.

Who it's for

Python, TypeScript, and Rust developers and AI teams who need to fit JSON, tool definitions, and grouped context into LLM requests under a token budget. Coding-agent, CLI, and proxy integrations all use the same underlying Core engine, so the same compression logic works across environments.

Highlights

  • Lossless structural compression measured at 45.6-63.9% on two fixtures (exact JSON-value recovery); never-larger guard protects small inputs
  • In saved local comparisons, Tokenfold beat Headroom on 6/6 fixtures (33.3% fewer tokens) and TOON on 7/7 (13.9% fewer)
  • Deterministic, model-free, offline core -- no model, GPU, or ML framework needed for the base workflow
  • Opt-in recoverable pruning reached up to 96.3% fewer tokens in a showcase, with omissions stored behind local $tf_ref handles rather than lossless inline compression
  • One fit() workflow across text, JSON, grouped context, and provider requests, each returning an inspectable receipt
  • Multiple entry points: Python SDK, TypeScript SDK, Rust crate, CLI, MCP server, and a drop-in OpenAI-compatible proxy with per-request measurement events
  • Select is explicitly experimental and unqualified, and it never runs unless you provision and consent to it

Related

  • Project
  • Project
  • Project
  • Project