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
//! Token counting library for LLM models
//!
//! This library provides exact tokenization for various LLM models using their official tokenizers.
//!
//! # Features
//!
//! - **Exact tokenization** for OpenAI models (GPT-3.5, GPT-4, GPT-4 Turbo, GPT-4o)
//! - **Model aliases** with case-insensitive matching
//! - **Fuzzy suggestions** for typos and unknown models
//! - **Zero runtime dependencies** - all tokenizers embedded
//! - **Fast and efficient** - ~2.7µs for small inputs
//!
//! # Quick Start
//!
//! ```
//! use token_count::count_tokens;
//!
//! // Count tokens for a specific model
//! let result = count_tokens("Hello world", "gpt-4", false, 0).unwrap();
//! assert_eq!(result.token_count, 2);
//! println!("Tokens: {}", result.token_count);
//! println!("Model: {}", result.model_info.name);
//! ```
//!
//! # Supported Models
//!
//! - `gpt-3.5-turbo` - GPT-3.5 Turbo (16K context)
//! - `gpt-4` - GPT-4 (128K context)
//! - `gpt-4-turbo` - GPT-4 Turbo (128K context)
//! - `gpt-4o` - GPT-4o (128K context)
//!
//! All models support aliases (e.g., `gpt4`, `GPT-4`, `openai/gpt-4`).
//!
//! # Error Handling
//!
//! ```
//! use token_count::{count_tokens, TokenError};
//!
//! // Unknown model returns an error with suggestions
//! match count_tokens("test", "gpt-5", false, 0) {
//! Ok(_) => panic!("Should have failed"),
//! Err(TokenError::UnknownModel { model, suggestion }) => {
//! assert_eq!(model, "gpt-5");
//! assert!(suggestion.contains("Did you mean"));
//! }
//! Err(_) => panic!("Wrong error type"),
//! }
//! ```
//!
//! # Architecture
//!
//! The library is organized into several modules:
//!
//! - [`tokenizers`] - Core tokenization engine and model registry
//! - [`output`] - Output formatting (simple, verbose, debug)
//! - [`cli`] - Command-line interface components
//! - [`error`] - Error types and handling
//! - [`api`] - API integration utilities (consent prompts, etc.)
//!
//! The main entry point is the [`count_tokens`] function, which takes text and a model name
//! and returns a [`TokenizationResult`] with the token count and model information.
pub use TokenError;
pub use ;
pub use ;
/// Count tokens in the given text using the specified model
///
/// # Arguments
///
/// * `text` - The text to tokenize
/// * `model_name` - The model to use for tokenization (e.g., "gpt-4", "claude-sonnet-4-6")
/// * `accurate` - Whether to use accurate mode for models that support it (Claude API)
/// * `verbosity` - Verbosity level (3+ includes detailed token information for debug mode)
///
/// # Returns
///
/// A `Result` containing the token count, model information, and optionally token details
///
/// # Errors
///
/// Returns `TokenError` if:
/// - The model is not supported
/// - The tokenizer fails to initialize
/// - Token counting fails
///
/// # Example
///
/// ```
/// use token_count::count_tokens;
///
/// let result = count_tokens("Hello world", "gpt-4", false, 0).unwrap();
/// assert_eq!(result.token_count, 2);
/// ```