Documentation
¶
Index ¶
- func UpdateFile(ctx context.Context, path string, fn func(*Config) error) error
- func WithFileLock(ctx context.Context, path string, fn func() error) (err error)
- type Agent
- type Config
- func (c *Config) Convert(amount, from, to string) (string, error)
- func (c *Config) Delete(kind, id string) error
- func (c *Config) ImportMediaPrices(prices map[string]MediaPrice, overwrite bool) (added, updated, skipped int, err error)
- func (c *Config) ImportModels(prices map[string]Price, overwrite bool) (added, updated, skipped int, err error)
- func (c *Config) QuoteMedia(id, quantity, currency string) (string, error)
- func (c *Config) QuoteText(model string, input, output int64, currency string) (string, error)
- func (c *Config) Save(path string) error
- func (c *Config) SetAgent(id string, a Agent) error
- func (c *Config) SetMediaPrice(id string, p MediaPrice) error
- func (c *Config) SetMediaUsage(id string, u MediaUsage) error
- func (c *Config) SetModel(id string, p Price) error
- func (c *Config) SetRate(code, rate string) error
- func (c *Config) SetUsage(id string, u Usage) error
- func (c *Config) Total(agent, currency string) (Report, error)
- func (c *Config) Validate() error
- type MediaPrice
- type MediaUsage
- type Price
- type Report
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UpdateFile ¶ added in v0.2.0
UpdateFile performs a serialized read-modify-save transaction among cooperating writers. If fn fails or validation fails, the existing ledger is not replaced.
func WithFileLock ¶ added in v0.2.0
WithFileLock serializes cooperating writers of the same ledger on a local filesystem. The .lock file is permanent and must not be deleted while any writer may be running. The context controls lock acquisition, not the duration of fn. Do not call recursively. The callback must use the same path and finish all writes before returning.
Types ¶
type Config ¶
type Config struct {
BaseCurrency string `json:"base_currency"`
Rates map[string]string `json:"rates"`
Agents map[string]Agent `json:"agents"`
Models map[string]Price `json:"models"`
Usages map[string]Usage `json:"usages"`
MediaPrices map[string]MediaPrice `json:"media_prices,omitempty"`
MediaUsages map[string]MediaUsage `json:"media_usages,omitempty"`
}
Config is the entire portable ledger. Rates are units of each currency per one base unit.
func LoadFile ¶ added in v0.2.0
LoadFile reads while holding the same lock used by UpdateFile and the Store watcher. Prefer this to Load when other processes may replace the file, especially on Windows.
func (*Config) ImportMediaPrices ¶ added in v0.2.0
func (c *Config) ImportMediaPrices(prices map[string]MediaPrice, overwrite bool) (added, updated, skipped int, err error)
ImportMediaPrices merges a batch without changing existing prices unless overwrite is true.
func (*Config) ImportModels ¶
func (c *Config) ImportModels(prices map[string]Price, overwrite bool) (added, updated, skipped int, err error)
ImportModels merges a batch atomically in memory. Existing prices are retained unless overwrite is true.
func (*Config) QuoteMedia ¶ added in v0.2.0
QuoteMedia returns an estimate using the current price per configured unit.
func (*Config) QuoteText ¶ added in v0.2.0
QuoteText returns an estimate using the model's current price and configured exchange rates.
func (*Config) Save ¶
Save replaces the file only after validation and a complete write in the same directory. Callers must serialize read-modify-write transactions across processes.
func (*Config) SetMediaPrice ¶ added in v0.2.0
func (c *Config) SetMediaPrice(id string, p MediaPrice) error
SetMediaPrice creates or replaces a unit price. It does not change recorded snapshots.
func (*Config) SetMediaUsage ¶ added in v0.2.0
func (c *Config) SetMediaUsage(id string, u MediaUsage) error
SetMediaUsage records a quantity and snapshots the current price identified by PriceID.
func (*Config) SetUsage ¶
SetUsage creates or replaces a record and snapshots the model's current price.
type MediaPrice ¶ added in v0.2.0
type MediaPrice struct {
Modality string `json:"modality"` // text, audio, or video
Model string `json:"model"` // provider/model and optional variant
Currency string `json:"currency"`
Unit string `json:"unit"` // e.g. second, character, token, request
Amount string `json:"amount"`
Per string `json:"per"`
}
MediaPrice defines a price per Per units for a text, audio, or video model. Multiple components of one request should use separate IDs and usage records.
type MediaUsage ¶ added in v0.2.0
type MediaUsage struct {
Agent string `json:"agent"`
PriceID string `json:"price_id"`
Quantity string `json:"quantity"`
Time string `json:"time"`
Price MediaPrice `json:"price"`
}
MediaUsage snapshots its price on creation or replacement.
type Price ¶
type Price struct {
Currency string `json:"currency"`
Input string `json:"input_per_million"`
Output string `json:"output_per_million"`
}
Price specifies the charge per one million tokens.
type Usage ¶
type Usage struct {
Agent string `json:"agent"`
Model string `json:"model"`
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
Time string `json:"time"`
Price Price `json:"price"`
}
Usage stores a price snapshot, so later model price edits never change past charges.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package catalog imports base text token prices from models.dev.
|
Package catalog imports base text token prices from models.dev. |
|
cmd
|
|
|
aiprice
command
|
|
|
cataloggen
command
|
|
|
mediacataloggen
command
|
|
|
releasebuild
command
releasebuild builds distributable CLI archives with correct Unix executable modes.
|
releasebuild builds distributable CLI archives with correct Unix executable modes. |
|
Package store provides opt-in initialization and hot reload of a file-backed ledger.
|
Package store provides opt-in initialization and hot reload of a file-backed ledger. |