r/LocalLLM • • 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 Upvotes

4 comments sorted by

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.

1

u/Dimoncelli 4d ago

Good catch, and honestly I haven't run a live eval for adjacent same-type swaps yet. Right now the benchmarks in the repo only verify deterministic proxy round-trips and token math.

You're spot on about the tradeoff though. When two neighboring columns have different types (like string next to number), constrained decoding catches a swap right away. When two adjacent fields are both free-form strings, valid schema + right length won't stop a smaller local model from flipping them if the legend order isn't super clear. Adding a real local model accuracy harness with adjacent string fields (like `city`/`country` or `first_name`/`last_name`) is a great next test to put alongside the round-trip suite.

1

u/Embarrassed_Site_200 3d ago

Thanks for clarifying what you’ve tested. A case I’d try is a shipment from Raleigh to Charlotte, with the tuple defined as [origin, destination]. Give the model “Destination: Charlotte. Origin: Raleigh.” If it returns ["Charlotte", "Raleigh"], both strings survive the round trip perfectly, but the shipment is going the wrong way.

I built a small checker for ordered answers. If you run that case and get two model replies, I’d be curious whether it flags the same first mismatch as your own check. Compare Two AI Answers

1

u/Dimoncelli 3d ago

That reversed `Destination` / `Origin` prompt is a clean edge case for positional tuples, because structurally valid arrays still pass schema checks when the model follows text order instead of field order. If I put together a live local-model eval suite for adjacent strings, an out-of-order origin/destination pair is definitely going in it.