Token Lab検索

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

言語・絵文字・JSONでトークン数が変わる理由

多言語の入力を比較し、文字・バイト・トークンの違いを理解します。

3つの単位を区別する

Unicodeコードポイントはテキスト、UTF-8バイトは保存形式、トークンは特定の語彙による表現です。見た目が1つの絵文字でも複数のコードポイントを含み、1つのコードポイントが複数のバイトやトークンになることがあります。

英語で約4文字が1トークンという目安は、多言語の予算には使えません。実際のプロンプトとエンコーディングで計測します。

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.

入力を固定して比較

多言語と絵文字の例を読み込み、エンコーディング比較を実行します。同じ入力が両方で計算されます。続いて日本語、中国語、スペイン語、韓国語の実際の入力を試し、比率が一定ではないことを確認します。

JSONにはキー、引用符、句読点、空白が加わります。整形版と圧縮版を比較しましょう。空白削除はバイトを減らしますが、トークンへの影響は周囲の文字とエンコーディング次第です。

import { encode } from 'gpt-tokenizer/encoding/cl100k_base';
// npm install gpt-tokenizer@4.1.0
const samples = [
  'Hello, world!', '你好,世界!',
  '¡Hola, mundo!', 'こんにちは、世界!',
  '안녕하세요, 세계!', '👨‍👩‍👧‍👦'
];
for (const text of samples) {
  const ids = encode(text);
  console.log(text, ids.length);
}
ツールで試す →

見た目より意味を最適化

不要なフィールド、重複文書、無関係な履歴から削除し、制約と根拠を残します。見た目の短さはトークンの少なさを保証せず、少ないトークンが同じ回答品質を保証するわけでもありません。

多言語の回帰テストを用意し、変更後は個数と回答の正しさを同時に確認します。別のモデルの分詞器に対して、この2つのエンコーディングを精密な計数器として使わないでください。

参考資料と実装

MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

Hold encoding and text fixed. Record UTF-8 bytes, code points and tokens separately; the English 4-characters-per-token rule is not a multilingual formula.

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

Equivalent texts in several languages → Hold tokenizer encoding fixed → Count bytes, code points and tokens → Compare measurements, not heuristics
Mechanism map: follow the transformations from top to bottom.

Before running: predict a boundary case

Hold encoding and text fixed. Record UTF-8 bytes, code points and tokens separately; the English 4-characters-per-token rule is not a multilingual formula.

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 multilingual-tokenization.mjs. npm install gpt-tokenizer@4.1.0

import { encode } from 'gpt-tokenizer/encoding/cl100k_base';
// npm install gpt-tokenizer@4.1.0
const samples = [
  'Hello, world!', '你好,世界!',
  '¡Hola, mundo!', 'こんにちは、世界!',
  '안녕하세요, 세계!', '👨‍👩‍👧‍👦'
];
for (const text of samples) {
  const ids = encode(text);
  console.log(text, ids.length);
}

Test and transfer

Hold encoding and text fixed. Record UTF-8 bytes, code points and tokens separately; the English 4-characters-per-token rule is not a multilingual formula.

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