compactionpoint

package
v0.7.4-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 4 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 compactionpoint answers ONE question: at what provider-billed input does a conversation's own compaction mechanism act?

It is a separate package because two very different layers need the same answer and neither can own it. The GATE needs it to decide when to fire (components, through apply); the PANEL needs it to size an episode's attribution span (dash). A copy in each would be two tables to keep in agreement, which is the failure this repo has already had with cache TTLs and with token rulers.

It is NOT in internal/modelinfo, despite being keyed by model, because it is not a property of the model. It is a property of a (deployment, client, model) TRIPLE: the same model behind a different agent, or behind Claude Code with a different auto-compact threshold, acts somewhere else. modelinfo owns what the provider publishes; this owns what a client does.

Index

Constants

View Source
const BobDefaultThreshold = 0.5

BobDefaultThreshold is the client's default, `fla = .5` in the client.

View Source
const BobFallbackWindow = 200_000

BobFallbackWindow is the window Bob uses when its catalog has no max_input_tokens for a model.

View Source
const ClientToBilled = 1.0

ClientToBilled converts the client's own token count to provider-billed tokens. 1.0 means the threshold is taken as billed tokens.

NOT MEASURED AS A CONVERSION. The two data points disagree:

  • 13 real Claude Code sessions (#940) auto-compacted at 164k to 167k billed, which is the client threshold itself (W - 33k for a 200k window): a factor of about 1.0.
  • One haiku session (the table above) billed 199,184 when the client's own count read about 168,000: a factor of 1.186.

1.0 is the lower, so it makes C smaller. A smaller C is the conservative direction for the replay cap and for the summarize trigger (see THE STATISTIC above). Change this one constant when a measurement settles it.

Variables

This section is empty.

Functions

func IsClientSource added in v0.6.8

func IsClientSource(src string) bool

IsClientSource is the ONE rule for "this source carries an absolute C that a row stores". The request-row writer and the dash walks use it.

Types

type Bob added in v0.6.8

type Bob struct {
	Read bool
	// Threshold is the raw model.compressionThreshold from the winning file, "" when not set.
	Threshold string
}

Bob is the auto-compact setting of Bob Shell (#963). A host fills it once, at start-up (proxy.ReadBobCompact). Read is true only when the host did the read (local mode, switch not off). The zero value changes nothing.

VERIFIED FROM THE SHIPPED CLIENT, Bob Shell 1.0.3 (bundle/bob.js, read only, not from memory):

  • KEY: `model.compressionThreshold` in settings.json (schema default 0.5, "the fraction of context usage at which to trigger context compression", restart required). Files: the user file ~/.bob/settings.json, the project file .bob/settings.json, and the system file (BOB_SHELL_CLI_SYSTEM_SETTINGS_PATH, else /Library/Application Support/Bob-Shell/settings.json on macOS, /etc/bob-shell/settings.json elsewhere). The merge order is system defaults, user, project (if the folder is trusted), system; the last one wins. There is no env override.
  • DECISION (class Eue, method compress): the client compacts before a turn when `getLastPromptTokenCount() >= T x ex(model)`, with T = `config.getCompressionThreshold() ?? 0.5`. getCompressionThreshold returns the user's value when it is truthy (so 0 means "not set"), else a server-side experiment flag (CONTEXT_COMPRESSION_THRESHOLD) when the flag is non-zero, else undefined, which gives the 0.5 above.
  • RULER: lastPromptTokenCount is set from `usageMetadata.promptTokenCount` of the last response, i.e. the PROVIDER-REPORTED prompt tokens. That is the same ruler as billed input (ClientToBilled = 1.0 is right here, not a guess). After a compaction the client seeds it with an estimate, JSON length / 4, until the next response. ex(model) is the model's context window from Bob's OWN gateway catalog, GET /inference/v1/model/info (or /v1/model/info), field data[].model_info.max_input_tokens, 200,000 when the field or the model is missing (getLiteLLMModels and the `contextWindow: a.max_input_tokens||2e5` line). So T is a fraction of the WHOLE window of THAT catalog, with no reserve.

WHICH WINDOW (review round 1). Bob's catalog window can differ from ours (ours is 1,000,000 for claude-opus-5; Bob's catalog may say 200,000). A C built on a window larger than Bob's is too high, the dangerous direction for this package. So Resolve takes min(our window, Bob's window): Bob's window is the max_input_tokens the proxy saw in the model/info response it passed through (BobWindow, a number for each model id); when the proxy has not seen it, Bob's own fallback 200,000 is used. A lower C is the conservative side.

NOT READ: the project file (the proxy does not know the directory), the old flat key `chatCompression` (the client migrates it), and the server-side experiment flag. If that flag is set for a user who changed nothing, the real default is not 0.5 and C is off. The PR says so.

func (Bob) Resolve added in v0.6.8

func (b Bob) Resolve(window, bobWindow int) (Point, bool)

Resolve gives C for Bob: the user's fraction of the window when set, else the client default. ok is false when nothing was read or the window is unknown: the caller then uses the table.

window is OUR window for the model (0 = unknown). bobWindow is Bob's own max_input_tokens for the model, seen in its model/info response (0 = not seen). The window used is min(window, bobWindow), or min(window, BobFallbackWindow) when bobWindow is not seen.

type ClaudeCode added in v0.6.8

type ClaudeCode struct {
	// Env values, raw: CLAUDE_CODE_AUTO_COMPACT_WINDOW, CLAUDE_AUTOCOMPACT_PCT_OVERRIDE,
	// DISABLE_AUTO_COMPACT, DISABLE_COMPACT.
	EnvWindow, EnvPct, EnvDisableAuto, EnvDisable string
	// Settings values, raw, already merged across the settings files by the host.
	SettingsWindow string
	// SettingsEnabled is "true", "false" or "" (not set).
	SettingsEnabled string
	// ModelWindows maps a model name from modelSettings to its autoCompactWindow, raw.
	ModelWindows map[string]string
}

ClaudeCode is what the user set to move Claude Code's own auto-compact point (#948). A host fills it once, at start-up, from its env and the user's settings files (proxy.ReadClaudeCompact). Every field is the RAW text of the setting, so the parsing rules below live in one place, next to the client rules they copy.

WHERE EACH SETTING IS FROM. Read from the shipped client, `claude` 2.1.319 (its bundled JavaScript, `strings` on the binary), not from memory:

  • CLAUDE_CODE_AUTO_COMPACT_WINDOW (env): the window the client counts against, in tokens. Accepts `200000`, `200k`, `1m`. Below 100,000 it is raised to 100,000. Above 1,000,000 it is lowered to 1,000,000. Not a number: ignored. The effective window is min(model window, this).
  • `autoCompactWindow` (settings.json, top level) and `modelSettings.<model>.autoCompactWindow` (what `/autocompact` writes): the same, in tokens, or "auto" for the client default. The env var wins over both. A model entry wins over the top-level one.
  • CLAUDE_AUTOCOMPACT_PCT_OVERRIDE (env): a percent, 0 < p <= 100, of the effective window less the output reserve. It can only LOWER the threshold: the client takes the smaller of it and the default.
  • DISABLE_AUTO_COMPACT and DISABLE_COMPACT (env; the values 1, true, yes, on), and `autoCompactEnabled: false` (settings.json or ~/.claude.json): auto-compact is off.

THE THRESHOLD. With W the effective window, the client compacts when its token count reaches

T = (W - 20,000) - 13,000                         (the output reserve, then the buffer)
T = min(floor((W - 20,000) x pct / 100), T)       (when the percent override is set)

20,000 is min(model max output, 20,000); every current model has a larger maximum. These are constants in the client (AZr and the L4 function). Nothing is guessed from max_tokens.

func (ClaudeCode) Resolve added in v0.6.8

func (c ClaudeCode) Resolve(model string, window int) (p Point, ok bool)

Resolve gives C for the model when the user changed a setting. ok is false when nothing was changed or nothing is readable: the caller then uses For(model), which is the old behavior. window is the model's context window (0 = unknown).

type Codex added in v0.6.8

type Codex struct {
	// Limit is the raw model_auto_compact_token_limit, "" when not set.
	Limit string
	// Scope and PostTurn are the raw values of the two settings above, "" when not set.
	Scope, PostTurn string
}

Codex is the user's own auto-compact setting of Codex CLI (#963). A host fills it once, at start-up (proxy.ReadCodexCompact). The zero value changes nothing.

VERIFIED FROM THE SHIPPED CLIENT'S SOURCE, github.com/openai/codex tag rust-v0.160.0 (read only; `codex --version` on the laptop says 0.160.0):

  • KEY: `model_auto_compact_token_limit` (integer tokens) at the top level of $CODEX_HOME/config.toml (default ~/.codex; codex-rs/utils/home-dir/src/lib.rs). No env override and no per-profile key exist in this version. Other layers exist (project config, managed config, `-c` on the command line); only the user file is read.
  • FORMULA (codex-rs/protocol/src/openai_models.rs ModelInfo::auto_compact_token_limit, and codex-rs/core/src/session/context_window.rs context_window_token_status_with_config): limit = min(configured limit or catalog limit, 0.9 x context_window); compact when active_context_tokens >= limit (+ a fallback buffer only when the token-budget feature is on).
  • RULER: active_context_tokens = the provider-reported total_tokens of the LAST response (input + output) plus estimates for items added after it. That is the next request's billed input within the size of the new items, so the factor to billed tokens is taken as 1.0.
  • THE LIMIT CLAMP uses Codex's own context_window (272,000 for the models in the bundled 0.160.0 catalog, models-manager/models.json, one at 372,000). That value is the CLIENT's catalog, not ours, so we clamp to 0.9 x OUR window only as a sanity bound.

WHAT IS NOT DONE, ON PURPOSE. The default (no key set) is 0.9 x the client's catalog window, which depends on a per-model catalog that changes with each release and that the user can replace (model_catalog_json). Our window for the same model differs (872,000 vs 272,000 for gpt-6-sol). No default is invented: the request stays on the table. Two settings change the meaning of the limit, and with either set no C is derived (fail open): `model_auto_compact_token_limit_scope = "body_after_prefix"` (the limit then counts only growth after a prefix) and `model_post_turn_compact_threshold_percent` > 0 (a second, lower trigger).

func (Codex) Resolve added in v0.6.8

func (c Codex) Resolve(window int) (Point, bool)

Resolve gives C for Codex when the user set a limit. window is OUR window for the model (0 = unknown).

type Point

type Point struct {
	// Frac is C as a fraction of the model's context window, in provider-billed tokens.
	Frac float64
	// Abs, when > 0, is C in absolute billed tokens and wins over Frac in Tokens. It is set by a Configured,
	// ClientDefault or Operator point: the client's or operator's own number does not scale with OUR window,
	// and a round trip through a fraction of the window could lose a token to the float. 0 = use Frac.
	Abs    int
	Source Source
	// Note is why this value, in enough detail to tell whether it still holds.
	Note string
}

Point is one resolved C.

func For

func For(model string) Point

For resolves a model's compaction point from the bootstrap table.

An unlisted model gets C == W. That is NOT conservative for the span -- too high makes the span too wide and OVER-reports -- and it is chosen anyway, because a fabricated lower bound would make every unlisted model's savings quietly smaller by an amount nobody chose, and an operator comparing two models would be reading a difference this table invented. C == W is transparently an absence of information rather than a guess pretending to be one, and the Source says so.

func Recorded added in v0.6.8

func Recorded(model string, abs int, src string) Point

Recorded rebuilds the Point a request row carries: the absolute C in billed tokens and the source it had when the row was written (#948, #963). A row with no recorded client value gets the table's answer for its model, so an old row reads as before. Only a Configured or ClientDefault row has an absolute C; the table value scales with the window and is better re-read from the table.

func Resolve added in v0.7.2

func Resolve(model string, tokens, window int) Point

Resolve is For with the operator's table in front. tokens is the operator's compaction_point for this model in billed input tokens (0 = none stated), window the model's context window (0 = unknown).

A stated value is used as it is, whatever the window: the window is the model's limit and this is where the CLIENT acts, and the two are not derived from each other. Frac is filled in when the window is known, for the callers (the dashboard span) that think in fractions; it is capped at 1.

func (Point) FromClient added in v0.6.8

func (p Point) FromClient() bool

FromClient reports whether the point came from the client itself (a user setting or the client's own default) and so carries an absolute C that a request row must store (#948, #963).

func (Point) Tokens

func (p Point) Tokens(window int) int

Tokens is C in absolute billed tokens for a given window. 0 when the window is unknown.

type Source

type Source string

Source says where a resolved C came from, so a published figure can be read for what it is. The same measured/guessed discipline modelinfo.Exact keeps for windows.

const (
	// Measured: case 1 or 2 -- observed in billed tokens on a real run, or set by us.
	Measured Source = "measured"
	// Assumed: reported behaviour or a reasoned bound, not observed.
	Assumed Source = "assumed"
	// Configured: the user changed the client's own auto-compact setting, and the local proxy read it at start-up
	// (see ClaudeCode, Codex, Bob). The number is the client's own threshold, taken as billed tokens by
	// ClientToBilled. It beats the table.
	Configured Source = "configured"
	// ClientDefault: the user changed nothing, and the client's own VERIFIED default threshold is
	// used (see Bob, #963). Like Configured it is an absolute number in billed tokens and it is
	// stored on the request row.
	ClientDefault Source = "client_default"
	// Operator: the operator stated it per model in the MODEL_PRICES table (compaction_point, billed
	// tokens). It beats the bootstrap table. It is a statement, not an observation: the operator owns it.
	Operator Source = "operator"
	// WindowFallback: case 3 and case 4 together -- C == W, because either nothing compacts here
	// (a fact) or we have never seen this client compact (a guess). This package cannot yet tell
	// those apart, which is itself worth reporting rather than smoothing over.
	WindowFallback Source = "window_fallback"
)

Jump to

Keyboard shortcuts

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