llmcost

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package llmcost holds the model-pricing primitives shared by the LLM budget and cost cap plugins: glob matching of model slugs, price resolution, model downgrade, and the stateless cost cap decision engine.

Index

Constants

View Source
const (
	BehaviorReject    = "reject"
	BehaviorDowngrade = "downgrade"
)

Behaviors taken when a model exceeds its cost ceiling.

View Source
const (
	UnknownReject      = "reject"
	UnknownPassThrough = "pass_through"
	UnknownAssumeMax   = "assume_max"
)

Policies for models whose price cannot be resolved.

View Source
const CostCapErrorType = "model_too_expensive"

CostCapErrorType is the error type emitted when a model exceeds its cost cap.

View Source
const DowngradeHeader = "X-NeuralTrust-Model-Downgraded"

DowngradeHeader flags a response whose model was downgraded, carrying the "original→target" transition as its value.

Variables

This section is empty.

Functions

func ApplyDowngrade

func ApplyDowngrade(req *infracontext.RequestContext, orig, target string) (string, []byte, map[string][]string, bool)

ApplyDowngrade rewrites the request body to target when the downgrade is valid, returning the new model, the rewritten body, and the downgrade header.

func BestMatch

func BestMatch[T any](m map[string]T, s string) (T, bool)

BestMatch returns the value whose key best matches s. An exact key wins over any glob; among globs the most specific pattern (most literal characters) wins, ties broken lexicographically.

func CostCapError

func CostCapError(dec Decision) *appplugins.PluginError

CostCapError builds the 403 plugin error for a cost cap violation.

func GlobMatch

func GlobMatch(pattern, s string) bool

GlobMatch reports whether s matches pattern, where '*' matches any run of characters (including the empty string).

func MicroUSD

func MicroUSD(costUSD float64) int64

MicroUSD converts a USD amount to micro-USD, rounding half away from zero.

func Per1k

func Per1k(perToken float64) float64

Per1k converts a per-token rate to a per-1000-token rate.

func PriceFor

func PriceFor(ctx context.Context, resolver appcatalog.PricingResolver, custom map[string]CustomPrice, provider string, models ...string) (float64, float64, bool)

PriceFor resolves per-token input and output USD prices for the first matching model. A custom overlay is consulted before the builtin resolver.

func ResolveDowngrade

func ResolveDowngrade(provider, target string, allowed []string) (string, bool)

ResolveDowngrade validates a downgrade target against the request provider and the allowed models, returning the bare target model when it is usable.

Types

type CapConfig

type CapConfig struct {
	Enabled             bool               `mapstructure:"enabled"`
	MaxInputCostPer1k   float64            `mapstructure:"max_input_cost_per_1k_tokens"`
	MaxOutputCostPer1k  float64            `mapstructure:"max_output_cost_per_1k_tokens"`
	PerModelOverrides   map[string]Ceiling `mapstructure:"per_model_overrides"`
	BehaviorOnViolation string             `mapstructure:"behavior_on_violation"`
	DowngradeTo         string             `mapstructure:"downgrade_to"`
	UnknownModel        string             `mapstructure:"unknown_model"`
}

CapConfig is the stateless per-request cost cap configuration.

func (*CapConfig) Validate

func (c *CapConfig) Validate() error

Validate checks an enabled cost cap configuration.

type Ceiling

type Ceiling struct {
	MaxInputCostPer1k  float64 `mapstructure:"max_input_cost_per_1k_tokens"`
	MaxOutputCostPer1k float64 `mapstructure:"max_output_cost_per_1k_tokens"`
}

Ceiling is a per-1k-token USD price ceiling.

func EvaluateCeiling

func EvaluateCeiling(cc *CapConfig, models ...string) Ceiling

EvaluateCeiling resolves the effective ceiling for a model, preferring the most specific per-model override before the global ceiling.

type CustomPrice

type CustomPrice struct {
	Input  float64 `mapstructure:"input"`
	Output float64 `mapstructure:"output"`
}

CustomPrice is a per-token USD overlay rate for a model slug or pattern.

type Decision

type Decision struct {
	Kind        DecisionKind
	Unknown     bool
	Model       string
	InputPrice  float64
	OutputPrice float64
	MaxInput    float64
	MaxOutput   float64
}

Decision is the outcome of evaluating a model against its cost ceiling.

func Decide

func Decide(ctx context.Context, resolver appcatalog.PricingResolver, custom map[string]CustomPrice, cc *CapConfig, provider string, models ...string) Decision

Decide evaluates a model against its cost cap, resolving its price first.

type DecisionKind

type DecisionKind int

DecisionKind classifies the outcome of a cost cap evaluation.

const (
	DecisionAllow DecisionKind = iota
	DecisionViolation
)

type Telemetry

type Telemetry struct {
	Violation   bool
	Unknown     bool
	InputPrice  float64
	OutputPrice float64
	MaxInput    float64
	MaxOutput   float64
}

Telemetry is a transport-neutral snapshot of a cost cap decision for metrics.

func TelemetryFrom

func TelemetryFrom(dec Decision) *Telemetry

TelemetryFrom projects a Decision into Telemetry.

Jump to

Keyboard shortcuts

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