tiktoken

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package tiktoken is the TokenCounter adapter: it counts tokens with a pure-Go, offline tiktoken BPE codec (o200k_base — the encoding text-embedding-3 and gpt-4o use). lore is provider-agnostic, so exact counts vary by model; this is treated as approximate-but-stable, which is enough to size chunks and bound retrieval budgets predictably. The dependency lives only here — the domain chunkers receive Count as an injected func, keeping stdlib-only domain clean.

Index

Constants

View Source
const EncodingName = "o200k_base"

EncodingName identifies the tiktoken encoding this adapter counts with. It is recorded in a Collection's ChunkerSpec so a change of tokenizer (which alters token counts and therefore chunk boundaries) is a chunker mismatch.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

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

Counter counts tokens with a tiktoken codec. Construct with New; the zero value is not usable.

func New

func New() (*Counter, error)

New returns a Counter over the o200k_base encoding. The codec's vocabulary is compiled into the binary, so this never touches the network.

func (*Counter) Count

func (c *Counter) Count(text string) int

Count returns the number of tokens in text. On the rare tokenize failure it falls back to a whitespace-word count, so sizing degrades to a sane estimate rather than reporting zero.

Jump to

Keyboard shortcuts

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