cost

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CostCalculator

type CostCalculator interface {
	Calculate(usage *SessionUsage) (*SessionCost, error)
}

CostCalculator computes dollar cost from session usage.

type CostSource

type CostSource string

CostSource identifies where token/cost data was collected from.

const (
	CostSourceTranscript CostSource = "transcript"
	CostSourceHook       CostSource = "hook"
)

type DefaultCalculator

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

DefaultCalculator implements CostCalculator using a PricingProvider.

func NewDefaultCalculator

func NewDefaultCalculator(provider PricingProvider, sessionID uuid.UUID, source CostSource) *DefaultCalculator

NewDefaultCalculator creates a CostCalculator backed by the given PricingProvider.

func (*DefaultCalculator) Calculate

func (c *DefaultCalculator) Calculate(usage *SessionUsage) (*SessionCost, error)

Calculate computes the cost for each model in the usage and returns a SessionCost.

type ModelCost

type ModelCost struct {
	Model      string  `json:"model"`
	InputCost  float64 `json:"input_cost"`
	OutputCost float64 `json:"output_cost"`
	CacheCost  float64 `json:"cache_cost"`
	TotalCost  float64 `json:"total_cost"`
}

ModelCost represents the computed cost for a single model.

type ModelPricing

type ModelPricing struct {
	ID          string  `json:"id"`
	Name        string  `json:"name,omitempty"`
	InputPer1M  float64 `json:"input"`
	OutputPer1M float64 `json:"output"`
	CacheRead   float64 `json:"cache_read"`
	CacheWrite  float64 `json:"cache_write"`
}

ModelPricing contains the per-token pricing for a model.

type ModelUsage

type ModelUsage struct {
	Model            string `json:"model"`
	InputTokens      int64  `json:"input_tokens"`
	OutputTokens     int64  `json:"output_tokens"`
	CacheReadTokens  int64  `json:"cache_read_tokens"`
	CacheWriteTokens int64  `json:"cache_write_tokens"`
}

ModelUsage represents token usage for a single model within a session.

func (*ModelUsage) TotalTokens

func (m *ModelUsage) TotalTokens() int64

TotalTokens returns the total token count for this model.

type PricingProvider

type PricingProvider interface {
	// GetPricing returns pricing for a model ID (as seen in transcripts).
	// Returns nil, nil if model is not found (not an error).
	GetPricing(modelID string) (*ModelPricing, error)

	// ListModels returns all known model IDs.
	ListModels() []string
}

PricingProvider looks up pricing for a model.

type SessionCost

type SessionCost struct {
	SessionID  uuid.UUID    `json:"session_id"`
	Usage      SessionUsage `json:"usage"`
	Models     []ModelCost  `json:"models"`
	TotalCost  float64      `json:"total_cost"`
	Currency   string       `json:"currency"`
	Source     CostSource   `json:"source"`
	ComputedAt time.Time    `json:"computed_at"`
}

SessionCost represents the computed cost for an entire session.

type SessionUsage

type SessionUsage struct {
	Models           []ModelUsage `json:"models"`
	InputTokens      int64        `json:"input_tokens"`
	OutputTokens     int64        `json:"output_tokens"`
	CacheReadTokens  int64        `json:"cache_read_tokens"`
	CacheWriteTokens int64        `json:"cache_write_tokens"`
}

SessionUsage represents aggregated token usage for an entire session.

func (*SessionUsage) Aggregate

func (s *SessionUsage) Aggregate()

Aggregate recomputes the top-level sums from the Models slice.

func (*SessionUsage) TotalTokens

func (s *SessionUsage) TotalTokens() int64

TotalTokens returns the total token count across all models.

type TokenCollector

type TokenCollector interface {
	// Collect returns token usage for a session.
	// transcriptPath may be empty if unavailable.
	// Returns nil, nil if no usage data is available.
	Collect(ctx context.Context, transcriptPath string) (*SessionUsage, error)

	// Source returns the data source this collector reads from.
	Source() CostSource
}

TokenCollector extracts token usage from an agent session.

Jump to

Keyboard shortcuts

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