# izikeros/count_tokens architecture

> Architecture diagram of the GitHub repository izikeros/count_tokens, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/izikeros/count_tokens
- Repository: https://github.com/izikeros/count_tokens

## Overview

##### Main Components
a. CLI Entrypoint
- Script: `count_tokens` (installed as a console script)
- Responsibilities: parse command-line arguments (file vs. directory, encoding, streaming, format, limits) and dispatch to the library.

b. Core Library
- Module: `count_tokens/count.py`
- Exposes:
- Simplified API (`count(...)`)
- Original API (`count_tokens_in_file`, `count_tokens_in_string`)
- Responsibilities:
- Mode (string vs. file vs. directory)
- Handle streaming large files (chunking, memory constraints)
- Apply exact or approximate token-counting rules
- Aggregate results

c. File-System & Streaming I/O
- Reads files or walks directories (with patterns and recursion)
- For large files, reads in configurable chunks

d. Tokenizer Engine (External Service)
- Dependency: `tiktoken` library
- Responsibilities: actual tokenization based on selected encoding (cl100k_base, r50k_base, etc.)

e. Output Formatter
- Formats results as plain text, JSON, or CSV
- Emits warnings for token-limit exceedance

f. Packaging & CI
- Build & test configs (`pyproject.toml`, `poetry.toml`, `pytest.ini`)
- Linters & security checks (`.flake8`, `.ruff.toml`, `.bandit`)
- GitHub Actions (`.github/workflows/publish.yaml`)
- Pre-commit rules (`.pre-commit-config.yaml`)

g. Test Suite
- Directory: `tests/`
- Uses pytest to validate functionality against sample docs

##### Relationships & Data Flows
- Packaging & CI surround everything as a separate layer that builds, lints, and publishes

##### Architectural Patterns & Principles
- Separation of concerns: CLI layer vs. core processing vs. formatting vs. I/O vs. external tokenization
- Single responsibility: each module does one job (e.g., streaming reader, directory walker, tokenizer wrapper)
- Testability: core logic decoupled from CLI parsing, making it easy to call via API in tests

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User-Facing Layer
    subgraph "User-Facing Layer"
        User(["User"]):::external
        CLI(["CLI: count_tokens script\n(parse args & dispatch)"]):::cli
    end

    %% Processing Layer
    subgraph "Processing Layer"
        Core(["Core: count_tokens/count.py\n(count(), count_tokens_in_file(), count_tokens_in_string())"]):::core
        FSIO(["File-System & Streaming I/O\n(directory walk, chunked reads)"]):::external
        TokenizerExt(["Tokenizer Engine: tiktoken"]):::external
    end

    %% Output Layer
    subgraph "Output Layer"
        Formatter(["Output Formatter\n(plain, JSON, CSV; warnings)"]):::formatter
    end

    %% Support Layer
    subgraph "Support Layer"
        subgraph "Packaging & CI"
            P1["pyproject.toml"]:::packaging
            P2["poetry.toml"]:::packaging
            P3["pytest.ini"]:::packaging
            P4[".flake8"]:::packaging
            P5[".ruff.toml"]:::packaging
            P6[".bandit"]:::packaging
            P7[".pre-commit-config.yaml"]:::packaging
            P8[".github/workflows/publish.yaml"]:::packaging
        end
        subgraph "Test Suite"
            T1["tests/test_count.py"]:::test
            T2["tests/conftest.py"]:::test
            T3["tests/docs/"]:::test
        end
    end

    %% Connections
    User -->|"stdin args & flags"| CLI
    CLI -->|"call count() or original APIs"| Core
    Core -->|"file paths & options"| FSIO
    FSIO -->|"text chunks / content"| Core
    Core -->|"text chunks"| TokenizerExt
    TokenizerExt -->|"token ids & counts"| Core
    Core -->|"aggregated results"| Formatter
    Formatter -->|"stdout or file"| User
    T1 -->|"tests API and CLI"| Core
    T1 -->|"tests via CLI"| CLI
    T2 --> Core
    T3 --> FSIO

    %% Click Events
    click CLI "https://github.com/izikeros/count_tokens/blob/main/count_tokens/count.py"
    click Core "https://github.com/izikeros/count_tokens/blob/main/count_tokens/count.py"
    click FSIO "https://github.com/izikeros/count_tokens/blob/main/count_tokens/count.py"
    click TokenizerExt "https://github.com/izikeros/count_tokens/blob/main/pyproject.toml"
    click Formatter "https://github.com/izikeros/count_tokens/blob/main/count_tokens/count.py"
    click P1 "https://github.com/izikeros/count_tokens/blob/main/pyproject.toml"
    click P2 "https://github.com/izikeros/count_tokens/blob/main/poetry.toml"
    click P3 "https://github.com/izikeros/count_tokens/blob/main/pytest.ini"
    click P4 "https://github.com/izikeros/count_tokens/blob/main/.flake8"
    click P5 "https://github.com/izikeros/count_tokens/blob/main/.ruff.toml"
    click P6 "https://github.com/izikeros/count_tokens/blob/main/.bandit"
    click P7 "https://github.com/izikeros/count_tokens/blob/main/.pre-commit-config.yaml"
    click P8 "https://github.com/izikeros/count_tokens/blob/main/.github/workflows/publish.yaml"
    click T1 "https://github.com/izikeros/count_tokens/blob/main/tests/test_count.py"
    click T2 "https://github.com/izikeros/count_tokens/blob/main/tests/conftest.py"
    click T3 "https://github.com/izikeros/count_tokens/tree/main/tests/docs/"

    %% Styles
    classDef external fill:#b3d9ff,stroke:#333,stroke-width:1px
    classDef core fill:#d3f8d3,stroke:#333,stroke-width:1px
    classDef cli fill:#fff2b8,stroke:#333,stroke-width:1px
    classDef formatter fill:#fff2b8,stroke:#333,stroke-width:1px
    classDef packaging fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef test fill:#cccccc,stroke:#333,stroke-width:1px
```
