finops

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 5 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 finops provides optional model pricebooks and budget caps for run admission (F0) and usage_events USD estimates (F1 foundation).

Index

Constants

View Source
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 UTCDayWindow

func UTCDayWindow(t time.Time) (since, until time.Time)

func ValidateOverlay

func ValidateOverlay(cfg *Config) error

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 Clone

func Clone(cfg *Config) *Config

Clone returns a deep copy of cfg (nil-safe).

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

func Merge(base, overlay *Config) *Config

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

func (c *Config) CancelInflightOnHardBreach() bool

CancelInflightOnHardBreach is true when on_hard_breach is cancel_inflight.

func (*Config) Enabled

func (c *Config) Enabled() bool

Enabled reports whether any budget cap is configured.

func (*Config) HasPricebook

func (c *Config) HasPricebook() bool

HasPricebook reports whether any model prices are configured.

func (*Config) LookupCaps

func (c *Config) LookupCaps(tenantID, agentID string) (tenantCap, agentCap *BudgetCap)

LookupCaps returns tenant and agent caps for a create (agent may be nil).

func (*Config) ReservationEnabled

func (c *Config) ReservationEnabled() bool

ReservationEnabled is true when any hold amount is configured (global or per-agent override).

func (*Config) ReservationFor

func (c *Config) ReservationFor(tenantID, agentID string) (usd float64, tokens int64)

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

func (c *Config) RoutingSoftPct() float64

RoutingSoftPct returns routing threshold (routing > alerts > 80).

func (*Config) SoftPct

func (c *Config) SoftPct() float64

SoftPct returns the configured approach threshold (default 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

func (p Pricebook) EstimateUSD(model string, tokensIn, tokensOut int64, costUSD float64) float64

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.

func (Pricebook) HasModel

func (p Pricebook) HasModel(model string) bool

HasModel reports whether model (or its provider-suffix soft match) has a pricebook row. Used to distinguish "intentionally free / tokens-only" (empty book, or row with $0 rates) from "admin forgot this model".

type ReservationAmount

type ReservationAmount struct {
	USDPerRun    float64
	TokensPerRun int64
}

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

type UsageSnapshot struct {
	TokensIn  int64
	TokensOut int64
	USD       float64
	Runs      int64
}

UsageSnapshot is current-day spend for budget evaluation.

func (UsageSnapshot) Tokens

func (u UsageSnapshot) Tokens() int64

Tokens returns tokens_in + tokens_out.

Jump to

Keyboard shortcuts

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