Overview
TheTokenAnalyzer class is the main orchestrator of the Tokenizador application. It coordinates the tokenization service, UI controller, and statistics calculator to provide real-time token analysis and visualization.
This is the entry point for the entire application. It initializes all services and manages the application lifecycle.
Constructor
Creates a new TokenAnalyzer instance and initializes all dependent services.- Instantiates
TokenizationService,UIController, andStatisticsCalculator - Calls
init()to set up event handlers and wait for tokenization initialization - Configures the UI with the selected model
Methods
init()
Initializes the application and sets up event handlers.Returns a promise that resolves when initialization is complete
- Configures event handlers for text changes, model changes, and clear actions
- Waits for the tokenization service to initialize tiktoken
- Triggers initial model change to configure the UI
handleTextChange()
Handles changes in the text input field.performRealTimeAnalysis() to tokenize and analyze the new text.
handleModelChange()
Handles changes in the model selection dropdown.Returns a promise that resolves when the model change is processed
- Gets the newly selected model ID
- Updates model information display (context limit, tokenizer type, pricing)
- Re-analyzes the current text with the new model
handleClear()
Handles the clear button click.performRealTimeAnalysis()
Performs real-time tokenization analysis on the current text.Returns a promise that resolves when analysis is complete
- Gets current text and selected model
- Returns early if text is empty (resets displays)
- Shows loading state in UI
- Tokenizes text using
TokenizationService - Calculates statistics using
StatisticsCalculator - Updates all UI displays with results
- Shows context warnings if applicable
getState()
Retrieves the current state of the application.Object containing current application state
The current text in the input field
The currently selected model ID (e.g., “gpt-4o”)
Whether the tokenization service has finished initializing
Array of all available model IDs from MODELS_DATA
compareModels()
Compares tokenization results across multiple models for the current text.Array of model IDs to compare
Array of comparison objects sorted by cost (cheapest first)
exportResults()
Exports the current analysis results in the specified format.Export format:
"json", "csv", or "txt"Formatted string containing the exported data
- JSON
- CSV
- TXT
Complete data with timestamp, model info, statistics, and token details
updateDisplays()
Updates all UI visualization components with analysis results.Result object from
tokenizeText() containing tokens array and countStatistics object from
calculateStatistics()- Statistics display (token count, character count, word count, cost)
- Visual token representation with color coding
- Token list with IDs and types
resetDisplays()
Resets all visualizations to empty state.showContextWarnings()
Displays warnings if the text exceeds or approaches context limits.Calculated statistics object
ID of the model to check limits against
- 100%+: Text exceeds context limit
- 90-99%: Near context limit
- 75-89%: High context usage
Usage Example
Dependencies
The TokenAnalyzer requires these services to be loaded:TokenizationService
Handles tiktoken integration and tokenization logic
UIController
Manages DOM manipulation and user interactions
StatisticsCalculator
Calculates token statistics and cost estimates
Error Handling
The TokenAnalyzer includes comprehensive error handling:Errors during initialization and analysis are logged to console and displayed to users through the UI.
See Also
TokenizationService
Learn about the tokenization engine
UIController
Explore UI management methods
StatisticsCalculator
Understand statistics calculations
Architecture
View the complete architecture