Documentation
¶
Overview ¶
Package guardrail tracks daily token usage and enforces limits.
Index ¶
- Constants
- func FormatUsage(u Usage, limit int64) string
- func WrapModel(llm model.LLM, tracker *Tracker) model.LLM
- type LimitExceededError
- type Tracker
- func (t *Tracker) Add(inputTokens, outputTokens int32) error
- func (t *Tracker) Check() error
- func (t *Tracker) Current() Usage
- func (t *Tracker) Limit() int64
- func (t *Tracker) PercentUsed() float64
- func (t *Tracker) Remaining() int64
- func (t *Tracker) SetLimit(limit int64)
- func (t *Tracker) TotalUsed() int64
- type Usage
Constants ¶
const DefaultMaxDailyTokens = 0
DefaultMaxDailyTokens is the default daily token limit (0 = unlimited).
Variables ¶
This section is empty.
Functions ¶
func FormatUsage ¶
FormatUsage returns a human-readable usage summary.
Types ¶
type LimitExceededError ¶
LimitExceededError is returned when the daily token limit is reached.
func (*LimitExceededError) Error ¶
func (e *LimitExceededError) Error() string
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker tracks daily token usage and enforces a configurable limit.
func New ¶
New creates a tracker with the given daily token limit. It loads any existing usage for today from ~/.pi-go/usage.json.
func NewWithPath ¶
NewWithPath creates a tracker with a custom file path (for testing).
func (*Tracker) Add ¶
Add records token usage from an LLM response. Returns an error if the daily limit would be exceeded.
func (*Tracker) PercentUsed ¶
PercentUsed returns the percentage of daily limit consumed (0-100+). Returns 0 if unlimited.
func (*Tracker) Remaining ¶
Remaining returns how many tokens are left today. Returns -1 if unlimited.
type Usage ¶
type Usage struct {
Date string `json:"date"` // YYYY-MM-DD
InputTokens int64 `json:"input_tokens"` // total prompt tokens
OutputTokens int64 `json:"output_tokens"` // total completion tokens
Requests int64 `json:"requests"` // number of LLM calls
}
Usage tracks token consumption for the current day.
func (*Usage) TotalTokens ¶
TotalTokens returns the combined input + output token count.