# Claude token counts via the free count_tokens API (needs ANTHROPIC_API_KEY set; the account needs a credit balance).
# count_tokens wraps the text in a user message, so the message overhead is measured with "x" (1 token) and subtracted.
import re, html, sys, pathlib, anthropic
client = anthropic.Anthropic()
here = pathlib.Path(__file__).parent
Q = [("English", "Where is my order?"), ("Tamil", "என் ஆர்டர் எங்க இருக்கு?"), ("Tanglish", "En order enga iruku?"),
     ("Telugu", "నా ఆర్డర్ ఎక్కడ ఉంది?"), ("Hindi", "मेरा ऑर्डर कहाँ है?")]
A = [("English", "Your order has been shipped. It will reach you tomorrow by 6 PM."),
     ("Tamil", "உங்கள் ஆர்டர் அனுப்பப்பட்டது. நாளை மாலை 6 மணிக்குள் உங்களுக்கு வந்து சேரும்."),
     ("Tanglish", "Unga order ship aayiduchu. Naalai saayangalam 6 manikulla vandhudum."),
     ("Telugu", "మీ ఆర్డర్ పంపబడింది. రేపు సాయంత్రం 6 గంటలకు మీకు చేరుతుంది."),
     ("Hindi", "आपका ऑर्डर भेज दिया गया है। यह कल शाम 6 बजे तक आप तक पहुँच जाएगा।")]
W = [("order", "order"), ("ஆர்டர்", "ஆர்டர்")]
def udhr(l):
    x = (here / "udhr" / f"udhr_{l}.xml").read_text(encoding="utf-8")
    return html.unescape("\n".join(re.findall(r"<para>(.*?)</para>", x, re.S)))
U = [(l, udhr(l)) for l in ["eng", "tam", "hin", "tel"]]
for model in sys.argv[1:] or ["claude-sonnet-5-5"]:
    n = lambda s: client.messages.count_tokens(model=model, messages=[{"role": "user", "content": s}]).input_tokens
    base = n("x") - 1
    print(f"== {model} (message overhead {base})")
    for title, rows in [("question", Q), ("reply", A), ("word", W), ("UDHR", U)]:
        counts = [(k, n(s) - base) for k, s in rows]
        e = counts[0][1]
        print(f"  {title:8} " + "  ".join(f"{k} {c} ({c/e:.1f}x)" for k, c in counts))
