Documentation
¶
Overview ¶
Package finops provides optional model pricebooks and budget caps for run admission (F0) and usage_events USD estimates (F1 foundation).
Index ¶
- Constants
- func EncodeOverlayPayload(cfg *Config) (json.RawMessage, error)
- func UTCDayWindow(t time.Time) (since, until time.Time)
- func ValidateOverlay(cfg *Config) error
- type AlertsConfig
- type BudgetCap
- type BudgetVerdict
- type Budgets
- type Config
- func (c *Config) CancelInflightOnHardBreach() bool
- func (c *Config) Enabled() bool
- func (c *Config) HasPricebook() bool
- func (c *Config) LookupCaps(tenantID, agentID string) (tenantCap, agentCap *BudgetCap)
- func (c *Config) ReservationEnabled() bool
- func (c *Config) ReservationFor(tenantID, agentID string) (usd float64, tokens int64)
- func (c *Config) RoutingSoftPct() float64
- func (c *Config) SoftPct() float64
- type ModelPrice
- type Pricebook
- type ReservationAmount
- type ReservationConfig
- type RoutingConfig
- type UsageSnapshot
Constants ¶
const ( OnHardBreachDenyOnly = "" OnHardBreachCancelInflight = "cancel_inflight" )
OnHardBreach controls what happens when a hard day cap is already breached. Empty / "deny_only" keeps today's behavior (refuse new creates). "cancel_inflight" also drains pending/running runs in the breached scope via the same cancel path as kill-switch drain.
Variables ¶
This section is empty.
Functions ¶
func EncodeOverlayPayload ¶
func EncodeOverlayPayload(cfg *Config) (json.RawMessage, error)
EncodeOverlayPayload marshals cfg for durable storage (hold_ttl as string).
func ValidateOverlay ¶
ValidateOverlay checks an Admin-supplied FinOps patch. Returns a human-readable error suitable for HTTP 400.
Types ¶
type AlertsConfig ¶
type AlertsConfig struct {
SoftPct float64 // 0 → default 80
}
AlertsConfig controls soft_pct approach alerts. Soft/hard budget trips always emit budget_alert hooks; SoftPct adds approaches.
type BudgetCap ¶
type BudgetCap struct {
MaxUSDPerDay float64 `json:"max_usd_per_day,omitempty"`
MaxTokensPerDay int64 `json:"max_tokens_per_day,omitempty"`
MaxRunsPerDay int64 `json:"max_runs_per_day,omitempty"`
Soft bool `json:"soft,omitempty"`
}
BudgetCap is one tenant- or agent-scoped daily ceiling. Zero / unset fields are unlimited. Soft=true means admission still allows but emits a budget_soft audit; Soft=false (default) hard-denies.
type BudgetVerdict ¶
type BudgetVerdict struct {
Allow bool
Soft bool // true when Allow and at least one soft cap tripped
Hard bool // true when !Allow due to a hard cap
Reason string
Scope string // "tenant" or "agent"
CapKind string // "usd" | "tokens" | "runs"
}
BudgetVerdict is the result of checking one or more caps.
func ApproachCap ¶
func ApproachCap(cap *BudgetCap, snap UsageSnapshot, scope string, softPct float64) *BudgetVerdict
ApproachCap reports a soft-style verdict when snap is at/above softPct of a hard (non-Soft) cap but not yet over the cap. softPct in (0,100]; 0 means 80. Soft caps are ignored here (they already warn at 100%).
func EvaluateCap ¶
func EvaluateCap(cap *BudgetCap, snap UsageSnapshot, scope string) BudgetVerdict
EvaluateCap compares a snapshot against one cap. Empty cap → allow. Hard deny wins over soft allow when multiple dimensions trip.
type Budgets ¶
type Budgets struct {
Tenants map[string]BudgetCap `json:"tenants,omitempty"`
Agents map[string]BudgetCap `json:"agents,omitempty"`
}
Budgets holds optional tenant and agent caps from langgraph.json finops.budgets. Agent keys are "tenant_id/agent_id".
type Config ¶
type Config struct {
Pricebook Pricebook `json:"pricebook,omitempty"`
Budgets Budgets `json:"budgets,omitempty"`
Alerts AlertsConfig `json:"alerts,omitempty"`
Reservation ReservationConfig `json:"reservation,omitempty"`
Routing RoutingConfig `json:"routing,omitempty"`
OnHardBreach string `json:"on_hard_breach,omitempty"`
}
Config is the runtime finops section (pricebook + budgets + F3+).
func DecodeOverlayPayload ¶
func DecodeOverlayPayload(raw json.RawMessage) (*Config, error)
DecodeOverlayPayload unmarshals a partial Config from overlay JSON. Accepts reservation.hold_ttl as a Go duration string (e.g. "24h").
func Merge ¶
Merge applies overlay onto base. Overlay map keys replace/add; scalar sections replace when the overlay JSON set them to a non-zero / non-empty value (partial patch semantics matching Admin PUT bodies). Neither argument is mutated. Nil overlay → Clone(base).
func (*Config) CancelInflightOnHardBreach ¶
CancelInflightOnHardBreach is true when on_hard_breach is cancel_inflight.
func (*Config) HasPricebook ¶
HasPricebook reports whether any model prices are configured.
func (*Config) LookupCaps ¶
LookupCaps returns tenant and agent caps for a create (agent may be nil).
func (*Config) ReservationEnabled ¶
ReservationEnabled is true when any hold amount is configured (global or per-agent override).
func (*Config) ReservationFor ¶
ReservationFor returns the hold amounts for a create. Per-agent overrides (key "tenant_id/agent_id") replace the global defaults when present; otherwise the global USD/token holds are used.
func (*Config) RoutingSoftPct ¶
RoutingSoftPct returns routing threshold (routing > alerts > 80).
type ModelPrice ¶
type ModelPrice struct {
InputPer1k float64 `json:"input_per_1k" yaml:"input_per_1k"`
OutputPer1k float64 `json:"output_per_1k" yaml:"output_per_1k"`
}
ModelPrice is USD per 1k tokens for one model.
type Pricebook ¶
type Pricebook map[string]ModelPrice
Pricebook maps model id → per-1k USD rates. Empty book: prefer reported cost_usd; otherwise usd_estimate stays 0 from tokens alone.
func (Pricebook) EstimateUSD ¶
EstimateUSD returns a USD estimate for one usage observation. When costUSD > 0 it wins (runner-reported). Otherwise, if the model has a pricebook entry, tokens are priced; else 0.
type ReservationAmount ¶
ReservationAmount is one static hold size (global or per-agent).
type ReservationConfig ¶
type ReservationConfig struct {
USDPerRun float64
TokensPerRun int64
// HoldTTL, when > 0, expires open usage_holds older than this age
// so a crashed runner that never releases cannot pin day caps forever.
HoldTTL time.Duration
// Agents maps "tenant_id/agent_id" → override amounts. When present,
// that entry fully replaces the global USD/token holds for the agent
// (0 on a dimension means no hold on that dimension for that agent).
Agents map[string]ReservationAmount
}
ReservationConfig is optimistic per-create hold amounts.
type RoutingConfig ¶
type RoutingConfig struct {
Enabled bool
SoftPct float64 // 0 → alerts SoftPct / 80
Aliases map[string][]string
}
RoutingConfig rewrites aliases to cheaper agents near soft_pct.
type UsageSnapshot ¶
UsageSnapshot is current-day spend for budget evaluation.
func (UsageSnapshot) Tokens ¶
func (u UsageSnapshot) Tokens() int64
Tokens returns tokens_in + tokens_out.