llm

package
v1.0.24 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBudgetExceeded indicates the request exceeds available budget
	ErrBudgetExceeded = errors.New("budget exceeded")

	// ErrNoProviders indicates no providers are available
	ErrNoProviders = errors.New("no providers available")

	// ErrAllProvidersFailed indicates all providers in the chain failed
	ErrAllProvidersFailed = errors.New("all providers failed")
)

Functions

func ResolveModelKey

func ResolveModelKey(provider Provider, opts Options) string

ResolveModelKey returns the single model key that both the budget ceiling and the charge must be accounted against for this request.

Preference order: what the provider says it will actually send, then the caller's explicit ModelKey. The result is empty only when neither is known, and a configured tracker refuses an empty key rather than admitting spend it cannot price.

It deliberately never substitutes a placeholder such as "default": a configured price table has no entry for that, so the request would be refused while the provider bills a real model.

Types

type Estimator

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

Estimator counts tokens for budget checks, caching results per (model, input).

Entries are keyed by a digest of the input: the cache must not keep prompt text alive, because prompts carry repository content. Eviction is arbitrary, not LRU - the cache exists to avoid recomputation, not to guarantee hits.

func NewEstimator

func NewEstimator() *Estimator

NewEstimator creates a new token estimator

func (*Estimator) Estimate

func (e *Estimator) Estimate(provider Provider, input string, model string) int

Estimate returns the provider's own token count for input, falling back to the character heuristic when the provider cannot count.

func (*Estimator) EstimateTokens

func (e *Estimator) EstimateTokens(input string) int

EstimateTokens returns a model-independent token estimate for input.

type ModelResolver

type ModelResolver interface {
	ResolveModel(opts Options) string
}

ModelResolver is implemented by providers that can name, before the call is made, the concrete model they will bill against for a given set of options.

This exists because Options.ModelKey is routinely empty - DefaultOptions never sets it - while every provider here substitutes a model of its own ("gpt-4", "llama3", a configured LiteLLM model). Without a pre-call answer the ceiling is checked against a key no price table contains while the charge lands on a different one, so the ceiling reads a ledger that never moves.

type Options

type Options struct {
	System      string  `json:"system,omitempty"`
	Temperature float64 `json:"temperature,omitempty"`
	MaxTokens   int     `json:"max_tokens,omitempty"`
	ModelKey    string  `json:"model_key,omitempty"`
}

Options represents LLM request options. Every field here must be carried by a provider; a field no provider sends is a silent no-op for the caller.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns sensible defaults for LLM options.

Temperature is left at its zero value, deliberately (AUR-460): a fixed 0.3 default meant every request carried an explicit sampling parameter, and a growing family of gateway-served models (measured: gpt-5.6-luna, -sol, -terra) 400s on ANY explicit "temperature" value, including 0 and including that model's own advertised default. A caller that wants a specific temperature still sets Options.Temperature itself; the field stays on the struct for that. What no longer happens is this package picking a non-zero value nobody asked for and every provider dutifully forwarding it upstream.

type Orchestrator

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

Orchestrator manages LLM provider chains with fallback and budget enforcement

func NewOrchestrator

func NewOrchestrator(primary Provider, fallbacks []Provider, tracker *cost.Tracker) *Orchestrator

NewOrchestrator creates a new orchestrator with a primary provider and optional fallbacks

func (*Orchestrator) Complete

func (o *Orchestrator) Complete(ctx context.Context, prompt string, opts Options) (Response, error)

Complete executes a completion request with fallback chain and budget enforcement

func (*Orchestrator) GetProviderChain

func (o *Orchestrator) GetProviderChain() []string

GetProviderChain returns the current provider chain (primary + fallbacks)

func (*Orchestrator) RemainingBudget

func (o *Orchestrator) RemainingBudget() (float64, float64)

RemainingBudget returns the remaining budget (perRun, daily)

func (*Orchestrator) ResetPerRunBudget

func (o *Orchestrator) ResetPerRunBudget()

ResetPerRunBudget resets the per-run budget counter

func (*Orchestrator) UntrackedSpends

func (o *Orchestrator) UntrackedSpends() int

UntrackedSpends returns how many completions were paid for but charged to no budget, because the model key has no price entry.

type Provider

type Provider interface {
	Complete(prompt string, opts Options) (Response, error)
	Tokens(input string) (int, error)
	Name() string
}

Provider defines the interface for LLM providers.

Complete takes no context: cancellation cannot reach the provider's transport, so the orchestrator can only abandon a slow call, not stop it.

type Response

type Response struct {
	Text         string `json:"text"`
	TokensIn     int    `json:"tokens_in"`
	TokensOut    int    `json:"tokens_out"`
	Model        string `json:"model,omitempty"`
	FinishReason string `json:"finish_reason,omitempty"`
}

Response represents an LLM response

Directories

Path Synopsis
provider

Jump to

Keyboard shortcuts

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