cost

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT 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 cost keeps the prompt-cache ledger per thread and model and prices a switch in uncached tokens, with USD only when a model has list prices. Codex's cache is per model and effort-neutral (research spikes 3 and 5).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func USD

USD prices a response at p's per-million list prices: uncached input, cached input and output (which includes reasoning tokens). An unpriced model costs 0.

Types

type Cache

type Cache struct {
	TTL        time.Duration
	WarmMargin time.Duration
}

Cache is the warmth rule: a model is warm from its last response until TTL minus WarmMargin has passed.

type CacheState

type CacheState struct {
	PrefixTokens int
	LastAt       time.Time
}

CacheState is what the ledger remembers per thread and model.

type Ledger

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

Ledger remembers, per thread, each model's cached prefix and the model the thread last ran on. Keys ignore effort: Codex's cache is effort-neutral. A detour to another model does not evict the previous model's entry. Safe for concurrent use.

func NewLedger

func NewLedger(c Cache) *Ledger

NewLedger returns an empty ledger using the warmth rule c.

func (*Ledger) Forget

func (l *Ledger) Forget(thread string)

Forget drops a thread's entries, for a caller evicting the thread.

func (*Ledger) Observe

func (l *Ledger) Observe(thread, model string, u responses.Usage, at time.Time)

Observe records a completed response on model: its whole input is now the model's cached prefix, and model becomes the thread's current model.

func (*Ledger) SwitchTax

func (l *Ledger) SwitchTax(thread, candidate string, tokens int, now time.Time) int

SwitchTax is how many more uncached input tokens the thread's next request, of tokens context tokens, writes on candidate than on the thread's current model. A warm model pays only what its cached prefix lacks; a cold one pays all tokens. It is 0 for the current model, for a candidate warm on at least the current model's prefix, and when the current model is cold or unknown, since staying then protects nothing.

The tax is relative to staying, not the candidate's absolute uncached tokens: the part both models must write is paid either way and must not raise the switch bar.

Jump to

Keyboard shortcuts

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