Tokenize

Convert text into token IDs using .Tokenize().

Basic Usage

int[] tokens = await "Hello, world!"
    .Tokenize()
    .ExecuteAsync();

Debug.Log($"Token IDs: {string.Join(", ", tokens)}");
Debug.Log($"Token count: {tokens.Length}");

Input Types

String Input

int[] tokens = await "Sample text"
    .Tokenize()
    .ExecuteAsync();

Prompt Input

var prompt = new Prompt("Analyze this text");
int[] tokens = await prompt
    .Tokenize()
    .ExecuteAsync();

What Are Tokens?

Tokens are the basic units that AI models process. Understanding tokens helps you:

  • Estimate costs - Most APIs charge per token

  • Manage limits - Models have maximum token counts

  • Optimize prompts - Shorter prompts = lower cost

Token Examples

Unity Integration Examples

Example 1: Token Counter Display

Example 2: Token Inspector

Example 3: Analyze Prompt Efficiency

Tokenization vs Token Counting

Method
Returns
Use Case

.Tokenize()

Token IDs (int[])

Detailed analysis

.CountTokens()

Token count (int)

Quick count only

Use .CountTokens() instead if you only need the count:

Provider Support

OpenAI

Anthropic

Best Practices

✅ Good Practices

❌ Bad Practices

Understanding Token Patterns

Common Patterns

Next Steps

Last updated