modelinfo

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package modelinfo resolves a model's context window (max input tokens) DYNAMICALLY, so context-guru's triggers can scale with the model rather than hard-coding thresholds. The window is obtained without maintaining our own model list: the primary source is LiteLLM's community-maintained model_prices_and_context_window.json (fetched once and cached); an optional gateway /model/info probe is tried first for deployments that expose it; a tiny embedded table is the last-resort fallback.

Every lookup fails OPEN: an unknown model returns ok=false and callers fall back to absolute thresholds. Nothing here ever blocks a request — fetches are cached, single-flighted, and time-bounded, and any error just leaves the window unknown.

Index

Constants

View Source
const LiteLLMPricesURL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"

LiteLLMPricesURL is the community-maintained map of model -> {max_input_tokens,…}. Overridable (air-gapped mirrors) via NewLiteLLM.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain

type Chain []Resolver

Chain tries each resolver in order; the first ok wins.

func (Chain) Price added in v0.1.1

func (c Chain) Price(ctx context.Context, model string) (Price, bool)

Price tries each element that can price a model; the first ok wins. Elements that only resolve windows are skipped, so a Chain{LiteLLM, Static} prices from LiteLLM and reports unknown when it has not loaded.

func (Chain) Window

func (c Chain) Window(ctx context.Context, model string) (int, bool)

type LiteLLM

type LiteLLM struct {
	URL    string
	Client *http.Client
	TTL    time.Duration
	// contains filtered or unexported fields
}

LiteLLM fetches and caches the LiteLLM prices map, serving per-model windows.

func NewLiteLLM

func NewLiteLLM(url string, client *http.Client, ttl time.Duration) *LiteLLM

NewLiteLLM builds a resolver. url="" uses LiteLLMPricesURL; ttl<=0 uses 6h.

func (*LiteLLM) Price added in v0.1.1

func (l *LiteLLM) Price(ctx context.Context, model string) (Price, bool)

Price returns the model's per-token rates from the cached LiteLLM map, matched the same way Window matches (full id, then bare tail, then a contains scan).

func (*LiteLLM) Window

func (l *LiteLLM) Window(ctx context.Context, model string) (int, bool)

Window returns the model's context window from the cached LiteLLM map.

type Price added in v0.1.1

type Price struct {
	Input      float64 `json:"input"`       // fresh (uncached) input per token
	Output     float64 `json:"output"`      // completion per token
	CacheRead  float64 `json:"cache_read"`  // cache-hit input per token
	CacheWrite float64 `json:"cache_write"` // cache-creation input per token
}

Price is a model's per-token USD rates, in the four tiers a prompt-caching provider bills. Zero rates mean "unknown" — a caller must treat a Price it did not get an ok=true for as "no pricing", never as free.

func (Price) Cost added in v0.1.1

func (p Price) Cost(fresh, cacheRead, cacheWrite, output int64) float64

Cost prices one request's four token tiers in USD.

func (Price) Zero added in v0.1.1

func (p Price) Zero() bool

Zero reports whether no rate at all is known (so a cost figure would be a lie).

type Pricer added in v0.1.1

type Pricer interface {
	Price(ctx context.Context, model string) (Price, bool)
}

Pricer resolves a model's per-token rates. ok=false means "unknown"; callers must then report cost as unavailable rather than zero.

type Resolver

type Resolver interface {
	Window(ctx context.Context, model string) (tokens int, ok bool)
}

Resolver returns a model's context window in tokens. ok=false means "unknown".

type Static

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

Static is a tiny last-resort table keyed by substring. Used only when a dynamic source is unavailable. Deliberately small — it is a floor, not a registry.

func DefaultStatic

func DefaultStatic() Static

DefaultStatic covers the common families with conservative windows.

func (Static) Window

func (s Static) Window(_ context.Context, model string) (int, bool)

type Table added in v0.1.1

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

A Table is an OPERATOR-SUPPLIED price list, consulted before the public LiteLLM map. It exists because the public map prices the public API, and a gateway does not have to charge that: IBM's ete-litellm bills claude-sonnet-5 at $1.52/MTok in where anthropic.com bills $3.00, so every dollar figure this dashboard produced for that gateway was ~2x too high. A price list is also the only way to price a model the public map has never heard of — a preview deployment, an internal route name, or a server-resolved TIER like Bob's `premium`, which is not a model id at all and left every Bob request's cost reading "unknown".

Rates in the file are USD per MILLION tokens, because that is the unit every vendor's price page and every gateway's admin UI uses; storing per-token floats in a hand-edited file invites a factor-of-a-thousand typo that nothing would catch. They are converted once, here.

Nothing in a Table is a secret: these are list prices. It is a plain file, and a missing or malformed one is a startup error rather than a silent fallback — a price list that failed to load looks exactly like "this model is free".

func LoadTable added in v0.1.1

func LoadTable(path string) (*Table, error)

LoadTable reads a price list. An empty path returns a nil *Table, which prices nothing and is safe to put in a Chain.

func ParseTable added in v0.1.1

func ParseTable(b []byte) (*Table, error)

ParseTable builds a Table from the YAML document LoadTable reads.

func (*Table) Len added in v0.1.1

func (t *Table) Len() int

Len is how many entries were loaded, for the startup log line.

func (*Table) Price added in v0.1.1

func (t *Table) Price(_ context.Context, model string) (Price, bool)

Price returns the operator's rate for a model. ok=false means "not in the file", and the next source in the Chain answers.

func (*Table) Window added in v0.1.1

func (t *Table) Window(_ context.Context, model string) (int, bool)

Window returns an operator-supplied context window, for models the public map does not list. Entries without one return ok=false so the public map still wins.

Jump to

Keyboard shortcuts

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