tokenizer

package
v1.29.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountTokensGlobal

func CountTokensGlobal(text string) int

CountTokensGlobal provides a convenient global function for token counting. It uses a singleton TokenCounter instance.

Types

type TokenCounter

type TokenCounter struct {
	// contains filtered or unexported fields
}

TokenCounter provides token counting functionality using tiktoken with cl100k_base encoding. This encoding is used for: - GPT-4, GPT-3.5-turbo models - text-embedding-ada-002 - Webhook/BYOW cost calculation (standardized token counting) - Groq models (compatible encoding)

func NewTokenCounter

func NewTokenCounter() (*TokenCounter, error)

NewTokenCounter creates a new token counter using cl100k_base encoding. This is the standard encoding for modern OpenAI models and our webhook pricing. It's also compatible with Groq models for consistent token counting across providers.

func (*TokenCounter) CalculateBYOWCost

func (tc *TokenCounter) CalculateBYOWCost(inputTokens, outputTokens int64) float64

CalculateBYOWCost calculates the cost for Bring Your Own Workflow based on token counts. Pricing: - Input tokens: $0.03 per 1M tokens - Output tokens: $0.12 per 1M tokens

func (*TokenCounter) CountTokens

func (tc *TokenCounter) CountTokens(text string) int

CountTokens counts the number of tokens in the given text using cl100k_base encoding. This is used for: - BYOW (Bring Your Own Workflow) pricing: $0.03 per 1M input tokens, $0.12 per 1M output tokens - Accounting/billing purposes - Pre-request estimation

func (*TokenCounter) CountTokensMultiple

func (tc *TokenCounter) CountTokensMultiple(texts []string) int

CountTokensMultiple counts tokens for multiple text strings and returns the total. Useful for counting tokens across multiple messages in a conversation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL