KORP AI — token cost & token-saving snippets (2026-06-02)
Working code companions to the post
AI Chatbot ต้นทุนต่อข้อความเท่าไหร่? คำนวณค่า token จริงต่อเดือน.
MIT licensed — adapt freely.
- token-cost-calculator.js — per-turn / per-conversation / per-month cost in THB across budget/mid/premium tiers, with compounding history growth.
- thai-token-estimator.py — quick token estimate for mixed Thai/English text + a
thai_premium() showing the 2–3× Thai token tax. (Use the vendor tokenizer for billing.)
- sliding-window-history.js — cap conversation history to a token budget (keep last N, optional summary). History is the #1 silent cost driver.
- rag-topk-limiter.py — greedy-pack only the highest-scoring RAG chunks that fit a token budget; drop low-similarity context instead of stuffing whole docs.
- prompt-cache-key.js — build a byte-stable cacheable prefix so prompt caching actually hits (50–90% input discount); keep volatile per-user data out of the prefix.
- deterministic-faq-router.js — regex-first router that answers hours/address/phone/parking for 0 tokens, falling through to the LLM only on a miss.
← all snippets · blog · korpai.co