How sassine/token-optimizer works
1. Repository shape
- This is a multi-module Java library repo with two supporting subsystems: a Node.js/browser comparison frontend and static documentation.
- The primary deliverable is the Maven library in lib-java/, published to Maven Central. The other directories are for validation, demos, docs, and release automation.
2. Core library boundary
- lib-java/src/main/java/dev/sassine/tokenoptimizer/ is the product boundary.
- The public API is centered on TokenOptimizer, TokenCounter, ToonConverter, and the immutable result/config types: OptimizationResult, OptimizationCriteria, OptimizationPolicy, and PayloadFormat.
- This module is the only part consumers depend on at runtime; examples and frontend are not packaged into the JAR.
3. Primary data flow: input → serialize/convert → score → choose
- The library accepts either a Java object or a JSON string.
- It produces both JSON and TOON representations, counts their size using the selected metric, and returns the winning format plus full comparison metrics.
- The decision boundary is configurable: default behavior optimizes by tokens, but it can also optimize by bytes or characters.
4. Serialization and conversion subsystem
- JSON handling is based on Jackson ObjectMapper.
- TOON conversion is handled by ToonConverter, which implements the TOON v2.0 format described in the README.
- The reverse path exists too: TOON can be converted back to JSON or to a Java object, so the converter is not just one-way transformation.
5. Token-counting subsystem
- TokenCounter provides two counting modes:
- a generic estimator for broad compatibility,
- and model-specific tokenization via jtokkit when a ModelType is supplied.
- This means the architecture separates format conversion from token accounting, so the optimizer can work in either model-agnostic or model-accurate mode.
6. Optimization policy and selection rules
- OptimizationPolicy and PayloadFormat define the selection strategy.
- The repo supports an AUTO mode, plus forced JSON_ONLY and TOON_ONLY behavior.
- There is also a minimum savings threshold, which means the winner is not always the smallest format; the policy layer can prevent format switching when the gain is too small.
7. Result model and metrics
- OptimizationResult is the immutable output boundary that carries:
- optimal format and content,
- JSON/TOON token counts,
- character counts,
- byte counts,
- and savings values for each metric.
- This makes the library useful not just for choosing a payload format, but also for observability and downstream reporting.
8. Validation and testing
- lib-java/src/test/java/ contains unit tests for the core optimizer.
- lib-java/src/examples/ contains runnable demo clients, including a comparison client and a jtokkit example.
- The README emphasizes validation against the official TOON JavaScript library, so the test strategy is partly cross-implementation verification, not only unit-level correctness.
9. Frontend comparison tool
- frontend/ is a separate Node.js-based visual comparison tool.
- It includes a browser UI (index.html, comparison.js, toon-converter.js) and a server (server.js) that uses the official TOON npm library for reference behavior.
- Architecturally, this frontend acts as a golden-model comparison harness: it helps compare the Java implementation against the official TOON implementation.
10. Documentation and static site
- docs/ contains the repo documentation site and assets, likely published via GitHub Pages.
- This is a separate presentation layer from the library and frontend, used for human-readable docs rather than runtime functionality.
11. Build, release, and distribution pipeline
- The Java library is built with Maven and packaged for publication to Maven Central.
- GitHub Actions workflows in .github/workflows/ automate:
- site publishing,
- library publishing,
- and central release publishing.
- This means the repo has a clear separation between source, validation artifacts, documentation publishing, and release distribution.
12. Important architecture boundaries for a diagram
- Draw the core library as the central box, with inputs from Java objects and JSON strings, and outputs to JSON/TOON plus metrics.
- Show two internal decision axes: format conversion and metric-based selection.
- Place the frontend and docs outside the library boundary as supporting systems, and show external dependencies to Jackson, jtokkit, the official TOON spec / npm library, Maven Central, and GitHub Pages.
Main entry points: TokenOptimizer.java TokenOptimizer · publish.yml Workflows · ToonConverter.java ToonConverter · TokenCounter.java TokenCounter
Components 22
Core library
Maven library published for runtime use
- TokenOptimizer · Main optimization entry point and orchestration boundary
lib-java/src/main/java/dev/sassine/tokenoptimizer/TokenOptimizer.java - TokenCounter · Counts payload size by generic or model-specific tokenization
lib-java/src/main/java/dev/sassine/tokenoptimizer/TokenCounter.java - ToonConverter · Converts between JSON, TOON, and Java objects
lib-java/src/main/java/dev/sassine/tokenoptimizer/ToonConverter.java - Criteria · Optimization metric selection and thresholds
lib-java/src/main/java/dev/sassine/tokenoptimizer/OptimizationCriteria.java - Policy · AUTO, JSON_ONLY, and TOON_ONLY selection rules
lib-java/src/main/java/dev/sassine/tokenoptimizer/OptimizationPolicy.java - PayloadFormat · Represents JSON and TOON payload choices
lib-java/src/main/java/dev/sassine/tokenoptimizer/PayloadFormat.java - Result · Immutable optimization result with format, content, and metrics
lib-java/src/main/java/dev/sassine/tokenoptimizer/OptimizationResult.java - Examples · Runnable Java demos for library usage
lib-java/src/examples/java/dev/sassine/tokenoptimizer/Example.java - jtokkit demo · Example showing model-specific token counting
lib-java/src/examples/java/dev/sassine/tokenoptimizer/ExampleWithTiktoken.java - TOON compare · Example client for comparing TOON output
lib-java/src/examples/java/dev/sassine/tokenoptimizer/ToonComparisonClient.java
Comparison frontend
Node.js/browser harness for cross-checking TOON behavior
- Frontend UI · Browser comparison interface for TOON validation
frontend/index.html - Comparison logic · Client-side comparison and display logic
frontend/comparison.js - TOON adapter · Frontend-side conversion helper used in comparisons
frontend/toon-converter.js - Server · Node server that serves the UI and reference behavior
frontend/server.js
Docs site
Static documentation and published pages
- Docs site · Repository documentation and published pages source
docs/index.html
Release automation
Build and publishing workflows
- Workflows · GitHub Actions for site and release publishing
.github/workflows/publish.yml
External systems
Third-party libraries and publishing targets
- Jackson · JSON serialization backbone for the Java library
- jtokkit · Model-specific token counting dependency
- TOON spec · Official TOON behavior and format reference
- TOON npm · Official TOON JavaScript library used by the frontend harness
- Maven Central · Publication target for the Java library
- GitHub Pages · Publication target for the documentation site
Connections 21
- TokenOptimizer → ToonConverter: serialize/convert · Optimizes by converting payloads between JSON and TOON
- TokenOptimizer → TokenCounter: score · Measures candidate payload sizes with the selected metric
- TokenOptimizer → Criteria: uses · Applies metric choice and minimum savings threshold
- TokenOptimizer → Policy: uses · Applies AUTO and forced format policies
- TokenOptimizer → PayloadFormat: returns · Selects the winning payload format
- TokenOptimizer → Result: produces · Returns immutable format, content, and metrics
- ToonConverter → Jackson: JSON I/O · Uses Jackson ObjectMapper for JSON handling
- TokenCounter → jtokkit: model mode · Uses model-specific tokenization when a model type is supplied
- ToonConverter → TOON spec: follows · Implements the TOON v2.0 format described in the repository
- Server → TOON npm: reference · Uses the official TOON npm library for comparison behavior
- Frontend UI → Comparison logic: loads · Browser UI is driven by comparison logic
- Comparison logic → TOON adapter: calls · Uses frontend conversion helper during comparisons
- Server → Frontend UI: serves · Hosts the browser comparison interface
- Examples → TokenOptimizer: demonstrates · Shows standard library usage
- jtokkit demo → TokenCounter: demonstrates · Shows model-specific counting usage
- TOON compare → ToonConverter: demonstrates · Exercises TOON comparison behavior
- Docs site → TOON spec: documents · Static docs explain the format and repo usage
- Workflows → Maven Central: publish · Automates library release publishing
- Workflows → GitHub Pages: publish · Automates docs site deployment
- Workflows → Docs site: builds · Publishes the documentation source
- Workflows → TokenOptimizer: release · Builds and publishes the Java library