feat: add rtk toon subcommand and filter for token-optimized JSON encoding#2897
Open
vishalneelkant wants to merge 1 commit into
Open
feat: add rtk toon subcommand and filter for token-optimized JSON encoding#2897vishalneelkant wants to merge 1 commit into
vishalneelkant wants to merge 1 commit into
Conversation
Author
|
| Data Structure | JSON (Indented) Tokens | JSON (Minified) Tokens | TOON Tokens | Savings vs Indented | Savings vs Minified |
|---|---|---|---|---|---|
| Simple Object (Metadata/Config) | 40 | 36 | 30 | 24.1% | 16.7% |
| REST API Response (Uniform Array) | 229 | 168 | 84 | 63.6% | 50.3% |
| Deeply Nested Structure | 140 | 71 | 61 | 56.8% | 14.5% |
Detailed Results
1. Simple Object (Metadata/Config)
- Use Case: Config files, single database entry lookups, feature flags.
- Conversion Benefit: Removes braces, spaces, quotes on keys/values, and trailing commas.
- Analysis:
- Raw JSON (158 chars):
{ "name": "Service Registry", "version": "1.4.2", "status": "healthy", "uptime_seconds": 86400, "debug_mode": false, "environment": "production" } - TOON (120 chars):
name: Service Registry version: "1.4.2" status: healthy uptime_seconds: 86400 debug_mode: false environment: production - Result: Saves 24.1% tokens over indented JSON, and 16.7% over minified JSON.
- Raw JSON (158 chars):
2. REST API Response (Uniform Array of Objects)
- Use Case: Standard API endpoints returning search results, lists, or tables of records (e.g. users, products, log entries).
- Conversion Benefit: This is where TOON shines. By declaring the schema once in the header (
results[5]{id,first_name,last_name,email,role,active}:), we completely avoid repeating the object keys ("id","first_name", etc.) for every single row. - Analysis:
- Raw JSON (914 chars): Paginated response with 5 user objects.
- TOON (333 chars):
page: 1 per_page: 5 total: 150 results[5]{id,first_name,last_name,email,role,active}: 101,John,Doe,john.doe@example.com,admin,true 102,Jane,Smith,jane.smith@example.com,user,true 103,Bob,Johnson,bob.j@example.com,user,false 104,Alice,Brown,alice.b@example.com,editor,true 105,Charlie,Green,charlie.g@example.com,user,false - Result: Saves 63.6% tokens over indented JSON, and 50.3% tokens over minified JSON. This cuts token cost in half!
3. Deeply Nested Structure
- Use Case: Multi-level hierarchical trees, complex domain models.
- Conversion Benefit: Replaces heavy braces nesting with clean 2-space indentation.
- Analysis:
- Raw JSON (558 chars): Organization -> department -> teams.
- TOON (241 chars):
organization: name: Acme Corp departments[2]: - name: Engineering teams[2]{name,lead,size}: Core Platform,Alice,8 Frontend UI,Bob,5 - name: Marketing teams[1]{name,lead,size}: Growth,Charlie,3 - Result: Saves 56.8% tokens over indented JSON, and 14.5% over minified JSON.
Conclusion
TOON is an exceptionally efficient representation format for feeding data into LLMs:
- REST API Responses: You can expect 50% to 65% token savings when converting standard paginated lists/results arrays.
- Deterministic & Less Ambiguity: In addition to saving tokens, the tabular format is highly readable and less prone to JSON bracket parsing errors by LLMs.
- Safety Guard (
never_worse): Becausertkguards output usingnever_worse, in the rare case where TOON structure is longer than JSON (e.g. extremely small objects), it automatically falls back to raw JSON.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Walkthrough —
rtk toonSubcommand & FilterWe have implemented and verified the new
rtk toonfeature, which converts JSON documents to Token-Oriented Object Notation (TOON) v3.3 to optimize token usage when sending API responses or structured data to LLMs.Changes Made
1. TOON Encoder Module
{}and:whitespace)[N]:)-prefix list items) for mixed/non-uniform array items-0to0)toonin mod.rs2.
rtk toonCommand & IntegrationCommandsenum, CLI command router, and exit-code mapping in main.rs."toon"toRTK_META_COMMANDSin constants.rs to declare it as a internal tool command.3. Piped Stream Filter
"toon"filter resolver in pipe_cmd.rs to support piped stream transformation, e.g.curl ... | rtk pipe --filter toon.Validation Results
Automated Tests
Ran the full test suite and all 2420 tests passed successfully:
All unit tests in
toon.rspassed, validating:Manual Verification
Tested the compiled debug binary against a sample JSON file, stdin input, and pipe filter:
$ ./target/debug/rtk toon test.json users[2]{id,name}: 1,Ada 2,Linus $ cat test.json | ./target/debug/rtk toon - users[2]{id,name}: 1,Ada 2,Linus $ cat test.json | ./target/debug/rtk pipe --filter toon users[2]{id,name}: 1,Ada 2,LinusAll output styles perfectly match the expected TOON v3.3 spec format.