Skip to main content

Module counterfactual

Module counterfactual 

Source
Expand description

Counterfactual savings metering (#701) — provider-authoritative receipts.

Local tokenizer counts (o200k_base) are an estimate of what a request would have cost without lean-ctx. Anthropic’s count_tokens endpoint is free and takes the identical body shape, so for each rewritten /v1/messages request the proxy can fire a probe with the original, uncompressed body concurrently with the real forward and read back the provider-counted answer: “this exact request would have billed N input tokens”. Paired with the billed usage block of the same response, that is a confound-free, provider-authoritative saving per request — the methodology pxpipe’s FAQ documents, adopted per #701.

Isolation guarantees:

  • The probe never mutates, delays or fails the forwarded request: it is spawned as a detached task and its result lands in a lock-free slot the usage recorder reads at response end (streams end seconds later, so the probe has long finished; a slow probe merely degrades that row to the local estimate).
  • Opt-in (proxy.counterfactual_metering, default off) and fired only for requests the proxy actually rewrote — an untouched body’s billed input is its counterfactual, no probe needed.

Structs§

CounterfactualSlot
Lock-free result slot shared between the detached probe task and the usage recorder. 0 means “no provider count” (pending or failed) — a real count_tokens answer is never 0 (model + messages always tokenize to something), and CounterfactualSlot::set clamps to ≥ 1 regardless.