model

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package model provides model selection, cost tracking, and escalation chains.

This package helps choose the appropriate LLM model for different task types and tracks token usage for cost estimation. It is designed to be task-type agnostic - define your own task types and map them to model tiers.

Model Selection

selector := model.NewSelector(
    model.WithThinkingModel(model.ModelOpus),
    model.WithDefaultModel(model.ModelSonnet),
    model.WithFastModel(model.ModelHaiku),
)
m := selector.SelectForTier(model.TierThinking)

Cost Tracking

tracker := model.NewCostTracker()
tracker.Record(model.ModelSonnet, 1000, 500)  // input, output tokens
cost := tracker.EstimatedCost()

Model Escalation

Escalation chains define how to retry with more capable models:

state := model.NewEscalationState(&model.DefaultEscalation, model.ModelSonnet)
for !state.Exhausted() {
    resp, err := tryRequest(state.CurrentModel)
    if err == nil {
        return resp, nil
    }
    state.RecordFailure(err)  // May escalate to next model
}

Package model provides model selection, escalation chains, and cost tracking.

The package implements a tiered model selection strategy:

  • Thinking tier (opus): Complex reasoning, architecture, risk assessment
  • Default tier (sonnet): Implementation, review, general tasks
  • Fast tier (haiku): Search, simple transforms, high-volume

This package is designed to be task-type agnostic. Higher-level packages (like devflow) can define their own task types and map them to model tiers.

Index

Constants

This section is empty.

Variables

View Source
var DefaultEscalation = EscalationChain{
	Models:      []ModelName{ModelSonnet, ModelOpus},
	MaxAttempts: 3,
}

DefaultEscalation is the standard escalation chain.

View Source
var FullEscalation = EscalationChain{
	Models:      []ModelName{ModelHaiku, ModelSonnet, ModelOpus},
	MaxAttempts: 5,
}

FullEscalation starts from haiku and goes through all tiers.

View Source
var ModelPrices = map[ModelName]ModelPricing{
	ModelOpus:   {InputPerMillion: 15.0, OutputPerMillion: 75.0},
	ModelSonnet: {InputPerMillion: 3.0, OutputPerMillion: 15.0},
	ModelHaiku:  {InputPerMillion: 0.25, OutputPerMillion: 1.25},
}

ModelPrices contains current pricing for Claude models (as of 2025).

View Source
var NoEscalation = EscalationChain{
	Models:      nil,
	MaxAttempts: 3,
}

NoEscalation disables model escalation (retry same model).

Functions

func NewContext

func NewContext(ctx context.Context, selector *Selector) context.Context

NewContext returns a new context with the selector attached.

Types

type CostTracker

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

CostTracker tracks token usage and estimated costs across models.

func NewCostTracker

func NewCostTracker() *CostTracker

NewCostTracker creates a new cost tracker.

func (*CostTracker) EstimatedCost

func (t *CostTracker) EstimatedCost() float64

EstimatedCost calculates the estimated cost based on current pricing.

func (*CostTracker) EstimatedCostByModel

func (t *CostTracker) EstimatedCostByModel() map[ModelName]float64

EstimatedCostByModel returns the estimated cost for each model.

func (*CostTracker) Record

func (t *CostTracker) Record(model ModelName, input, output int)

Record adds a usage record for the given model.

func (*CostTracker) RecordUsage

func (t *CostTracker) RecordUsage(model ModelName, usage Usage)

RecordUsage adds a usage record for the given model.

func (*CostTracker) Reset

func (t *CostTracker) Reset()

Reset clears all tracked usage.

func (*CostTracker) Summary

func (t *CostTracker) Summary() map[ModelName]Usage

Summary returns a copy of all usage totals.

func (*CostTracker) TotalUsage

func (t *CostTracker) TotalUsage() Usage

TotalUsage returns aggregated usage across all models.

func (*CostTracker) Usage

func (t *CostTracker) Usage(model ModelName) Usage

Usage returns the usage for a specific model.

type EscalationChain

type EscalationChain struct {
	// Models in ascending order of capability (e.g., haiku, sonnet, opus)
	Models []ModelName

	// MaxAttempts is the maximum total attempts before giving up
	MaxAttempts int
}

EscalationChain defines the order of models to try when escalating.

func (*EscalationChain) CanEscalate

func (e *EscalationChain) CanEscalate(current ModelName) bool

CanEscalate returns true if the current model can escalate to a higher tier.

func (*EscalationChain) HighestModel

func (e *EscalationChain) HighestModel() ModelName

HighestModel returns the highest capability model in the chain.

func (*EscalationChain) Next

func (e *EscalationChain) Next(current ModelName, attempt int) (ModelName, bool)

Next returns the next model to try after a failure. Returns the next model in the chain and whether to continue. If already at the highest tier or max attempts reached, returns ("", false).

type EscalationState

type EscalationState struct {
	Chain        *EscalationChain
	CurrentModel ModelName
	Attempt      int
	LastError    error
}

EscalationState tracks the state of an escalation attempt.

func NewEscalationState

func NewEscalationState(chain *EscalationChain, startModel ModelName) *EscalationState

NewEscalationState creates a new escalation state starting at the given model.

func (*EscalationState) Exhausted

func (s *EscalationState) Exhausted() bool

Exhausted returns true if all attempts have been used.

func (*EscalationState) RecordFailure

func (s *EscalationState) RecordFailure(err error) bool

RecordFailure records a failed attempt and escalates if possible. Returns true if escalation occurred and there are more attempts available.

type ModelName

type ModelName string

ModelName represents a Claude model name.

const (
	ModelOpus   ModelName = "opus"
	ModelSonnet ModelName = "sonnet"
	ModelHaiku  ModelName = "haiku"
)

Model name constants for Claude models.

type ModelPricing

type ModelPricing struct {
	InputPerMillion  float64
	OutputPerMillion float64
}

ModelPricing holds per-million-token pricing for a model.

type Selector

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

Selector provides task-based model selection with override support. Uses any for task type to allow flexibility - higher-level packages can define their own task types.

func FromContext

func FromContext(ctx context.Context) *Selector

FromContext retrieves the selector from the context. Returns a default selector if none is present.

func NewSelector

func NewSelector(opts ...SelectorOption) *Selector

NewSelector creates a new model selector with the given options.

func (*Selector) Clone

func (s *Selector) Clone() *Selector

Clone returns a copy of the selector with the same configuration.

func (*Selector) Select

func (s *Selector) Select(task any) ModelName

Select returns the appropriate model for the given task. Priority order: global override > task override > task default > tier model > sonnet fallback

func (*Selector) SelectForTier

func (s *Selector) SelectForTier(tier Tier) ModelName

SelectForTier returns the model for a specific tier.

func (*Selector) WithGlobal

func (s *Selector) WithGlobal(model ModelName) *Selector

WithGlobal returns a new selector with a global override applied.

type SelectorOption

type SelectorOption func(*Selector)

SelectorOption configures a Selector.

func WithDefaultModel

func WithDefaultModel(model ModelName) SelectorOption

WithDefaultModel sets the default model for most tasks.

func WithDefaults

func WithDefaults(defaults map[any]ModelName) SelectorOption

WithDefaults sets the default task-to-model mapping.

func WithFastModel

func WithFastModel(model ModelName) SelectorOption

WithFastModel sets the model for simple, high-volume tasks.

func WithGlobalOverride

func WithGlobalOverride(model ModelName) SelectorOption

WithGlobalOverride sets a global model that overrides all selections.

func WithTaskOverride

func WithTaskOverride(task any, model ModelName) SelectorOption

WithTaskOverride sets a model override for a specific task.

func WithTaskOverrides

func WithTaskOverrides(overrides map[any]ModelName) SelectorOption

WithTaskOverrides sets multiple task overrides at once.

func WithThinkingModel

func WithThinkingModel(model ModelName) SelectorOption

WithThinkingModel sets the model for complex reasoning tasks.

func WithTierFunc

func WithTierFunc(fn TierFunc) SelectorOption

WithTierFunc sets the function to determine a task's tier.

type Tier

type Tier int

Tier represents a model capability tier.

const (
	TierFast Tier = iota
	TierDefault
	TierThinking
)

Tier constants representing model capability levels.

func TierForModel

func TierForModel(model ModelName) Tier

TierForModel returns the tier for a given model.

func (Tier) String

func (t Tier) String() string

String returns the tier name.

type TierFunc

type TierFunc func(task any) Tier

TierFunc maps a task to its tier.

type Usage

type Usage struct {
	InputTokens  int
	OutputTokens int
	Requests     int
}

Usage tracks token usage for a model.

func (*Usage) Add

func (u *Usage) Add(other Usage)

Add adds the given usage to this usage.

func (*Usage) TotalTokens

func (u *Usage) TotalTokens() int

TotalTokens returns the total tokens used.

Jump to

Keyboard shortcuts

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