usage

package
v7.1.76 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultServiceTier = "default"

DefaultServiceTier is used when a request does not specify service_tier.

Variables

This section is empty.

Functions

func PublishRecord

func PublishRecord(ctx context.Context, record Record)

PublishRecord publishes a record using the default manager.

func ReasoningEffortFromContext added in v7.1.18

func ReasoningEffortFromContext(ctx context.Context) string

ReasoningEffortFromContext returns the client-requested reasoning effort stored in ctx.

func RegisterNamedPlugin added in v7.1.47

func RegisterNamedPlugin(name string, plugin Plugin)

RegisterNamedPlugin registers or replaces a named plugin on the default manager.

func RegisterPlugin

func RegisterPlugin(plugin Plugin)

RegisterPlugin registers a plugin on the default manager.

func RequestedModelAliasFromContext

func RequestedModelAliasFromContext(ctx context.Context) string

RequestedModelAliasFromContext returns the client-requested model name stored in ctx.

func ServiceTierFromContext added in v7.1.27

func ServiceTierFromContext(ctx context.Context) string

ServiceTierFromContext returns the client-requested service tier stored in ctx.

func StartDefault

func StartDefault(ctx context.Context)

StartDefault starts the default manager's dispatcher.

func StopDefault

func StopDefault()

StopDefault stops the default manager's dispatcher.

func WithReasoningEffort added in v7.1.18

func WithReasoningEffort(ctx context.Context, effort string) context.Context

WithReasoningEffort stores the client-requested reasoning effort for usage sinks.

func WithRequestedModelAlias

func WithRequestedModelAlias(ctx context.Context, alias string) context.Context

WithRequestedModelAlias stores the client-requested model name for usage sinks.

func WithServiceTier added in v7.1.27

func WithServiceTier(ctx context.Context, tier string) context.Context

WithServiceTier stores the client-requested service tier for usage sinks.

Types

type Detail

type Detail struct {
	InputTokens         int64
	OutputTokens        int64
	ReasoningTokens     int64
	CachedTokens        int64
	CacheReadTokens     int64
	CacheCreationTokens int64
	TotalTokens         int64
}

Detail holds the token usage breakdown.

type Failure

type Failure struct {
	StatusCode int
	Body       string
}

Failure holds HTTP failure metadata for an upstream request attempt.

type Manager

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

Manager maintains a queue of usage records and delivers them to registered plugins.

func DefaultManager

func DefaultManager() *Manager

DefaultManager returns the global usage manager instance.

func NewManager

func NewManager(buffer int) *Manager

NewManager constructs a manager with a buffered queue.

func (*Manager) Publish

func (m *Manager) Publish(ctx context.Context, record Record)

Publish enqueues a usage record for processing. If no plugin is registered the record will be discarded downstream.

func (*Manager) Register

func (m *Manager) Register(plugin Plugin)

Register appends a plugin to the delivery list.

func (*Manager) RegisterNamed added in v7.1.47

func (m *Manager) RegisterNamed(name string, plugin Plugin)

RegisterNamed registers or replaces a plugin by name.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context)

Start launches the background dispatcher. Calling Start multiple times is safe.

func (*Manager) Stop

func (m *Manager) Stop()

Stop stops the dispatcher and drains the queue.

type Plugin

type Plugin interface {
	HandleUsage(ctx context.Context, record Record)
}

Plugin consumes usage records emitted by the proxy runtime.

type Record

type Record struct {
	Provider string
	// ExecutorType stores the concrete executor type that handled the request.
	ExecutorType string
	Model        string
	Alias        string
	APIKey       string
	AuthID       string
	AuthIndex    string
	AuthType     string
	Source       string
	// ReasoningEffort stores the translated upstream thinking level for request event logs.
	ReasoningEffort string
	// ServiceTier stores the client-requested service tier for request event logs.
	ServiceTier string
	RequestedAt time.Time
	Latency     time.Duration
	TTFT        time.Duration
	Failed      bool
	Fail        Failure
	Detail      Detail
	// ResponseHeaders stores a snapshot of upstream response headers for usage sinks.
	ResponseHeaders http.Header
}

Record contains the usage statistics captured for a single provider request.

Jump to

Keyboard shortcuts

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