contextwindow

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package contextwindow estimates token usage for a message history and looks up the model's context-window capacity. Both numbers are approximate — exact tokenization depends on the model's tokenizer and varies enough between providers that "good enough for a watermark" is the operative bar.

Two callers consume this:

  • The TUI status bar, which colors the token counter when usage crosses a configurable warn_threshold.
  • The auto-summarization trigger, which fires before the next turn when usage crosses auto_threshold.

Both treat the numbers as ratios, not absolute counts, so a 10–15% estimation error is harmless.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EstimateTokens

func EstimateTokens(messages []adapter.Message) int

EstimateTokens returns an approximate token count for the given messages. Uses the rough ~4-chars-per-token heuristic that holds for English text across most BPE-family tokenizers (GPT, Claude, Llama). Tool-call argument JSON and tool-result content are counted; role metadata is not (it's bounded and small).

func WindowFor

func WindowFor(model string, defaultWindow int) int

WindowFor returns the context-window capacity (in tokens) for the given model tag, falling back to defaultWindow when the model isn't in the lookup table. Matching is by lowercase prefix — generous on purpose so unknown variants of known families still resolve.

The table is intentionally short. We update it as model families ship; users with an exotic or self-hosted model can always pin a fallback via context.default_window in config.toml.

Types

This section is empty.

Jump to

Keyboard shortcuts

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