Skip to content

pi_llm.models

models

Dynamic model catalog.

Functions for constructing, fetching, and comparing LLM models.

get_model(provider, model_id, **overrides)

Construct a Model from known metadata. Works offline.

Unknown models get sensible defaults (128k context, 16k max_tokens, no pricing).

Parameters:

Name Type Description Default
provider str

Provider name (e.g. "openai").

required
model_id str

Model identifier (e.g. "gpt-4o").

required
**overrides Any

Override any Model field (e.g. base_url).

{}

Returns:

Type Description
Model

A fully configured Model instance.

Example

model = get_model("openai", "gpt-4o") model.context_window 128000

fetch_models(provider='openai', api_key=None, base_url=None) async

Fetch available models from the provider's API.

Each returned model is enriched with local pricing and metadata. This is optional — get_model() works without calling this.

Parameters:

Name Type Description Default
provider str

Provider name (default "openai").

'openai'
api_key str | None

API key (falls back to OPENAI_API_KEY env var).

None
base_url str | None

Override the API base URL.

None

Returns:

Type Description
list[Model]

List of Model instances available from the provider.

calculate_cost(model, usage)

Calculate dollar cost from token usage and model pricing.

Mutates usage.cost in-place. Does nothing if the model has no pricing.

Parameters:

Name Type Description Default
model Model

Model with pricing information.

required
usage Usage

Token usage to calculate cost for.

required

supports_xhigh(model)

Check if a model supports the "xhigh" thinking level.

models_are_equal(a, b)

Check if two models are the same by comparing id and provider.