Skip to main content

Component Overview

Tokenizador consists of four main components, each with specific responsibilities:

TokenAnalyzer

Application orchestrator that coordinates all other components and manages the application lifecycle

TokenizationService

Tokenization engine that integrates with tiktoken and provides fallback tokenization

UIController

UI manager that handles all DOM manipulation, user input, and visual updates

StatisticsCalculator

Computation engine that calculates metrics, costs, and context utilization

TokenAnalyzer

The TokenAnalyzer class is the main application orchestrator. It creates and coordinates all other components, handling the overall application lifecycle and user workflows.

Responsibilities

  • Initialize all services and controllers
  • Coordinate communication between components
  • Handle user actions (text change, model change, clear)
  • Manage error handling and warnings
  • Provide export functionality

Component Diagram

Key Methods

Creates instances of all three core components and calls init().
Initializes the application by setting up event handlers and waiting for tiktoken to load.
The core analysis workflow that tokenizes text, calculates statistics, and updates the UI.
Compares tokenization across multiple models.
Exports analysis results in JSON, CSV, or TXT format.

Usage Example


TokenizationService

The TokenizationService handles all tokenization logic, including integration with the tiktoken library and providing fallback tokenization when tiktoken is unavailable.

Responsibilities

  • Initialize and manage the tiktoken encoder
  • Tokenize text using the appropriate encoding
  • Create token objects with type classification
  • Provide fallback tokenization when tiktoken fails
  • Apply model-specific token ratios

Component Diagram

Key Methods

Asynchronously loads and initializes the tiktoken library.
The main tokenization method that processes text using tiktoken or fallback.
Creates visual token objects from tiktoken encoding with type classification.
Token Types:
  • palabra - Regular word token
  • espacio_en_blanco - Whitespace
  • number - Numeric token
  • punctuation - Punctuation mark
  • palabra_con_espacio - Word with leading space
  • subword - Subword/partial word token
Provides approximate tokenization when tiktoken is unavailable.

Token Structure

Each token object returned by the service has this structure:

Usage Example


UIController

The UIController manages all user interface interactions, DOM manipulation, and visual updates. It serves as the presentation layer of the application.

Responsibilities

  • Initialize and cache DOM element references
  • Handle user input events (keyboard, mouse)
  • Update statistics displays
  • Render token visualizations
  • Manage UI state (loading, empty, error)
  • Provide visual feedback (highlighting, scrolling)

Component Diagram

Key Methods

Caches all DOM element references for performance.
Caching element references improves performance by avoiding repeated DOM queries.
Attaches event listeners to DOM elements.
Updates all statistics displays with formatted values.
Renders color-coded token spans in the main visualization area.
Renders the detailed token list with IDs and types.
Highlights a specific token in the list and scrolls it into view.

Event Handler Pattern

The UIController uses a callback pattern for loose coupling:

Usage Example


StatisticsCalculator

The StatisticsCalculator performs all statistical computations including token counts, costs, context utilization, and efficiency metrics.

Responsibilities

  • Calculate comprehensive text statistics
  • Estimate API costs based on token counts
  • Compute context window utilization
  • Generate context limit warnings
  • Compare statistics across models
  • Calculate efficiency metrics

Component Diagram

Key Methods

Computes all statistics for a given text and model.
Returns:
Calculates the estimated API cost based on token count.
Cost calculation uses input pricing since the text being analyzed represents input to the model. Output costs are shown separately for reference.
Computes what percentage of the model’s context window is being used.
Returns appropriate warning messages based on context utilization.
Warning Thresholds:
  • 100%+: Critical - exceeds context limit
  • 90-99%: Warning - approaching limit
  • 75-89%: Info - high utilization
  • Under 75%: No warning
Compares statistics across multiple models for the same text.
Calculates efficiency metrics for deeper analysis.

Statistics Object Structure

A complete statistics object contains:

Usage Example


Component Interactions

Here’s a comprehensive example showing how all components work together:

Summary

TokenAnalyzer

Role: Application OrchestratorKey Features:
  • Coordinates all components
  • Handles user workflows
  • Manages application lifecycle
  • Provides export functionality

TokenizationService

Role: Tokenization EngineKey Features:
  • Integrates with tiktoken
  • Classifies token types
  • Provides fallback tokenization
  • Applies model-specific ratios

UIController

Role: Presentation ManagerKey Features:
  • Manages DOM interactions
  • Handles user input
  • Updates visualizations
  • Provides visual feedback

StatisticsCalculator

Role: Computation EngineKey Features:
  • Calculates comprehensive stats
  • Estimates costs
  • Compares models
  • Generates warnings

Next Steps

Architecture Overview

Return to the architecture overview

API Reference

Explore the complete API documentation

How to Use

Learn how to use the application

Contributing

Contribute to Tokenizador