tokclient

package
v0.209.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tokclient is the harness's REAL-tokenizer path: it asks the serving endpoint's own /tokenize route how a text actually tokenizes, instead of estimating with chars/4. The house has been burned by estimates before — three real transcripts were rejected with exceed_context_size while the chars/4 ladder declined to compact (ADR 0017), and a truncation misdiagnosis cost a full debugging session (truncation-needs-context-check, 2026-08-08). Estimates stay acceptable for BUDGET headroom; any decision that CUTS content at an exact boundary must use this package.

Endpoint shape mirrors internal/agent's /props probe (window.go): llama.cpp serves POST /tokenize at the server root, and llama-swap proxies it per model under /upstream/{model}/tokenize (which may cold-start the model — acceptable for the same reason as the window probe: the caller is about to use exactly that model — and never onto a card a GPU lease fences: the passthrough is built by modelaffinity.AwaitUpstream, which refuses it while a render or an exclusive hold owns the card and the seat is not resident). A trailing /v1 on base is stripped via the one endpoint-normalization rule in internal/swapclient.

FAIL-OPEN CONTRACT: every method returns ok=false on any failure (transport, non-200, malformed payload, a piece accounting that does not reconstruct the input). Callers fall back to their estimate-driven path and MUST NOT fail the run because tokenization was unanswerable — a generic OpenAI endpoint has no /tokenize and that is fine.

Index

Constants

View Source
const DefaultTimeout = 60 * time.Second

DefaultTimeout bounds one /tokenize round-trip. Warm, tokenizing tens of KB is milliseconds; the generous bound exists because /upstream/{model}/ may cold-start the model first (same rationale as the window probe's 60s).

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client tokenizes text with one served model's own tokenizer.

func New

func New(base, model string, timeout time.Duration) *Client

New builds a client for the model served at base (any /v1 suffix is stripped). timeout <= 0 uses DefaultTimeout.

func NewUpstreamOnly

func NewUpstreamOnly(base, model string, timeout time.Duration) *Client

NewUpstreamOnly builds a client that tries ONLY the llama-swap per-model passthrough — never the bare-server root fallback. The root route answers for WHATEVER model is currently loaded, so in a multi-model process (the cascade) a stale per-model alias falling through to the root would count and cut with the PREVIOUS tier's tokenizer — silently, cached under the callee's key (TO-3 round-1 review finding 2026-08-14: the real cross-model contamination channel). Single-model agent paths keep New's fallback.

func (*Client) Count

func (c *Client) Count(ctx context.Context, text string) (int, bool)

Count returns how many tokens text costs under the served model's tokenizer (content tokens only — add_special false). The light call for budget arithmetic: no pieces on the wire.

func (*Client) LastErr

func (c *Client) LastErr() string

LastErr reports why the most recent failing call failed ("" = none yet). Consulted by the agent loop's sticky wrapper at downgrade time so the degradation is reportable, not just observable-by-absence.

func (*Client) LastFailDefinitive

func (c *Client) LastFailDefinitive() bool

LastFailDefinitive reports whether the most recent failure was a definitive route absence (all candidates 404/405) rather than a transient failure.

func (*Client) LastFailFenced

func (c *Client) LastFailFenced() bool

LastFailFenced reports whether the most recent failure was the GPU-lease fence refusing the per-model passthrough rather than the endpoint failing. The agent loop's sticky wrapper never counts such a failure toward a downgrade: the card frees when the render ends and the route is healthy.

func (*Client) Pieces

func (c *Client) Pieces(ctx context.Context, text string) ([]int, bool)

Pieces tokenizes text and returns the BYTE LENGTH of every token's piece, in token order. sum(lengths) == len(text) is verified here — if the pieces do not reconstruct the input exactly, the byte→token mapping a caller would build from them is unreliable, so the whole call fails open instead of handing back a mapping that could cut mid-message.

Jump to

Keyboard shortcuts

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