usage

package
v6.2.36 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package usage provides usage tracking and logging functionality for the CLI Proxy API server. It includes plugins for monitoring API usage, token consumption, and other metrics to help with observability and billing purposes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetStatisticsEnabled added in v6.0.5

func SetStatisticsEnabled(enabled bool)

SetStatisticsEnabled toggles whether in-memory statistics are recorded.

func StatisticsEnabled added in v6.0.5

func StatisticsEnabled() bool

StatisticsEnabled reports the current recording state.

Types

type APISnapshot

type APISnapshot struct {
	TotalRequests int64                    `json:"total_requests"`
	TotalTokens   int64                    `json:"total_tokens"`
	Models        map[string]ModelSnapshot `json:"models"`
}

APISnapshot summarises metrics for a single API key.

type LoggerPlugin

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

LoggerPlugin collects in-memory request statistics for usage analysis. It implements coreusage.Plugin to receive usage records emitted by the runtime.

func NewLoggerPlugin

func NewLoggerPlugin() *LoggerPlugin

NewLoggerPlugin constructs a new logger plugin instance.

Returns:

  • *LoggerPlugin: A new logger plugin instance wired to the shared statistics store.

func (*LoggerPlugin) HandleUsage

func (p *LoggerPlugin) HandleUsage(ctx context.Context, record coreusage.Record)

HandleUsage implements coreusage.Plugin. It updates the in-memory statistics store whenever a usage record is received.

Parameters:

  • ctx: The context for the usage record
  • record: The usage record to aggregate

type ModelSnapshot

type ModelSnapshot struct {
	TotalRequests int64           `json:"total_requests"`
	TotalTokens   int64           `json:"total_tokens"`
	Details       []RequestDetail `json:"details"`
}

ModelSnapshot summarises metrics for a specific model.

type RequestDetail

type RequestDetail struct {
	Timestamp time.Time  `json:"timestamp"`
	Source    string     `json:"source"`
	Tokens    TokenStats `json:"tokens"`
	Failed    bool       `json:"failed"`
}

RequestDetail stores the timestamp and token usage for a single request.

type RequestStatistics

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

RequestStatistics maintains aggregated request metrics in memory.

func GetRequestStatistics

func GetRequestStatistics() *RequestStatistics

GetRequestStatistics returns the shared statistics store.

func NewRequestStatistics

func NewRequestStatistics() *RequestStatistics

NewRequestStatistics constructs an empty statistics store.

func (*RequestStatistics) Record

func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record)

Record ingests a new usage record and updates the aggregates.

func (*RequestStatistics) Snapshot

func (s *RequestStatistics) Snapshot() StatisticsSnapshot

Snapshot returns a copy of the aggregated metrics for external consumption.

type StatisticsSnapshot

type StatisticsSnapshot struct {
	TotalRequests int64 `json:"total_requests"`
	SuccessCount  int64 `json:"success_count"`
	FailureCount  int64 `json:"failure_count"`
	TotalTokens   int64 `json:"total_tokens"`

	APIs map[string]APISnapshot `json:"apis"`

	RequestsByDay  map[string]int64 `json:"requests_by_day"`
	RequestsByHour map[string]int64 `json:"requests_by_hour"`
	TokensByDay    map[string]int64 `json:"tokens_by_day"`
	TokensByHour   map[string]int64 `json:"tokens_by_hour"`
}

StatisticsSnapshot represents an immutable view of the aggregated metrics.

type TokenStats

type TokenStats struct {
	InputTokens     int64 `json:"input_tokens"`
	OutputTokens    int64 `json:"output_tokens"`
	ReasoningTokens int64 `json:"reasoning_tokens"`
	CachedTokens    int64 `json:"cached_tokens"`
	TotalTokens     int64 `json:"total_tokens"`
}

TokenStats captures the token usage breakdown for a request.

Jump to

Keyboard shortcuts

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