modelinfo

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 14 Imported by: ? 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.

README

modelinfo

What a model can do and what it costs: context window, output limit, capabilities (vision, reasoning, tools) and per-token prices, read from LiteLLM's public price table. No dependencies.

go get github.com/7ar-chat/harness/modelinfo
table, err := modelinfo.Load(ctx, modelinfo.Options{CacheDir: dir}) // fetches the table, keeps a cached copy
info, ok := table.Lookup("gpt-4o-mini", "openai")                  // the second argument is a provider hint
fmt.Println(info.ContextWindow, info.MaxOutput, info.Vision)

usd, priced := modelinfo.Cost(info, modelinfo.Usage{In: 12_000, Out: 800, Cached: 9_000, Prompt: 12_000})
  • Matching is conservative. An id is matched exactly, then with a provider prefix, then as the same name under another provider or a dated release of the same model. It never matches across unrelated models, so a wrong price is rarer than a missing one. ok is false when nothing matches.
  • Cost is honest about unknowns. priced is false when the table has no price for the model: zero then means "unknown", not "free". Cached tokens are billed at the cache-read price when the table has one. Models with a higher price above a prompt size (Tier) use it when Usage.Prompt crosses the threshold.
  • Offline use. Options.File reads a local table, Offline uses the cache however old it is, and Override is your own file in the same schema whose models win. LoadAsync returns a table at once and fills it in the background.
  • DefaultContextWindow (128000) is what a caller can fall back on for an unknown model.

The table is data, not authority: check prices that matter against the provider.

License

MIT. See LICENSE.

Documentation

Overview

Package modelinfo describes models: their context window, output limit, capabilities and per-token prices, read from LiteLLM's public price table (model_prices_and_context_window.json). It imports nothing from the harness; callers pass the provider they configured as a hint to Lookup.

The table is data, not authority: ids are matched conservatively (exact, then provider-prefixed, then the same name under another provider or a dated release of the same model) and never across unrelated models.

Index

Constants

View Source
const (
	// DefaultURL is LiteLLM's public price table.
	DefaultURL = "https://raw.githubusercontent.com/BerriAI/litellm/refs/heads/main/model_prices_and_context_window.json"
	// DefaultTTL is how long the cached copy is used without asking again.
	DefaultTTL = 24 * time.Hour
)

Defaults of Options.

View Source
const DefaultContextWindow = 128000

DefaultContextWindow is the context window assumed for a model whose window is not known (not in the configured windows nor in the price table).

Variables

This section is empty.

Functions

func Cost

func Cost(i Info, u Usage) (usd float64, ok bool)

Cost is the price of u in dollars. ok is false when the model has no price, in which case the amount is 0. Cached tokens are billed at the cache-read price when known, else at the input price.

func Parse

func Parse(data []byte) (map[string]Info, error)

Parse reads the LiteLLM table: a JSON object of model id -> specification. The "sample_spec" documentation entry, entries that are not objects and non-chat models (embeddings, images…) are skipped; fields of the wrong type are ignored.

Types

type Info

type Info struct {
	ID       string
	Provider string
	// ContextWindow is the input window in tokens, MaxOutput the largest answer.
	ContextWindow, MaxOutput int
	// Prices are in dollars per token. Priced is false when the table has no
	// price at all for the model (a zero price is then "unknown", not "free").
	InputPerToken, OutputPerToken, CacheReadPerToken, CacheWritePerToken float64
	Priced                                                               bool
	// Tier holds the higher prices some models charge for a request whose
	// prompt exceeds Tier.Above tokens (nil: one price).
	Tier                     *Tier
	Vision, Reasoning, Tools bool
}

Info is what is known about one model. A zero field is unknown.

type Options

type Options struct {
	// CacheDir holds the downloaded table (model_prices.json) and its ETag; ""
	// keeps nothing on disk (the table is fetched again at every start).
	CacheDir string
	// URL replaces DefaultURL.
	URL string
	// File, when set, is a local table read instead of the URL (and never cached).
	File string
	// Override is a user file in the same schema; its models win over the table's.
	Override string
	// TTL is the age under which the cache is used as is (default DefaultTTL).
	TTL time.Duration
	// HTTPClient performs the request (default http.DefaultClient).
	HTTPClient *http.Client
	// Offline makes no request: the cache, however old, is all there is.
	Offline bool
}

Options configure Load.

type Table

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

Table is a set of models. It is safe for concurrent use; the zero value and a nil *Table are empty.

func Load

func Load(ctx context.Context, o Options) (*Table, error)

Load reads the table: the cache when fresh, else the URL (a conditional GET that sends nothing but the request itself), else the stale cache. With none of them the table is empty and the error says why; callers may ignore it, a table without data answers every Lookup with false.

func LoadAsync

func LoadAsync(ctx context.Context, o Options) *Table

LoadAsync returns the table at once and fills it in the background; Ready reports when it is done. Starting never waits for the network.

func (*Table) Len

func (t *Table) Len() int

Len is the number of models.

func (*Table) Lookup

func (t *Table) Lookup(id, providerHint string) (Info, bool)

Lookup finds the model id as served by the provider providerHint (the configured provider type or name; "" when unknown): an exact entry, then "<hint>/<id>", then the same name under another prefix, then the same model as a dated release or an undated alias. A miss is (Info{}, false).

func (*Table) Ready

func (t *Table) Ready() bool

Ready reports that loading has finished (successfully or not): until then the table may simply not have arrived yet.

func (*Table) Set

func (t *Table) Set(entries map[string]Info)

Set adds entries, replacing the models of the same id.

type Tier

type Tier struct {
	Above                                int
	Input, Output, CacheRead, CacheWrite float64
}

Tier is the price list of requests above a prompt size ("_above_200k_tokens"). A zero price falls back to the base price.

type Usage

type Usage struct {
	In, Out, Cached int
	// Prompt is the size of the largest single prompt, which selects the price
	// tier (0: the base prices).
	Prompt int
}

Usage is the token usage a cost is computed from. In counts every input token including the cached ones; reasoning tokens are part of Out.

Jump to

Keyboard shortcuts

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