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 ¶
- Variables
- func NewContext(ctx context.Context, selector *Selector) context.Context
- type CostTracker
- func (t *CostTracker) EstimatedCost() float64
- func (t *CostTracker) EstimatedCostByModel() map[ModelName]float64
- func (t *CostTracker) Record(model ModelName, input, output int)
- func (t *CostTracker) RecordUsage(model ModelName, usage Usage)
- func (t *CostTracker) Reset()
- func (t *CostTracker) Summary() map[ModelName]Usage
- func (t *CostTracker) TotalUsage() Usage
- func (t *CostTracker) Usage(model ModelName) Usage
- type EscalationChain
- type EscalationState
- type ModelName
- type ModelPricing
- type Selector
- type SelectorOption
- func WithDefaultModel(model ModelName) SelectorOption
- func WithDefaults(defaults map[any]ModelName) SelectorOption
- func WithFastModel(model ModelName) SelectorOption
- func WithGlobalOverride(model ModelName) SelectorOption
- func WithTaskOverride(task any, model ModelName) SelectorOption
- func WithTaskOverrides(overrides map[any]ModelName) SelectorOption
- func WithThinkingModel(model ModelName) SelectorOption
- func WithTierFunc(fn TierFunc) SelectorOption
- type Tier
- type TierFunc
- type Usage
Constants ¶
This section is empty.
Variables ¶
var DefaultEscalation = EscalationChain{ Models: []ModelName{ModelSonnet, ModelOpus}, MaxAttempts: 3, }
DefaultEscalation is the standard escalation chain.
var FullEscalation = EscalationChain{ Models: []ModelName{ModelHaiku, ModelSonnet, ModelOpus}, MaxAttempts: 5, }
FullEscalation starts from haiku and goes through all tiers.
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).
var NoEscalation = EscalationChain{ Models: nil, MaxAttempts: 3, }
NoEscalation disables model escalation (retry same model).
Functions ¶
Types ¶
type CostTracker ¶
type CostTracker struct {
// contains filtered or unexported fields
}
CostTracker tracks token usage and estimated costs across models.
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) 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 ModelPricing ¶
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 ¶
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) Select ¶
Select returns the appropriate model for the given task. Priority order: global override > task override > task default > tier model > sonnet fallback
func (*Selector) SelectForTier ¶
SelectForTier returns the model for a specific tier.
func (*Selector) WithGlobal ¶
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.
Tier constants representing model capability levels.
func TierForModel ¶
TierForModel returns the tier for a given model.
type Usage ¶
Usage tracks token usage for a model.
func (*Usage) TotalTokens ¶
TotalTokens returns the total tokens used.