Token Lab검색

← 모든 튜토리얼
개발자 가이드 / 01

토큰과 토크나이저: 텍스트에서 모델 입력까지

토큰의 의미를 이해하고 실제 인코딩 결과와 원문 복원을 확인합니다.

토큰은 단어가 아닙니다

모델은 토큰 ID를 받습니다. 토크나이저는 어휘와 인코딩 규칙으로 텍스트를 정수 시퀀스로 바꿉니다. 흔한 단어는 한 토큰이 될 수 있지만 낯선 이름은 여러 토큰으로 나뉠 수 있습니다. 공백, 구두점, 줄바꿈도 영향을 줍니다.

BPE는 학습된 병합 규칙으로 하위 단어 단위를 구성합니다. 화면의 조각은 디버깅용이며 언어학적 단어 분할과 같지 않습니다.

Study guide & lesson outlineLEARNING CONTRACT

Understand → implement → investigate

Goal: explain the mechanism in this lesson, reproduce one example and test a failure case. Reading alone does not mark mastery.

  • Understand: state assumptions, shapes/units and a counterexample.
  • Implement: reproduce a numerical example and inspect boundaries.
  • Investigate: compare one alternative under a matched budget.

재현 가능한 예제 실행

cl100k_base를 선택하고 쉼표와 공백을 포함해 Hello, world!를 입력하세요. 결과는 9906, 11, 1917, 0의 네 ID입니다. 각 색상 블록을 선택한 뒤 쉼표나 대소문자를 바꾸며 비교하세요.

o200k_base로 바꾸면 어휘가 달라져 ID나 개수도 변할 수 있습니다. ID는 인코딩과 함께 해석해야 하므로 내보낼 때 둘 다 보관하세요.

import { encode, decode } from
  'gpt-tokenizer/encoding/cl100k_base';

const text = 'Hello, world!';
const ids = encode(text);
// [9906, 11, 1917, 0]
console.log(ids.length); // 4
console.log(decode(ids) === text); // true
도구에서 실행 →

통합 전에 검증하기

전체 시퀀스를 디코딩하고 원문과 비교하세요. 토큰 하나가 UTF-8 문자의 일부만 포함하면 단독으로는 �로 보여도 전체 시퀀스는 정확히 복원됩니다.

이 도구는 텍스트를 계산하며 전체 API 요청을 재현하지 않습니다. 역할 표시, 도구 정의, 미디어는 사용량을 추가할 수 있습니다. 과금이나 엄격한 한도에 적용하기 전에 API usage와 비교하세요.

참고 자료 및 구현

MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

Round-trip decode(encode(text)) equals the original text. Individual tokens may split UTF-8 sequences; do not require each token to decode independently.

Shared code, English mathematical explanation and key multilingual numeric passages reviewed. This is not a full translation proofread.

Unicode text → UTF-8 bytes → merge vocabulary → Token IDs → count → Decode full sequence → round trip
Mechanism map: follow the transformations from top to bottom.

Before running: predict a boundary case

Round-trip decode(encode(text)) equals the original text. Individual tokens may split UTF-8 sequences; do not require each token to decode independently.

Hint: isolate the assumption

Keep the baseline input fixed, change one parameter or boundary, and compare both the numerical result and the claimed units. A passing example verifies this fixture only.

Minimal executable example

Save the example and linked modules in one folder. Use Node.js 22 or newer, then run node tokens-and-tokenizers.mjs. npm install gpt-tokenizer@4.1.0

import { encode, decode } from
  'gpt-tokenizer/encoding/cl100k_base';

const text = 'Hello, world!';
const ids = encode(text);
// [9906, 11, 1917, 0]
console.log(ids.length); // 4
console.log(decode(ids) === text); // true

Test and transfer

Round-trip decode(encode(text)) equals the original text. Individual tokens may split UTF-8 sequences; do not require each token to decode independently.

Record input, code version, expected result and observed result in the notebook below. Then construct a counterexample that violates the assumption above and explain why the original conclusion no longer follows.

Download complete kit (.tar.gz) · Complete reference kit and regression checks

Predict, test and keep evidence

Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.

Open notebook / export / clear →

The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope