modelsdev

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package modelsdev provides an adapter for loading model pricing data from models.dev (https://models.dev/api.json).

The adapter supports loading from a local file, fetching from the URL, or refreshing the data on-demand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLookup

func NewLookup(opts ...Option) (llmproxy.CostLookup, error)

NewLookup is a convenience function that creates an adapter and returns just the CostLookup function.

Example:

lookup, _ := modelsdev.NewLookup(modelsdev.WithFile("models.json"))
billing := interceptors.NewBilling(lookup, onResult)

Types

type Adapter

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

Adapter loads and caches model pricing data from models.dev.

func LoadFromFile

func LoadFromFile(path string) (*Adapter, error)

LoadFromFile is a convenience function that creates an adapter and loads data from a file in one step.

func LoadFromURL

func LoadFromURL() (*Adapter, error)

LoadFromURL is a convenience function that creates an adapter and fetches data from the URL in one step.

func New

func New(opts ...Option) *Adapter

New creates a new models.dev adapter. Data is not loaded until Load() or Lookup() is called.

Example - fetch from URL:

adapter := modelsdev.New()
adapter.Load(context.Background()) // optional, loads on first lookup

Example - load from file:

adapter := modelsdev.New(modelsdev.WithFile("models.json"))

Example - custom URL, TTL, and markup:

adapter := modelsdev.New(
    modelsdev.WithURL("https://internal.example.com/models.json"),
    modelsdev.WithTTL(30*time.Minute),
    modelsdev.WithMarkup(1.2), // 20% markup
)

func (*Adapter) GetCostLookup

func (a *Adapter) GetCostLookup() llmproxy.CostLookup

GetCostLookup returns a CostLookup function for use with interceptors.

Example:

adapter := modelsdev.New(modelsdev.WithFile("models.json"))
billing := interceptors.NewBilling(adapter.GetCostLookup(), func(r llmproxy.BillingResult) {
    log.Printf("Cost: $%.6f", r.TotalCost)
})

func (*Adapter) GetModel

func (a *Adapter) GetModel(provider string, model string) *Model

GetModel returns model information for a provider/model. Returns nil if not found.

func (*Adapter) GetProvider

func (a *Adapter) GetProvider(provider string) *Provider

GetProvider returns provider information. Returns nil if not found.

func (*Adapter) ListModels

func (a *Adapter) ListModels(provider string) []string

ListModels returns all model IDs for a provider.

func (*Adapter) ListProviders

func (a *Adapter) ListProviders() []string

ListProviders returns all provider IDs.

func (*Adapter) Load

func (a *Adapter) Load(ctx context.Context) error

Load fetches or reads the models.dev JSON data. If a file path is set, it reads from the file. Otherwise, it fetches from the URL.

func (*Adapter) Lookup

func (a *Adapter) Lookup(provider string, model string) (llmproxy.CostInfo, bool)

Lookup returns pricing for a provider/model. It implements llmproxy.CostLookup interface.

If data is not loaded or TTL expired, it loads automatically. Note: Auto-load uses context.Background() - call Load() explicitly to control the context for initial load.

func (*Adapter) Refresh

func (a *Adapter) Refresh(ctx context.Context) error

Refresh forces a reload of the data, bypassing TTL.

type Cost

type Cost struct {
	Input       float64 `json:"input,omitempty"`
	Output      float64 `json:"output,omitempty"`
	Reasoning   float64 `json:"reasoning,omitempty"`
	CacheRead   float64 `json:"cache_read,omitempty"`
	CacheWrite  float64 `json:"cache_write,omitempty"`
	InputAudio  float64 `json:"input_audio,omitempty"`
	OutputAudio float64 `json:"output_audio,omitempty"`
}

Cost represents pricing in USD per 1M tokens.

type Limit

type Limit struct {
	Context int `json:"context,omitempty"`
	Input   int `json:"input,omitempty"`
	Output  int `json:"output,omitempty"`
}

Limit represents token limits.

type Modalities

type Modalities struct {
	Input  []string `json:"input,omitempty"`
	Output []string `json:"output,omitempty"`
}

Modalities represents supported input/output types.

type Model

type Model struct {
	ID               string      `json:"id"`
	Name             string      `json:"name"`
	Family           string      `json:"family,omitempty"`
	Attachment       bool        `json:"attachment"`
	Reasoning        bool        `json:"reasoning"`
	ToolCall         bool        `json:"tool_call"`
	Temperature      bool        `json:"temperature,omitempty"`
	Knowledge        string      `json:"knowledge,omitempty"`
	ReleaseDate      string      `json:"release_date"`
	LastUpdated      string      `json:"last_updated"`
	OpenWeights      bool        `json:"open_weights"`
	StructuredOutput bool        `json:"structured_output,omitempty"`
	Status           string      `json:"status,omitempty"`
	Cost             *Cost       `json:"cost,omitempty"`
	Limit            *Limit      `json:"limit,omitempty"`
	Modalities       *Modalities `json:"modalities,omitempty"`
}

Model represents a model in models.dev.

type ModelsDevJSON

type ModelsDevJSON map[string]Provider

ModelsDevJSON is the root structure of models.dev/api.json.

type Option

type Option func(*Adapter)

Option configures the Adapter.

func WithFile

func WithFile(path string) Option

WithFile sets a local file path to load the JSON from.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient sets a custom HTTP client for fetching.

func WithMarkup

func WithMarkup(multiplier float64) Option

WithMarkup sets a markup multiplier applied to all prices. For example:

  • 1.0 = no markup (default)
  • 1.2 = 20% markup (price * 1.2)
  • 1.5 = 50% markup (price * 1.5)

This is useful for reselling API access with a profit margin.

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets the cache TTL for auto-refresh when fetching from URL. Default is 1 hour. Set to 0 to disable auto-refresh.

func WithURL

func WithURL(url string) Option

WithURL sets a custom URL for fetching the models.dev JSON.

type Provider

type Provider struct {
	ID     string           `json:"id"`
	Name   string           `json:"name"`
	Env    []string         `json:"env"`
	NPM    string           `json:"npm"`
	Doc    string           `json:"doc"`
	API    string           `json:"api,omitempty"`
	Models map[string]Model `json:"models"`
}

Provider represents a provider in models.dev.

Jump to

Keyboard shortcuts

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