Documentation
¶
Overview ¶
Package accountant is the context accountant: it tracks what a context optimizer saves. It prices the changes that the optimizer makes to an LLM request, in tokens for each billing tier.
A host calls it in four steps for each request. [Trackers.BeginRequest] takes the body the client sent. [Request.Add] records each change an operator reported. [Request.EndRequest] takes the body that was forwarded and the cache facts. [Request.Compute] takes the billed Usage and returns a Result: the tokens each change saved or caused. The host turns tokens into money. A keep-alive ping has no request record: use ComputeStandalone.
The module imports only the standard library. Its design is in docs/design.md, and a complete program is in the package example.
This file is the whole public API. The implementation is in internal/engine, with the state it keeps for each thread; none of that is part of the API.
Example ¶
An operator shortens a tool output from 8,000 bytes to 1,000 on the second request. The first two messages are already cached, so they are read in both bills. The new message would have been written to the cache, so the saving is the tokens it did not write, at the 5-minute write tier.
package main
import (
"fmt"
"strings"
"time"
"github.com/rossoctl/accountant"
)
// body hashes one request body: one entry per message, in order.
func body(sizes ...int) accountant.Body {
msgs := make([][]byte, len(sizes))
for i, n := range sizes {
msgs[i] = []byte(fmt.Sprintf("m%d", i) + strings.Repeat(".", n))
}
return accountant.NewBody(nil, nil, msgs)
}
// An operator shortens a tool output from 8,000 bytes to 1,000 on the second request. The first
// two messages are already cached, so they are read in both bills. The new message would have been
// written to the cache, so the saving is the tokens it did not write, at the 5-minute write tier.
func main() {
ts := accountant.NewTrackers()
t0 := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)
facts := accountant.EntryFacts{TTL: 5 * time.Minute, Lifetime: accountant.LifetimeExact}
thread := accountant.Thread{ID: "main"}
last := accountant.Position{Section: accountant.SectionMessages, Index: 1}
// Request 1: nothing changed, the provider wrote the cache.
first := body(2000, 2000)
r1 := ts.BeginRequest(accountant.RequestMeta{RequestID: "r1", Session: "s1", At: t0}, first,
[]accountant.Breakpoint{{Pos: last, TTL: "5m"}})
r1.EndRequest(thread, first, []accountant.Breakpoint{{Pos: last, TTL: "5m"}}, facts)
r1.Compute(accountant.Usage{CacheWrite: 4000})
// Request 2: the client sent 8,000 bytes at message 2; an operator forwarded 1,000.
client, forwarded := body(2000, 2000, 8000), body(2000, 2000, 1000)
at := accountant.Position{Section: accountant.SectionMessages, Index: 2}
r2 := ts.BeginRequest(accountant.RequestMeta{RequestID: "r2", Session: "s1", At: t0.Add(10 * time.Second)},
client, []accountant.Breakpoint{{Pos: at, TTL: "5m"}})
r2.Add(accountant.Operation{ID: "op1", Kind: accountant.KindRewrite, Operator: "mask", ChangeID: "mask:1", At: at,
SizeBefore: accountant.Size{Bytes: 8000}, SizeAfter: accountant.Size{Bytes: 1000}})
r2.EndRequest(thread, forwarded, []accountant.Breakpoint{{Pos: at, TTL: "5m"}}, facts)
res := r2.Compute(accountant.Usage{CacheRead: 4000, CacheWrite: 1000})
p := res.Ops[0]
fmt.Printf("saved %+v\ncaused %+v\nchecks %d\n", p.Saved, p.Caused, len(res.Checks))
}
Output: saved {Input:0 CacheWrite5m:6992 CacheWrite1h:0 CacheRead:0} caused {Input:0 CacheWrite5m:0 CacheWrite1h:0 CacheRead:0} checks 0
Index ¶
- Constants
- type Billing
- type BillingMode
- type Body
- type Breakpoint
- type CacheEffect
- type CacheEffectCause
- type CacheEffectKind
- type CachePhase
- type CallUsage
- type CheckFailure
- type EntryFacts
- type Kind
- type LifetimeKind
- type MessageSplice
- type ModelCall
- type Operation
- type Position
- type PricedOp
- type Request
- type RequestMeta
- type RequestOps
- type Result
- type Section
- type SettingChange
- type Size
- type Span
- type Thread
- type TierTokens
- type Trackers
- type Unpriced
- type Usage
Examples ¶
Constants ¶
const ( KindRewrite = engine.KindRewrite KindSpend = engine.KindSpend KindSetting = engine.KindSetting SectionTools = engine.SectionTools SectionSystem = engine.SectionSystem SectionMessages = engine.SectionMessages PhaseHot = engine.PhaseHot PhaseCold = engine.PhaseCold PhaseUnknown = engine.PhaseUnknown LifetimeExact = engine.LifetimeExact LifetimeMinimum = engine.LifetimeMinimum LifetimeUnknown = engine.LifetimeUnknown BillingAPI = engine.BillingAPI BillingSubscription = engine.BillingSubscription BillingUnknown = engine.BillingUnknown EffectBreak = engine.EffectBreak EffectRescue = engine.EffectRescue EffectSplit = engine.EffectSplit EffectTTLPremium = engine.EffectTTLPremium EffectTTLRescue = engine.EffectTTLRescue CauseModelChanged = engine.CauseModelChanged )
const ( ReasonEndRequestMissing = engine.ReasonEndRequestMissing ReasonPingResultUnknown = engine.ReasonPingResultUnknown ReasonUnknownKind = engine.ReasonUnknownKind ReasonNegativeSize = engine.ReasonNegativeSize ReasonPenaltyUnknown = engine.ReasonPenaltyUnknown ReasonNoUsage = engine.ReasonNoUsage ReasonNotSpend = engine.ReasonNotSpend )
Machine-stable reasons in Result.Unpriced.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Result ¶
Result is what [Request.Compute] returns.
func ComputeStandalone ¶
func ComputeStandalone(ops RequestOps) Result
ComputeStandalone prices operations that have no request record, such as a keep-alive ping.
type Trackers ¶
Trackers holds the state of every thread. Make one with NewTrackers, and share it.
func NewTrackers ¶
func NewTrackers() *Trackers
NewTrackers returns an empty set: every thread starts with no history.
Directories
¶
| Path | Synopsis |
|---|---|
|
events
module
|
|
|
host
module
|
|
|
internal
|
|
|
engine
Package engine is the implementation of package accountant, which re-exports its public names.
|
Package engine is the implementation of package accountant, which re-exports its public names. |
|
metrics
module
|
|
|
test
|
|
|
cases
Package cases is the exhaustive list of test cases the implementation must meet (testing.md §10).
|
Package cases is the exhaustive list of test cases the implementation must meet (testing.md §10). |