Utilities

AI Dev Kit provides utility functions for tokenization, token counting, and billing.

Available Utilities

Tokenization

Convert text to token IDs:

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

Debug.Log($"Token count: {tokens.Length}");

Token Counting

Count tokens without getting IDs:

int count = await "Long text content..."
    .CountTokens()
    .ExecuteAsync();

Debug.Log($"Tokens: {count}");

Credits / Billing

Check account credits:

var credits = await Api.OpenAI
    .GetCredits()
    .ExecuteAsync();

Debug.Log($"Remaining: ${credits.Balance}");

Common Use Cases

Check Cost Before Request

public async UniTask<bool> CheckCostAndProceed(string prompt)
{
    int tokenCount = await prompt.CountTokens().ExecuteAsync();
    float estimatedCost = CalculateCost(tokenCount);
    
    if (estimatedCost > maxCost)
    {
        Debug.LogWarning($"Cost too high: ${estimatedCost}");
        return false;
    }
    
    return true;
}

Token Budget Management

public async UniTask<string> TrimToTokenBudget(string text, int maxTokens)
{
    int tokens = await text.CountTokens().ExecuteAsync();
    
    if (tokens <= maxTokens)
        return text;
    
    // Trim text to fit budget
    return TrimText(text, maxTokens);
}

Billing Monitor

public class BillingMonitor : MonoBehaviour
{
    async void Start()
    {
        var credits = await Api.OpenAI.GetCredits().ExecuteAsync();
        
        if (credits.Balance < warningThreshold)
        {
            ShowLowBalanceWarning();
        }
    }
}

Next Steps

Last updated