Documentation
¶
Overview ¶
Package catalog holds the descriptors that a model id stands for and the price table that says what a call of it costs.
Prices come from three layers, most specific first: a tenant's override, the deployment's override, and the snapshot shipped with the binary. The first layer with an entry for a model supplies that model's whole price; layers never merge or fall through (REQ-MODEL-071, REQ-MODEL-073). A call's tenant is read from its context with core.TenantFrom, never from the request (REQ-MODEL-072).
A price entry is either ordered rate rules or a CEL formula. Rate rules are chosen by their when condition, which sees the call's start time, parameters, usage and tenant. A model with rules and no formula of its own uses the default token formula; a model with its own formula uses it with the chosen rule's rates. Amounts are whole milli-points, rounded half away from zero. A model's own formula must return a whole number of milli-points in integer arithmetic; rates and priced amounts are never negative.
Index ¶
- type Catalog
- func (c *Catalog) AddDescriptor(d model.Descriptor) error
- func (c *Catalog) Estimate(ctx context.Context, req Request) (int64, error)
- func (c *Catalog) Lookup(_ context.Context, modelID string) (model.Descriptor, error)
- func (c *Catalog) SetDeploymentPrice(modelID string, p Price) error
- func (c *Catalog) SetSnapshotPrice(modelID string, p Price) error
- func (c *Catalog) SetTenantPrice(tenant core.TenantID, modelID string, p Price) error
- func (c *Catalog) Settle(q Quote, usage model.Usage) (Charge, error)
- func (c *Catalog) Start(ctx context.Context, req Request) (Quote, error)
- type Charge
- type Price
- type Quote
- type Rates
- type Request
- type Rule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Catalog ¶
type Catalog struct {
// contains filtered or unexported fields
}
Catalog stores descriptors by model id and the three layers of the price table. A Catalog is safe for concurrent use.
func New ¶
New returns a catalog with the shipped price snapshot embedded in the binary. The clock is read when a call starts and recorded with its quote.
func NewWithSnapshot ¶
NewWithSnapshot returns a catalog whose snapshot layer is yamlText instead of the bytes embedded in the binary. A deployment build may pass its own generated snapshot at startup.
func (*Catalog) AddDescriptor ¶
func (c *Catalog) AddDescriptor(d model.Descriptor) error
AddDescriptor stores d by its model id, replacing an earlier descriptor for the same id.
func (*Catalog) Estimate ¶
Estimate returns the reserved cost without keeping the quote. Callers that settle a call use Start instead.
func (*Catalog) Lookup ¶
Lookup returns the descriptor held for modelID. An id the catalog does not hold fails with core.CodeNotFound (REQ-MODEL-067, REQ-MODEL-068).
func (*Catalog) SetDeploymentPrice ¶
SetDeploymentPrice sets modelID's price in the deployment layer. An empty Price removes the entry.
func (*Catalog) SetSnapshotPrice ¶
SetSnapshotPrice sets modelID's price in the snapshot shipped with the binary. An empty Price removes the entry.
func (*Catalog) SetTenantPrice ¶
SetTenantPrice sets modelID's price in tenant's layer. An empty Price removes the entry. A tenant id is required: a tenantless call never reads a tenant layer (REQ-MODEL-072).
func (*Catalog) Settle ¶
Settle prices a started call's actual usage with the price entry and start time its quote captured (REQ-MODEL-078). Usage that reports more cached tokens than input tokens fails with core.CodeInvalidArgument before any formula runs (REQ-MODEL-082).
func (*Catalog) Start ¶
Start prices a call before it starts, records the start time (the request's At, or the catalog's clock when it is zero) and returns the pinned quote. Its estimate is the model's formula on the counted input tokens and the output limit (REQ-MODEL-077). A model with no price in any layer, or whose chosen layer has no rule that holds, fails with core.CodeUnsupported (REQ-MODEL-073).
type Charge ¶
type Charge struct {
// MilliPoints is the cost in whole milli-points.
MilliPoints int64
// Rule is the when condition of the rate rule that held at settlement;
// empty means an unconditional rule or a formula with no rules.
Rule string
}
Charge is what a settled call costs.
type Price ¶
type Price struct {
Formula string `json:"formula,omitempty" yaml:"formula,omitempty"`
Rules []Rule `json:"rules,omitempty" yaml:"rules,omitempty"`
}
Price is one model's price in one layer. Rules are tried in order and the first whose When holds applies. A blank When (including whitespace only) means the rule always holds. A Formula, when set after trimming surrounding whitespace, is CEL over params, usage and rates and sets the amount for the rule that held; it does not replace rule selection.
type Quote ¶
type Quote struct {
// Model is the id the quote was taken for.
Model string
// Estimate is the reserved cost in whole milli-points.
Estimate int64
// Rule is the when condition of the chosen rate rule; empty means an
// unconditional rule or a formula with no rules.
Rule string
// contains filtered or unexported fields
}
Quote pins the price entry, the model's formula, the start time, the tenant and the parameters that applied when the call started. Retain it and pass it to Settle; later price changes do not reprice the call (REQ-MODEL-078). A quote is serializable: a durable step records it and every replay settles by the pin it held, even when a price layer changed in between.
func (Quote) MarshalJSON ¶
MarshalJSON records the quote's pin so a durable step can store it and a replay can settle by the entry that applied when the call started.
func (*Quote) UnmarshalJSON ¶
UnmarshalJSON restores a quote recorded by MarshalJSON. Settle still rejects a record with no valid pinned entry, so only a quote produced by Start (or restored from one) can settle a call.
type Rates ¶
Rates are milli-points per million units. The token formula reads the "input" and "output" keys, and "cached" when a rule defines it; a model's own formula may read any key.
type Request ¶
type Request struct {
Model string
Params json.RawMessage
InputTokens int64
OutputLimit int64
// At is the run clock's time to price at. It is zero when the caller uses
// the catalog's own clock; a run passes dc.Now() so a replay prices a
// time-based rule at the same instant (REQ-MODEL-078).
At time.Time
}
Request is what the price table needs to price a call before it starts: the model, its parameters, its counted input tokens and its output limit.