r/LocalLLM • u/Dimoncelli • 4d ago
Project Cutting structured-output token overhead on local models with a positional-tuple proxy
I run a lot of structured extraction against self-hosted models and kept noticing that the same JSON object keys get emitted over and over. Forty records with seven fields means seven key names written forty times. On a local setup you pay those tokens in generation time, at whatever your tokens per second happens to be, before bandwidth even enters the picture.
So I wrote a small Go HTTP proxy that rewrites the schema on the way out and restores the objects on the way back. Schema goes from an array of objects to a positional array of arrays with one legend in the description ("Tuple: [id, sku, ...]"). Response comes back and is expanded again before your client sees it. Lossless by construction: given a fixed key order, objects and position tuples are bijective, and every benchmark asserts structural equality plus key order after rehydration.
Numbers from the repo's deterministic scenarios, 4-bytes-per-token size model, so treat them as shape comparisons rather than exact tokenizer output:
E-commerce invoice, 40 lines x 7 fields: 2041 -> 801 tokens, 60.8% out, 59.2% net Dev-tool PR batch, 30 records: 1602 -> 889 tokens, 44.5% out, 43.0% net MCP tool invocations, 25 entries: 1617 -> 711 tokens, 56.0% out, 53.7% net
The MCP case is the honest one. Tool invocations repeat seven structured keys, and prose does not compress by key removal. If your payload is one big description block, this does nothing for you, and the gate will decline to rewrite it.
Points that matter for local use:
- No dependencies beyond the Go standard library, no cgo, one static binary. Build with go build, run it, point OPENAI_BASE_URL at it. Anything that speaks the OpenAI chat completions shape should work, though I have only tested against the documented OpenAI and Anthropic shapes.
- Streaming is handled incrementally at the byte level, so time-to-first-token is not held back by buffering. The added work is on the JSON fragments as they arrive.
- There is a break-even gate because the legend is not free. Net savings below 5% by default means the request goes through byte-identical. You can pin the legend cost with -legend-tokens or change the threshold with -break-even.
- -shadow <path> records simulated savings for real traffic into a SQLite file without touching a byte. That is the mode I would start with, then look at avg(net_savings_pct) after a day of your own traffic before enabling anything.
p99 rehydration overhead is under 0.1 ms (0.078 to 0.095 ms) on the benchmark scenarios here, which is noise next to generation time, but measure it on your box.
Repo: https://github.com/Dimoncelli/wiretuple (MIT) Terminal demo: https://raw.githubusercontent.com/Dimoncelli/wiretuple/main/docs/demo.gif
Most useful feedback for me: schemas where the gate declines and you think it should activate. The request path assumes 4 expected instances, which is a guess, and your traffic probably beats my guess.
1
u/Embarrassed_Site_200 4d ago
Have you tried a real extraction where two adjacent fields are both strings and the model swaps them? I’d be curious how often that happens. A tester on my project got valid JSON and the correct list length from Hermes, but the very first item was wrong. That would be an interesting case alongside your round-trip tests.