Dev.to AI πŸ€– Ai πŸ‘ 2 πŸ“– 2 min read

Terset: an open source format that sends JSON records to an LLM with fewer tokens

Terset is open source (MIT). It is a compact text format for sending lists of JSON records to a language model. We built it at StackDeck because our prompts were full of JSON lists. JSON repeats every key in every recor

Terset is open source (MIT). It is a compact text format for sending lists of JSON records to a language model.

We built it at StackDeck because our prompts were full of JSON lists. JSON repeats every key in every record. The model reads each repeat as input tokens. We were paying for the same key names again and again.

Terset writes the list as one table. The keys go in a header once. Each record becomes a row. decode turns the text back into the same JSON, key order included.

The same data, two ways

Compact JSON:

{"team":"core","users":[{"id":"u1","name":"Ada","role":"admin","active":true,"logins":41},{"id":"u2","name":"Ben","role":"ops","active":true,"logins":7},{"id":"u3","name":"Chen","role":"ops","active":false,"logins":0},{"id":"u4","name":"Dara","active":true,"logins":12,"note":"on leave"}]}

Terset:

team: core
users[4]:
id  name    role    active  logins  note
u1  Ada admin   true    41  
u2  Ben ops true    7   
u3  Chen    ops false   0   
u4  Dara        true    12  on leave

In real Terset the gaps are tab characters. Dara has no role and a note the others lack. The record still sits in the table.

You also send one short rules line before the data, so the model knows how to read it:

Tables use tab-separated headers and rows. Empty cells mean absent. A leading bare id header marks string IDs.

The numbers

Counted with the open o200k_base tokenizer, rules line included:

Format Tokens (4 records) Tokens (10 records)
Compact JSON 99 233
Terset plus rules 78 138

Over our 11 benchmark cases, Terset 1.5 used 28.3% fewer tokens than compact JSON. It used 8.5% fewer than the best form of TOON, another table format for prompts.

The benchmark decodes every Terset output and checks it against its input before counting it. All cases and the full prompt are on the benchmark page: https://terset.dev/benchmark

Use it

JavaScript:

npm install @stackdeck/terset
import { encode, decode, rulesFor } from "@stackdeck/terset";

const text = encode(data); // the data, in the user message
const rules = rulesFor(text); // right before the data

Python:

pip install terset
from terset import encode, decode, rules_for

text = encode(data)  # the data, in the user message
rules = rules_for(text)  # right before the data

Both packages write the same text and pass the same format tests. Neither one installs anything else.

Try it

Paste your own JSON and see the count: https://terset.dev/#b2

Code: https://github.com/stackdeckhq/terset

What does the JSON in your prompts look like? We would like to test Terset on shapes we have not seen.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.