guardrail

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package guardrail tracks daily token usage and enforces limits.

Index

Constants

View Source
const DefaultMaxDailyTokens = 50000000

DefaultMaxDailyTokens is the default daily token limit.

Variables

This section is empty.

Functions

func FormatUsage

func FormatUsage(u Usage, limit int64) string

FormatUsage returns a human-readable usage summary.

func WrapModel

func WrapModel(llm model.LLM, tracker *Tracker) model.LLM

WrapModel wraps an LLM model to track token usage via the guardrail tracker. Each response's UsageMetadata is recorded. If the daily limit is exceeded, subsequent calls return an error.

Types

type LimitExceededError

type LimitExceededError struct {
	Limit int64
	Used  int64
	Asked int64
}

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

func New(maxDailyTokens int64) *Tracker

New creates a tracker with the given daily token limit. It loads any existing usage for today from ~/.pi-go/usage.json.

func NewWithPath

func NewWithPath(maxDailyTokens int64, path string) *Tracker

NewWithPath creates a tracker with a custom file path (for testing).

func (*Tracker) Add

func (t *Tracker) Add(inputTokens, outputTokens int32) error

Add records token usage from an LLM response. Returns an error if the daily limit would be exceeded.

func (*Tracker) Check

func (t *Tracker) Check() error

Check returns an error if the daily limit is already exceeded.

func (*Tracker) ContextPercentUsed added in v0.0.18

func (t *Tracker) ContextPercentUsed() float64

ContextPercentUsed returns the percentage of the context window used (0-100+). Returns 0 if context window size is unknown.

func (*Tracker) ContextWindowSize added in v0.0.18

func (t *Tracker) ContextWindowSize() int64

ContextWindowSize returns the model's context window size.

func (*Tracker) Current

func (t *Tracker) Current() Usage

Current returns the current usage snapshot.

func (*Tracker) LastPromptTokens added in v0.0.18

func (t *Tracker) LastPromptTokens() int64

LastPromptTokens returns the most recent prompt token count from an LLM response. This represents how much of the context window is currently used.

func (*Tracker) Limit

func (t *Tracker) Limit() int64

Limit returns the configured daily token limit.

func (*Tracker) PercentUsed

func (t *Tracker) PercentUsed() float64

PercentUsed returns the percentage of daily limit consumed (0-100+). Returns 0 if unlimited.

func (*Tracker) Remaining

func (t *Tracker) Remaining() int64

Remaining returns how many tokens are left today. Returns -1 if unlimited.

func (*Tracker) SetContextWindowSize added in v0.0.18

func (t *Tracker) SetContextWindowSize(size int64)

SetContextWindowSize sets the model's context window size for display.

func (*Tracker) SetLimit

func (t *Tracker) SetLimit(limit int64)

SetLimit updates the daily token limit.

func (*Tracker) TotalUsed

func (t *Tracker) TotalUsed() int64

TotalUsed returns total tokens consumed today.

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

func (u *Usage) TotalTokens() int64

TotalTokens returns the combined input + output token count.

Jump to

Keyboard shortcuts

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