budget

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package budget 提供令牌预算管理与成本控制能力。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alert

type Alert struct {
	Type      AlertType `json:"type"`
	Message   string    `json:"message"`
	Threshold float64   `json:"threshold"`
	Current   float64   `json:"current"`
	Timestamp time.Time `json:"timestamp"`
}

警报代表预算警报。

type AlertHandler

type AlertHandler func(alert Alert)

警报汉德勒处理预算警报.

type AlertType

type AlertType string

提醒Type代表预算提醒的类型.

const (
	AlertTokenMinute AlertType = "token_minute_threshold"
	AlertTokenHour   AlertType = "token_hour_threshold"
	AlertTokenDay    AlertType = "token_day_threshold"
	AlertCostDay     AlertType = "cost_day_threshold"
	AlertLimitHit    AlertType = "limit_hit"
)

type BudgetConfig

type BudgetConfig struct {
	MaxTokensPerRequest int           `json:"max_tokens_per_request"`
	MaxTokensPerMinute  int           `json:"max_tokens_per_minute"`
	MaxTokensPerHour    int           `json:"max_tokens_per_hour"`
	MaxTokensPerDay     int           `json:"max_tokens_per_day"`
	MaxCostPerRequest   float64       `json:"max_cost_per_request"`
	MaxCostPerDay       float64       `json:"max_cost_per_day"`
	AlertThreshold      float64       `json:"alert_threshold"` // 0.0-1.0, alert when usage exceeds this
	AutoThrottle        bool          `json:"auto_throttle"`
	ThrottleDelay       time.Duration `json:"throttle_delay"`
}

预算Config 配置符号预算管理 。

func DefaultBudgetConfig

func DefaultBudgetConfig() BudgetConfig

默认预览返回合理的默认值 。

type BudgetStatus

type BudgetStatus struct {
	TokensUsedMinute  int64      `json:"tokens_used_minute"`
	TokensUsedHour    int64      `json:"tokens_used_hour"`
	TokensUsedDay     int64      `json:"tokens_used_day"`
	CostUsedDay       float64    `json:"cost_used_day"`
	MinuteUtilization float64    `json:"minute_utilization"`
	HourUtilization   float64    `json:"hour_utilization"`
	DayUtilization    float64    `json:"day_utilization"`
	CostUtilization   float64    `json:"cost_utilization"`
	IsThrottled       bool       `json:"is_throttled"`
	ThrottleUntil     *time.Time `json:"throttle_until,omitempty"`
}

预算状况是目前的预算状况。

type TokenBudgetManager

type TokenBudgetManager struct {
	// contains filtered or unexported fields
}

TokenBudgetManager管理符名预算并强制执行限制.

func NewTokenBudgetManager

func NewTokenBudgetManager(config BudgetConfig, logger *zap.Logger) *TokenBudgetManager

NewTokenBudgetManager 创建了新的代币预算管理器.

func (*TokenBudgetManager) CheckBudget

func (m *TokenBudgetManager) CheckBudget(ctx context.Context, estimatedTokens int, estimatedCost float64) error

检查预算是否在预算范围内 。

func (*TokenBudgetManager) GetStatus

func (m *TokenBudgetManager) GetStatus() BudgetStatus

Get Status 返回当前预算状况 。

func (*TokenBudgetManager) OnAlert

func (m *TokenBudgetManager) OnAlert(handler AlertHandler)

OnAlert登记了一个警报处理器。

func (*TokenBudgetManager) RecordUsage

func (m *TokenBudgetManager) RecordUsage(record UsageRecord)

记录Usage记录符和成本使用.

func (*TokenBudgetManager) Reset

func (m *TokenBudgetManager) Reset()

重置所有计数器(用于测试).

type UsageRecord

type UsageRecord struct {
	Timestamp time.Time `json:"timestamp"`
	Tokens    int       `json:"tokens"`
	Cost      float64   `json:"cost"`
	Model     string    `json:"model"`
	RequestID string    `json:"request_id"`
	UserID    string    `json:"user_id,omitempty"`
	AgentID   string    `json:"agent_id,omitempty"`
}

用法记录代表单一使用记录.

Jump to

Keyboard shortcuts

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