contexttax

package
v0.167.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 2 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 contexttax prices what a session pays to re-send its own context.

Every tool call re-reads the whole conversation as a cache read before it does anything. At 382k tokens that is about $0.19 a call on Opus 5, and a loop of twelve pays it twelve times. That charge is the context tax, and until now Caprock stored every number needed to compute it and showed none of them.

The formulas are spec/spec-context-tax.md section 5. Prices come from the pricing table rather than from multipliers on the input price: Fable 5.1 and Mythos 5.1 read cache at 0.025x, not the 0.1x the rest of the table uses.

Index

Constants

View Source
const (
	// SubStartTokens is a subagent's starting context, median over 364
	// archived subagent transcripts.
	SubStartTokens = 17_900
	// SummaryTokens is what a subagent hands back to its parent, median over
	// the same archive. It is cache-written once and re-read by every turn
	// left in the session, so it is a material cost, not a rounding error.
	SummaryTokens = 10_900
	// BriefTokens is the task description the lead writes to start the
	// subagent.
	BriefTokens = 1_000
)

The isolation counterfactual (spec section 5.4). It is displayed and never acted on: Stage 0 nudged the model 8 times on eligible series and it complied once, with delivery confirmed, so the lever is parked. What survives is the number, because it is what tells a user their loop was expensive. The intervention that follows is compaction.

The constants are measured, not guessed. Stage 0 read 364 real subagent transcripts: a subagent's first turn is a 17.9k median context, and the summary it hands back is 10.9k median -- not the 1k the spec first assumed, which is why the summary is charged here rather than rounded away.

Variables

This section is empty.

Functions

func CostOf

func CostOf(calls []Call, p Prices) float64

CostOf sums the full charge over a run of calls.

func NextCall

func NextCall(contextTokens int64, p Prices) float64

NextCall is what the next tool call costs at the current context, before it does any work. It is the meter's sharpest number: on a 968k-token session it reads $0.48 for a call that has not run yet.

func PriceSeries added in v0.70.0

func PriceSeries(calls []TimedCall, t RateTable) ([]Call, Prices)

PriceSeries keeps the calls of a series that can be priced and returns the rates to price them at. It is how a loop is priced, by the live alert and by the Week card alike, so the two can never disagree about one loop.

A call with no context or no model is dropped, never priced as free. The series is priced at the rates of the model that ran it; a series that changed model mid-way is rare and the last row wins, because refusing to price it at all helps nobody.

func TaxOf

func TaxOf(calls []Call, p Prices) float64

TaxOf sums the context tax over a run of calls.

Types

type Call

type Call struct {
	Context   int64
	Result    int64
	TurnsLeft int
}

Call is one priced tool call: the context it re-read, what it returned, and how many assistant turns still followed it in the session.

func (Call) Cost

func (c Call) Cost(p Prices) float64

Cost is the full section 5.2 charge for a call: the context it re-read, the result it wrote to cache, and that result being re-read by every turn left in the session.

func (Call) Tax

func (c Call) Tax(p Prices) float64

Tax is the first term of section 5.2 — the context this call re-read, and nothing else. It is the number the meter shows, kept apart from the cost of the result so a reader can see which half is which.

type Isolation

type Isolation struct {
	Actual   float64 `json:"actual"`
	Isolated float64 `json:"isolated"`
	Saved    float64 `json:"saved"`
}

Isolation is what a series cost, and what it would have cost inside a subagent that starts small.

The saving is not a cheaper model. Same-model isolation captured 96% of it in Stage 0 ($466 of $487), so the subagent is charged at the parent's own rates by default. The saving is that a subagent never re-reads the parent's 380k context: it starts near zero and grows only by its own results.

func Isolate

func Isolate(calls []Call, p Prices, turnsLeftEnd int) Isolation

Isolate prices a run of calls as it ran and as it would have run in a subagent. turnsLeftEnd is the number of assistant turns still to come after the series ends, over which the returned summary keeps being re-read.

type Lifetime

type Lifetime struct {
	TaxUSD  float64 `json:"tax_usd"`
	CostUSD float64 `json:"cost_usd"`
	Share   float64 `json:"share"`
	// Unpriced is how many cache-read tokens belong to models with no pricing
	// row, so the tax above excludes them. A tax figure that silently dropped
	// them would understate itself with no way for a reader to tell.
	UnpricedTokens int64 `json:"unpriced_tokens,omitempty"`
}

Lifetime is the context tax over a range, and what share of the spend it is.

Share is against the spend actually priced, so a workload with unpriced models reports a share of what it could price rather than a share diluted by what it could not. Unpriced volume is already reported separately.

func Sum

func Sum(models []ModelTax, t Table) Lifetime

Sum prices each model's re-read volume at that model's own cache-read rate.

Per model, not blended: Fable 5.1 and Mythos 5.1 read cache at 0.025x while the rest of the table reads at 0.1x, so a single rate would misprice any mixed workload by up to four times.

type ModelTax

type ModelTax struct {
	Model     string
	CacheRead int64
	CostUSD   float64
}

ModelTax is one model's re-read volume, the shape Lifetime sums over.

type Prices

type Prices struct {
	CacheRead  float64
	CacheWrite float64
}

Prices are the per-token rates a series is charged at, already divided out of the table's per-million figures.

func PricesOf

func PricesOf(row cost.Model) Prices

PricesOf reads the rates for a model out of a pricing row.

CacheWrite5m is the right write rate: a series writes results it re-reads within the same loop, which is minutes, not the hour the 1h rate is for.

type RateTable added in v0.70.0

type RateTable interface {
	LookupAt(model string, at time.Time) (cost.Model, bool)
}

RateTable is the subset of the pricing table that prices an instant.

type Table

type Table interface {
	Lookup(model string) (cost.Model, bool)
}

Table is the subset of the pricing table this package needs, so callers can pass the real one without this package importing the daemon.

Lookup, not LookupAt: an aggregate over a range has no single instant to price at. Where a model's rate changed inside the range -- Sonnet 5 went from $0.20 to $0.30 per million cache-read tokens on 2026-08-30 -- the lifetime tax is charged at today's rate for that model's whole volume. The error is bounded by how much a rate moved and applies only to the models that moved; per-call figures, where an instant does exist, use LookupAt.

type TimedCall added in v0.70.0

type TimedCall struct {
	Call
	Model string
	At    time.Time
}

TimedCall is a call with what its rates are looked up by: the model that issued it and when.

Jump to

Keyboard shortcuts

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