dx-serializer 0.1.0

A token-efficient serialization format for LLM context windows with high-performance binary encoding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Token counting for LLM serializer output
//!
//! Provides token counting and measurement for various LLM models:
//! - **GPT-4o / o1**: Uses `o200k_base` tokenizer via tiktoken-rs
//! - **Gemini 3**: Uses SentencePiece tokenizer via tokenizers crate
//! - **Claude Opus 4.5**: Uses approximate BPE tokenizer
//!
//! ## Usage
//!
//! ```rust
//! use serializer::llm::tokens::{TokenCounter, ModelType, TokenInfo};
//!
//! let counter = TokenCounter::new();
//! let text = "Hello, world!";
//!
//! // Count tokens for GPT-4o
//! let info = counter.count(text, ModelType::Gpt4o);
//! assert!(info.count > 0);
//! println!("Token count: {}", info.count);
//! ```

use std::collections::HashMap;

/// Supported LLM model types for token counting
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModelType {
    /// OpenAI GPT-4o (uses o200k_base tokenizer)
    Gpt4o,
    /// OpenAI o1 model (uses o200k_base tokenizer)
    O1,
    /// OpenAI GPT-4 (uses cl100k_base tokenizer)
    Gpt4,
    /// Google Gemini 3 (uses SentencePiece tokenizer)
    Gemini3,
    /// Anthropic Claude Opus 4.5 (uses BPE tokenizer)
    ClaudeOpus45,
    /// Anthropic Claude Sonnet 4 (uses BPE tokenizer)
    ClaudeSonnet4,
    /// Generic "Other" model (uses average tokenization)
    Other,
}

impl std::fmt::Display for ModelType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ModelType::Gpt4o => write!(f, "GPT-4o"),
            ModelType::O1 => write!(f, "o1"),
            ModelType::Gpt4 => write!(f, "GPT-4"),
            ModelType::Gemini3 => write!(f, "Gemini 3"),
            ModelType::ClaudeOpus45 => write!(f, "Claude Opus 4.5"),
            ModelType::ClaudeSonnet4 => write!(f, "Claude Sonnet 4"),
            ModelType::Other => write!(f, "Other"),
        }
    }
}

/// Token information returned from counting
#[derive(Debug, Clone)]
pub struct TokenInfo {
    /// Total number of tokens
    pub count: usize,
    /// Token IDs (if available)
    pub ids: Vec<u32>,
    /// Token strings (decoded tokens)
    pub tokens: Vec<String>,
    /// Model used for counting
    pub model: ModelType,
}

impl TokenInfo {
    /// Create a new TokenInfo
    pub fn new(count: usize, ids: Vec<u32>, tokens: Vec<String>, model: ModelType) -> Self {
        Self {
            count,
            ids,
            tokens,
            model,
        }
    }

    /// Create TokenInfo with just count (for models without ID access)
    pub fn count_only(count: usize, model: ModelType) -> Self {
        Self {
            count,
            ids: Vec::new(),
            tokens: Vec::new(),
            model,
        }
    }
}

/// Token counter for multiple LLM models
///
/// Provides unified interface for counting tokens across different models.
/// Uses model-specific tokenizers internally.
pub struct TokenCounter {
    // Note: Caching was considered but removed as token counting is fast enough
    // that the overhead of cache management outweighs the benefits for typical use cases.
    // If profiling shows token counting as a bottleneck, caching can be re-added.
}

impl TokenCounter {
    /// Create a new token counter
    pub fn new() -> Self {
        Self {}
    }

    /// Count tokens for the given text and model
    ///
    /// # Arguments
    /// * `text` - The text to tokenize
    /// * `model` - The model type to use for tokenization
    ///
    /// # Returns
    /// TokenInfo containing count, IDs, and decoded tokens
    pub fn count(&self, text: &str, model: ModelType) -> TokenInfo {
        match model {
            ModelType::Gpt4o | ModelType::O1 => self.count_openai_o200k(text, model),
            ModelType::Gpt4 => self.count_openai_cl100k(text, model),
            ModelType::Gemini3 => self.count_gemini(text, model),
            ModelType::ClaudeOpus45 | ModelType::ClaudeSonnet4 => self.count_claude(text, model),
            ModelType::Other => self.count_other(text, model),
        }
    }

    /// Count tokens using OpenAI o200k_base tokenizer (GPT-4o, o1)
    fn count_openai_o200k(&self, text: &str, model: ModelType) -> TokenInfo {
        // Use tiktoken-rs o200k_base tokenizer
        #[cfg(feature = "tiktoken")]
        {
            use tiktoken_rs::o200k_base;
            if let Ok(bpe) = o200k_base() {
                let tokens = bpe.encode_with_special_tokens(text);
                let decoded: Vec<String> = tokens
                    .iter()
                    .filter_map(|&id| bpe.decode(vec![id]).ok())
                    .collect();
                return TokenInfo::new(tokens.len(), tokens, decoded, model);
            }
        }

        // Fallback: approximate token count (4 chars per token average)
        self.approximate_token_count(text, model, 4.0)
    }

    /// Count tokens using OpenAI cl100k_base tokenizer (GPT-4)
    fn count_openai_cl100k(&self, text: &str, model: ModelType) -> TokenInfo {
        #[cfg(feature = "tiktoken")]
        {
            use tiktoken_rs::cl100k_base;
            if let Ok(bpe) = cl100k_base() {
                let tokens = bpe.encode_with_special_tokens(text);
                let decoded: Vec<String> = tokens
                    .iter()
                    .filter_map(|&id| bpe.decode(vec![id]).ok())
                    .collect();
                return TokenInfo::new(tokens.len(), tokens, decoded, model);
            }
        }

        // Fallback: approximate token count
        self.approximate_token_count(text, model, 4.0)
    }

    /// Count tokens using Gemini/Gemma tokenizer
    fn count_gemini(&self, text: &str, model: ModelType) -> TokenInfo {
        #[cfg(feature = "tokenizers")]
        {
            use tokenizers::Tokenizer;
            // Try to load Gemma tokenizer from known paths
            let paths = [
                "tokenizers/gemma-tokenizer.json",
                "~/.cache/huggingface/tokenizers/google/gemma-3/tokenizer.json",
            ];

            for path in &paths {
                if let Ok(tokenizer) = Tokenizer::from_file(path) {
                    if let Ok(encoding) = tokenizer.encode(text, false) {
                        let ids: Vec<u32> = encoding.get_ids().to_vec();
                        let tokens: Vec<String> = encoding
                            .get_tokens()
                            .iter()
                            .map(|s| s.to_string())
                            .collect();
                        return TokenInfo::new(ids.len(), ids, tokens, model);
                    }
                }
            }
        }

        // Fallback: Gemini uses ~3.5 chars per token
        self.approximate_token_count(text, model, 3.5)
    }

    /// Count tokens using Claude tokenizer
    fn count_claude(&self, text: &str, model: ModelType) -> TokenInfo {
        // Claude tokenizer is approximated since no official local tokenizer exists
        // Use HuggingFace tokenizers crate with community Claude tokenizer if available
        #[cfg(feature = "tokenizers-hf")]
        {
            use tokenizers::Tokenizer;
            // Try to load Claude tokenizer from known paths
            let paths = [
                "tokenizers/claude-tokenizer.json",
                "~/.cache/huggingface/tokenizers/anthropic/claude/tokenizer.json",
            ];

            for path in &paths {
                if let Ok(tokenizer) = Tokenizer::from_file(path) {
                    if let Ok(encoding) = tokenizer.encode(text, false) {
                        let ids: Vec<u32> = encoding.get_ids().to_vec();
                        let tokens: Vec<String> = encoding
                            .get_tokens()
                            .iter()
                            .map(|s| s.to_string())
                            .collect();
                        return TokenInfo::new(ids.len(), ids, tokens, model);
                    }
                }
            }
        }

        // Fallback: Claude uses ~3.8 chars per token
        self.approximate_token_count(text, model, 3.8)
    }

    /// Count tokens using generic "Other" model (average tokenization)
    fn count_other(&self, text: &str, model: ModelType) -> TokenInfo {
        // Use an average of ~3.7 chars per token (between Claude and OpenAI)
        self.approximate_token_count(text, model, 3.7)
    }

    /// Approximate token count when tokenizer is not available
    fn approximate_token_count(
        &self,
        text: &str,
        model: ModelType,
        chars_per_token: f64,
    ) -> TokenInfo {
        let count = (text.len() as f64 / chars_per_token).ceil() as usize;
        TokenInfo::count_only(count.max(1), model)
    }

    /// Count tokens for all supported models
    pub fn count_all(&self, text: &str) -> HashMap<ModelType, TokenInfo> {
        let models = [
            ModelType::Gpt4o,
            ModelType::O1,
            ModelType::Gpt4,
            ModelType::Gemini3,
            ModelType::ClaudeOpus45,
            ModelType::ClaudeSonnet4,
            ModelType::Other,
        ];

        models
            .iter()
            .map(|&model| (model, self.count(text, model)))
            .collect()
    }

    /// Count tokens for the 4 primary models (OpenAI, Claude, Gemini, Other)
    /// as required by the token efficiency display feature.
    ///
    /// Returns counts for:
    /// - OpenAI (GPT-4o)
    /// - Claude (Sonnet 4)
    /// - Gemini (Gemini 3)
    /// - Other (generic model)
    pub fn count_primary_models(&self, text: &str) -> HashMap<ModelType, TokenInfo> {
        let models = [
            ModelType::Gpt4o,         // OpenAI representative
            ModelType::ClaudeSonnet4, // Claude representative
            ModelType::Gemini3,       // Gemini representative
            ModelType::Other,         // Generic model
        ];

        models
            .iter()
            .map(|&model| (model, self.count(text, model)))
            .collect()
    }

    /// Get a summary of token counts for all models
    pub fn summary(&self, text: &str) -> String {
        let counts = self.count_all(text);
        let mut lines = vec![format!("Token counts for {} chars:", text.len())];

        for model in [
            ModelType::Gpt4o,
            ModelType::Gemini3,
            ModelType::ClaudeOpus45,
        ] {
            if let Some(info) = counts.get(&model) {
                lines.push(format!("  {}: {} tokens", model, info.count));
            }
        }

        lines.join("\n")
    }
}

impl Default for TokenCounter {
    fn default() -> Self {
        Self::new()
    }
}

/// Measure token efficiency of dx format vs other formats
pub struct TokenEfficiencyMeasurement {
    /// Original text (e.g., JSON)
    pub original: TokenInfo,
    /// DX format text
    pub dx_format: TokenInfo,
    /// Savings percentage
    pub savings_percent: f64,
}

impl TokenEfficiencyMeasurement {
    /// Calculate token savings
    pub fn calculate(original: TokenInfo, dx_format: TokenInfo) -> Self {
        let savings = if original.count > 0 {
            ((original.count as f64 - dx_format.count as f64) / original.count as f64) * 100.0
        } else {
            0.0
        };

        Self {
            original,
            dx_format,
            savings_percent: savings,
        }
    }
}

/// Extension trait for adding token counting to dx format serializer
pub trait TokenCountExt {
    /// Get token count for the serialized output
    fn token_count(&self, model: ModelType) -> TokenInfo;

    /// Get token counts for all models
    fn token_counts(&self) -> HashMap<ModelType, TokenInfo>;
}

impl TokenCountExt for String {
    fn token_count(&self, model: ModelType) -> TokenInfo {
        let counter = TokenCounter::new();
        counter.count(self, model)
    }

    fn token_counts(&self) -> HashMap<ModelType, TokenInfo> {
        let counter = TokenCounter::new();
        counter.count_all(self)
    }
}

impl TokenCountExt for str {
    fn token_count(&self, model: ModelType) -> TokenInfo {
        let counter = TokenCounter::new();
        counter.count(self, model)
    }

    fn token_counts(&self) -> HashMap<ModelType, TokenInfo> {
        let counter = TokenCounter::new();
        counter.count_all(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_token_counter_creation() {
        let counter = TokenCounter::new();
        let info = counter.count("Hello, world!", ModelType::Gpt4o);
        assert!(info.count > 0);
    }

    #[test]
    fn test_count_all_models() {
        let counter = TokenCounter::new();
        let counts = counter.count_all("Hello, world!");

        assert!(counts.contains_key(&ModelType::Gpt4o));
        assert!(counts.contains_key(&ModelType::Gemini3));
        assert!(counts.contains_key(&ModelType::ClaudeOpus45));
        assert!(counts.contains_key(&ModelType::Other));
    }

    #[test]
    fn test_count_primary_models() {
        let counter = TokenCounter::new();
        let counts = counter.count_primary_models("Hello, world!");

        // Should have exactly 4 models
        assert_eq!(counts.len(), 4);
        assert!(counts.contains_key(&ModelType::Gpt4o));
        assert!(counts.contains_key(&ModelType::ClaudeSonnet4));
        assert!(counts.contains_key(&ModelType::Gemini3));
        assert!(counts.contains_key(&ModelType::Other));

        // All counts should be non-zero
        for info in counts.values() {
            assert!(info.count > 0, "Token count should be non-zero");
        }
    }

    #[test]
    fn test_other_model() {
        let counter = TokenCounter::new();
        let info = counter.count("Hello, world!", ModelType::Other);
        assert!(info.count > 0);
        assert_eq!(info.model, ModelType::Other);
    }

    #[test]
    fn test_token_efficiency_measurement() {
        let original = TokenInfo::count_only(100, ModelType::Gpt4o);
        let dx = TokenInfo::count_only(73, ModelType::Gpt4o);
        let measurement = TokenEfficiencyMeasurement::calculate(original, dx);

        assert!((measurement.savings_percent - 27.0).abs() < 0.1);
    }

    #[test]
    fn test_empty_string() {
        let counter = TokenCounter::new();
        let info = counter.count("", ModelType::Gpt4o);
        assert_eq!(info.count, 1); // Minimum 1 token
    }

    #[test]
    fn test_model_display() {
        assert_eq!(format!("{}", ModelType::Gpt4o), "GPT-4o");
        assert_eq!(format!("{}", ModelType::Gemini3), "Gemini 3");
        assert_eq!(format!("{}", ModelType::ClaudeOpus45), "Claude Opus 4.5");
    }

    #[test]
    fn test_summary() {
        let counter = TokenCounter::new();
        let summary = counter.summary("Hello, world!");
        assert!(summary.contains("Token counts"));
        assert!(summary.contains("GPT-4o"));
    }

    #[test]
    fn test_token_count_extension() {
        let text = "Hello, world!";
        let info = text.token_count(ModelType::Gpt4o);
        assert!(info.count > 0);
    }

    #[test]
    fn test_dx_format_token_efficiency() {
        // Compare JSON vs DX format for same data
        // Use a larger example to show more significant savings
        let json = r#"{"name":"dx-serializer","version":"0.1.0","description":"Binary-first serialization format for LLMs","workspace":["frontend/www","frontend/mobile","backend/api","backend/workers"],"dependencies":{"serde":"1.0","bincode":"2.0","tokio":"1.0"},"enabled":true,"count":42}"#;
        let dx = "nm=dx-serializer ver=0.1.0 ds=\"Binary-first serialization format for LLMs\" ws:frontend/www,frontend/mobile,backend/api,backend/workers deps.serde=1.0 deps.bincode=2.0 deps.tokio=1.0 en=true ct=42";

        let counter = TokenCounter::new();
        let json_tokens = counter.count(json, ModelType::Gpt4o);
        let dx_tokens = counter.count(dx, ModelType::Gpt4o);

        // Store counts before moving
        let json_count = json_tokens.count;
        let dx_count = dx_tokens.count;

        // DX format should use fewer tokens
        let measurement = TokenEfficiencyMeasurement::calculate(json_tokens, dx_tokens);
        println!(
            "JSON: {} tokens, DX: {} tokens, Savings: {:.1}%",
            measurement.original.count, measurement.dx_format.count, measurement.savings_percent
        );

        // DX should be more efficient (or at least not worse)
        // Small examples may not show significant savings due to tokenizer overhead
        assert!(
            measurement.savings_percent >= 0.0 || dx_count <= json_count,
            "DX format should not be worse than JSON"
        );
    }
}