Help us shape the future of Go! Take the 2026 Go Developer Survey

cost

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cost implements per-service cost alerting for nSelf AI and media plugins. It runs hourly (via the cron plugin or nself doctor), queries np_ai_usage.cost_usd for the last 24h, and fires a Slack webhook when any configured budget is exceeded.

Configuration env vars:

COST_ALERT_ENABLED       true | false (default false)
COST_BUDGET_AI_DAILY_USD float, e.g. "5.00"
COST_BUDGET_MEDIA_DAILY_USD float
COST_BUDGET_TOTAL_DAILY_USD float
SLACK_WEBHOOK_URL        https://hooks.slack.com/...
SLACK_ALERT_CHANNEL      #nself-alerts (informational only — embedded in message)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enabled

func Enabled() bool

Enabled returns true when COST_ALERT_ENABLED=true.

Types

type Alerter

type Alerter struct {
	DB         *sql.DB
	Budget     Budget
	HTTPClient *http.Client
	Log        *slog.Logger
}

Alerter checks cost against budgets and sends Slack notifications on breach.

func New

func New(db *sql.DB, budget Budget) *Alerter

New creates an Alerter with defaults.

func (*Alerter) CheckAndAlert

func (a *Alerter) CheckAndAlert(ctx context.Context) error

CheckAndAlert queries the last 24h of cost_usd from np_ai_usage, compares against configured budgets, and posts to Slack on breach. It is safe to call from a cron handler or periodic health check.

type Budget

type Budget struct {
	AIDailyUSD    float64
	MediaDailyUSD float64
	TotalDailyUSD float64
}

Budget holds per-service daily spend limits in USD. A zero value means "no limit for this service".

func LoadBudgetFromEnv

func LoadBudgetFromEnv() Budget

LoadBudgetFromEnv reads COST_BUDGET_* vars and returns the parsed Budget. Malformed values are ignored (budget treated as unlimited).

Jump to

Keyboard shortcuts

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