📖

Token とは?

AIにおけるトークン:言語モデルが読み書きする基本単位。トークン化の仕組み、コストやコンテキストにどう影響するか、そしてモデルの挙動をどう形づくるかを解説します。

トークンとは、言語モデルが実際に扱うテキストの最小単位です。GPT、Claude、Llama のようなモデルにプロンプトを送ると、まずテキストは一連のトークンに分割されます。通常は単語全体、頻出するサブワード、または単一文字です。その後、各トークンはモデルが処理できる数値に変換されます。モデルの出力生成も同じ仕組みで行われ、停止を判断するまで 1 トークンずつ予測して出力します。

トークンの仕組み

トークンはトークナイザーによって生成されます。トークナイザーはテキストとモデルの間に位置する独立したプログラムです。最も一般的な方式是バイトペアエンコーディング(BPE)WordPieceで、いずれもまず個々の文字から始め、出現頻度の高い隣接ペアを繰り返しマージしてより長い単位を作り上げていきます。その結果として、固定語彙(通常 30,000〜200,000 エントリ)が得られ、短い頻出語と再利用できるサブワードピースがバランス良く含まれます。the のような頻出語は通常 1 つのトークンになりますが、unbelievableness のような造語や稀少な語は複数のトークン(unbelievableness)に分割されます。

英語では平均して 1 トークンあたり約 4 文字になるため、おおよその目安として100 トークン ≈ 75 英語単語ですが、これはトークナイザーや言語によって異なります。価格、コンテキスト制限、生成速度はすべて、単語や文字ではなくトークンで計測されます。200,000 トークンのコンテキストウィンドウを持つモデルは、1 つのプロンプトに長編小説に加えて複数の研究論文に相当する量を保持できます。

なぜ重要か

トークンは、すべてのユーザーが気にする 3 つの要素を決定します。コスト容量、そして挙動です。API プロバイダーは 100 万トークン単位で課金するため、トークン化の効率が悪いプロンプトは不要にコストが高くなります。コンテキストウィンドウ(モデルが一度に考慮できる最大テキスト量)はトークンでカウントされるため、非常に長いドキュメントは入力前に分割する必要があります。挙動にも影響します。トークナイザーが単語を異なる形で分割すると、モデルがその単語を推論する方式が変わることがあります。また、英語よりもはるかに多くのサブワードに分割される言語もあり、そうした言語のユーザーにとってはコストが膨らみ、実質的なコンテキストが短くなります。

トークンの主要概念

  • トークン化:テキストをトークンに分割するアルゴリズム。通常は BPE、WordPiece、または Unigram を使用します。
  • 語彙:モデルが知るトークンの固定リストで、各エントリに一意の整数 ID が割り当てられています。
  • 特殊トークン:<BOS><EOS>、パディングマーカーなど、内容ではなく境界や構造を示すために予約されたシンボルです。
  • コンテキストウィンドウ:1 つのリクエストでモデルが処理できる最大トークン数。入力と生成された出力の両方を含みます。
  • トークン制限:プロバイダーが設けるリクエスト内のトークン数に対する上限。多くの場合、入力上限と出力上限に分けられます。

バイトペアエンコーディングをより深く理解するには、Andrej Karpathy による解説 minbpe が実践的な出発点となります。また、オリジナルの論文 Neural Machine Translation of Rare Words with Subword Units は、今日の多くのトークナイザーが基礎とする手法を提案しました。

Frequently Asked Questions

How many tokens are in a word?
It depends on the tokenizer, but English words are usually one or two tokens. A common short word like "the" is typically a single token, while longer or less common words are split into subword pieces — for example, "unbelievableness" might become four tokens. On average, English text runs about 0.75 tokens per word, or roughly 100 tokens per 75 words.
Why do AI models use tokens instead of words?
Words create problems for models: vocabularies balloon, rare words are unseen during training, and similar forms like "run," "running," and "ran" are treated as unrelated. Subword tokens give the model a fixed, manageable vocabulary while still letting it represent any word, including ones it has never seen, by combining familiar pieces.
Do tokens count toward the context window?
Yes. The context window is the total number of tokens the model can process in a single request, and it includes both the input you send and the output the model generates. If a model has a 100,000-token context window, your prompt and the model's reply together must fit within that budget.
Are tokens the same across different AI models?
No. Each model family uses its own tokenizer and vocabulary, so the same sentence can produce different token counts on different models. A prompt that fits comfortably in one model's context window may exceed another's, which is worth checking when switching between providers.