본문 바로가기
C.W.K.
Stream
Lesson 01 of 05 · published

Token은 단어가 아니야 — BPE 60초 정리

~22 min · tokenization, bpe, measurement

Level 0Window Watcher
0 XP0/50 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

모형은 조각으로 읽는다

token은 단어일 수도 있고 단어 일부일 수도 있어. 구두점 하나, 공백 하나, Unicode 조각 하나일 수도 있고. tokenizer는 사람 직관이 아니라 학습 데이터에 뭐가 자주 나왔느냐로 최적화되거든. 영어 산문이랑 코드, JSON, 한글, URL, stack trace가 token을 제각각 — 가끔은 자릿수가 달라질 만큼 — 다르게 먹는 게 그래서야.

BPE 60초 요약

요즘 LLM은 대부분 byte-pair encoding(BPE) 계열을 써. 만드는 순서는 이래. 1) single byte 256개로 vocab을 시작한다. 2) 학습 corpus에서 제일 자주 붙어 다니는 쌍을 찾는다. 3) 그 쌍을 하나로 합쳐 vocab에 넣는다. 4) 목표 vocab 크기(보통 50K-200K)가 될 때까지 반복. 결과가 이거야. 자주 쓰는 영어 표현은 token 하나로 떨어지고, 희귀한 용어나 non-Latin 문자는 잘게 부서져.

눈대중은 그만

예산이 걸린 일이면 provider tokenizer를 돌려. 문단 하나 정도는 어림해도 괜찮아. 200K token짜리 session을 어림하면 안 되고 — 30%쯤 빗나간 걸 청구서나 잘려나간 답변으로 알게 되거든.

token 비용은 재봐야 아는 거야. 작업 규모가 좀 된다 싶으면 세. 재보기 전까지 어림은 그냥 어림일 뿐이고.

Code

tiktoken으로 측정 (OpenAI/GPT 계열)·python
from tiktoken import encoding_for_model

enc = encoding_for_model("gpt-5")
text = open("lesson.md", "r", encoding="utf-8").read()
print(f"{len(enc.encode(text))} tokens")
Anthropic count_tokens·python
from anthropic import Anthropic
client = Anthropic()

count = client.messages.count_tokens(
    model="claude-sonnet-4-7",
    messages=[{"role": "user", "content": open("lesson.md").read()}],
)
print(count.input_tokens)
대충 감 잡는 비율·text
English prose:                ~4 chars / token
English code:                 ~3 chars / token
JSON / structured text:       ~3.5 chars / token
Korean / Japanese / Chinese:  ~1-1.5 chars / token  (영어 비용 2-3배)
Emoji / unusual Unicode:      ~0.25-0.5 chars / token

External links

Exercise

눈으로 봤을 때 길이가 비슷한 영어 문단 하나, 한국어 문단 하나, 코드 조각 하나를 골라봐. tiktoken이나 count_tokens로 token을 세고, 어느 쪽이 의외였는지 설명해봐.
Hint
눈에 보이는 길이는 거짓말해. token 하나에 뜻이 얼마나 실렸느냐가 진짜 지표야.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.