token

package
v0.1.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package token provides token counting for MCP tool call content.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountJSON

func CountJSON(c Counter, v any) int

CountJSON estimates the token count for a value by marshaling it to JSON first. This is useful for counting tool call arguments (map[string]any).

Types

type Counter

type Counter interface {
	// Count returns the estimated number of tokens in the given text.
	Count(text string) int
}

Counter estimates token counts for text content. Implementations may vary in accuracy — the interface allows swapping a heuristic counter for a tiktoken-based one without changing consumers.

type HeuristicCounter

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

HeuristicCounter estimates tokens using a simple bytes-per-token ratio. This is fast and zero-dependency but approximate. Suitable for visibility purposes where exact counts are not required.

func NewHeuristicCounter

func NewHeuristicCounter(bytesPerToken int) *HeuristicCounter

NewHeuristicCounter creates a counter that estimates tokens at the given bytes-per-token ratio. A ratio of 4 is a reasonable default for English text.

func (*HeuristicCounter) Count

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

Count returns the estimated token count for the given text.

Jump to

Keyboard shortcuts

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