usage

package
v7.2.49 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 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

func ReasoningEffortFromContext(ctx context.Context) string

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

func RegisterNamedPlugin

func RegisterNamedPlugin(name string, plugin Plugin)

RegisterNamedPlugin registers or replaces a named plugin on the default manager. The plugin host uses this to keep the delivery list stable across plugin reloads.

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 ResetDefaultManager

func ResetDefaultManager()

ResetDefaultManager resets the global usage manager to a fresh reusable instance.

func ServiceTierFromContext added in v7.2.49

func ServiceTierFromContext(ctx context.Context) string

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

func SetDefaultManager

func SetDefaultManager(manager *Manager)

SetDefaultManager replaces the global usage manager instance.

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

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.2.49

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. It is populated when a request fails with a structured upstream error so plugin usage sinks can report status codes and response snippets.

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 ResetDefaultManagerForTest

func ResetDefaultManagerForTest(tb interface{ Cleanup(func()) }) *Manager

ResetDefaultManagerForTest swaps the global usage manager for tests and restores it afterwards.

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

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

RegisterNamed registers or replaces a plugin by name. When name already exists the previously-registered plugin is replaced in place so the delivery order is preserved; otherwise the plugin is appended. Plugins registered by name can be refreshed (e.g. by the plugin host when reloading a plugin) without producing duplicates in the dispatch list.

func (*Manager) Reset

func (m *Manager) Reset()

Reset stops the dispatcher and clears pending state so the manager can be reused.

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.

func (*Manager) Wait

func (m *Manager) Wait()

Wait blocks until the dispatcher has drained the queue and exited.

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.
	// Plugin sinks use this to attribute traffic to a specific executor
	// implementation (e.g. "gemini-cli", "claude-cli", "codex-ws").
	ExecutorType string
	Model        string
	Alias        string
	APIKey       string
	AuthID       string
	AuthIndex    string
	// AuthType stores the credential type that backed the request (e.g.
	// "oauth", "api-key"). Empty means the value was not captured.
	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
	// FirstByteLatency / APIFirstByteLatency preserve the fork's historical
	// TTFB measurements for the file-backed usage logger and are kept populated
	// alongside TTFT below for compatibility with both fork and upstream sinks.
	FirstByteLatency    time.Duration
	APIFirstByteLatency time.Duration
	// TTFT is the upstream-aligned time-to-first-token duration used by plugin
	// usage sinks. It mirrors FirstByteLatency semantically.
	TTFT time.Duration
	// Failed flags a terminal failure for the request. Both Failed and Fail
	// carry failure information: Failed is the boolean summary, Fail carries
	// structured HTTP status/body detail when available.
	Failed bool
	Fail   Failure
	// ChunkCount tracks the number of stream chunks observed for the request.
	ChunkCount int64
	// ResponseBytes / APIResponseBytes preserve the fork's byte counters.
	ResponseBytes    int64
	APIResponseBytes int64
	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