"""Count how many tokens the same text costs in different languages, on the tokenizer your model uses.

    pip install tiktoken
    python count_tokens.py english.txt hindi.txt amharic.txt          # the first file is the baseline
    python count_tokens.py --encoding cl100k_base english.txt hindi.txt  # GPT-4 / GPT-3.5's tokenizer

Each file should hold the same text (a real translation of your prompt, your docs or your typical user message).
o200k_base is the tokenizer of GPT-4o, GPT-4.1, the o-series and GPT-5; cl100k_base is GPT-4's and GPT-3.5's. Other
providers use their own tokenizers: count with their token-counting endpoint or tokenizer instead, because the ratios
differ a lot between tokenizers.
"""
import argparse
import tiktoken


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("files", nargs="+")
    ap.add_argument("--encoding", default="o200k_base")
    ap.add_argument("--price", type=float, default=None, help="your model's price per 1M input tokens, to show cost")
    a = ap.parse_args()
    enc = tiktoken.get_encoding(a.encoding)
    rows = []
    for path in a.files:
        text = open(path, encoding="utf-8").read()
        rows.append((path, len(text), len(enc.encode(text, disallowed_special=()))))
    base = rows[0][2]
    print(f"tokenizer: {a.encoding}")
    for path, chars, n in rows:
        cost = f"  ${n * a.price / 1e6:.6f}" if a.price else ""
        print(f"{path:30s} {chars:8d} chars {n:8d} tokens  {n / base:5.2f}x{cost}")


if __name__ == "__main__":
    main()
