Token Lab検索

← チュートリアル一覧
開発者ガイド / 01

トークンとトークナイザー:テキストからモデル入力へ

トークンの意味を理解し、実際のエンコーディングと可逆性を確認します。

トークンは単語とは限らない

モデルが受け取るのはトークンIDです。トークナイザーは語彙とルールに従って文字列を整数列へ変換します。よく使われる単語が1トークンになる一方、珍しい名前は複数に分かれます。空白、句読点、改行も影響します。

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の4つです。色付きトークンを選んで確認し、カンマを削除したり大文字を変えたりして比較してください。

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
ツールで試す →

実装前に検証する

全トークン列をデコードして原文と比較します。1つのトークンが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