LLM Price Checker

The Smarter LLM Price Checker: an LLMClient that keeps the LLM prices up to date, every week.

Every LLMPrices row that the price catalog or the checker itself set, and that has a source_url, is checked against that page: the provider’s pricing page. The platform fetches each page once, converts it to Markdown, and prompts the staff-only LLMClient smarter_llm_price_checker of the Smarter account with the page and the models that it prices. The LLMClient answers with each model’s standard price per million input and output tokens, as JSON.

Its answer is checked again here, whatever its system prompt says (check_price()):

  • a price must be of a model that it was asked about;

  • its input and output prices must both be dollar amounts that are written on the page, so that it can’t invent a number;

  • the new blended price may differ from the current one by a factor of at most LLM_PRICE_CHECKER_MAX_CHANGE, up or down. A larger change is more likely a misreading, e.g. of a batch rate or of another model’s row, than a new price.

A price that fails is refused, logged, and kept as it was. A price that a person set (HUMAN), e.g. in the Django admin, is never checked or changed. Celery Beat runs smarter.apps.account.tasks.check_llm_prices() every week, while the enable_llm_price_checker switch is on.

The LLMClient is prompted in-process, as its owner, with smarter.apps.llmclient.prompting.prompt_in_process(): its system prompt, guardrails and budget apply, as they would to a person’s prompt.

class smarter.apps.account.llm_price_checker.LLMPriceCheckerAnswer(**data)[source]

Bases: BaseModel

The checker’s answer.

model_config = {'extra': 'ignore'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

prices: list[LLMPriceCheckerPrice]
class smarter.apps.account.llm_price_checker.LLMPriceCheckerPrice(**data)[source]

Bases: BaseModel

The checker’s price of one model.

Extra fields are ignored: an LLM adds some.

evidence: str
found: bool
input: Decimal | None
model: str
model_config = {'extra': 'ignore'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

output: Decimal | None
provider: str
class smarter.apps.account.llm_price_checker.LLMPriceCheckerRequest(**data)[source]

Bases: LLMPriceCheckerModel

The checker’s prompt: a pricing page, and the models that it prices.

current(provider, model)[source]

Return a model of the request.

Parameters:
  • provider (str) – The provider’s name.

  • model (str) – The model’s name.

Returns:

The model, or None if the request doesn’t have it.

Return type:

LLMPriceCheckerCurrentPrice | None

model_config = {'extra': 'forbid'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

models: list[LLMPriceCheckerCurrentPrice]
page: str
url: str
class smarter.apps.account.llm_price_checker.LLMPriceCheckerResult(**data)[source]

Bases: BaseModel

What the checker did with one pricing page.

changed: int
checked: int
error: str
model_config = {'extra': 'forbid'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

refused: list[str]
url: str
smarter.apps.account.llm_price_checker.apply_price(price)[source]

Apply a checked price to every charge type’s row of its model that the checker keeps.

A row whose price is unchanged is only marked as checked, and keeps its source.

Parameters:

price (LLMPriceCheckerPrice) – The price, which check_price() accepted.

Returns:

True if the price changed.

Return type:

bool

smarter.apps.account.llm_price_checker.check_change(provider, model, new)[source]

Check that a model’s new price is within a factor of its current price, up or down.

Parameters:
  • provider (str) – The provider’s name.

  • model (str) – The model’s name.

  • new (Decimal) – The new blended price, in USD per million tokens.

Returns:

Why the change is refused, or None if it is fine, or the model has no price yet.

Return type:

str | None

smarter.apps.account.llm_price_checker.check_llm_prices()[source]

Check every price that the checker keeps, page by page, while the enable_llm_price_checker switch is on.

Returns:

What was done with each pricing page, or nothing if the switch is off or the checker doesn’t exist on this platform.

Return type:

list[LLMPriceCheckerResult]

smarter.apps.account.llm_price_checker.check_page(llmclient, url, models)[source]

Ask the checker for the prices of a page’s models, and apply those that pass the checks.

Parameters:
  • llmclient (LLMClient) – The checker.

  • url (str) – The pricing page.

  • models (list[LLMPriceCheckerCurrentPrice]) – Its models, with their current prices.

Returns:

What was checked, changed and refused.

Return type:

LLMPriceCheckerResult

smarter.apps.account.llm_price_checker.check_price(price, request, amounts)[source]

Check a price that the checker read, against its request and its page.

Parameters:
Returns:

Why the price is refused, or None if it is fine.

Return type:

str | None

smarter.apps.account.llm_price_checker.checked_prices()[source]

Return the models whose prices the checker keeps up to date, by the page that publishes them.

Returns:

For each pricing page, its models, each once, whatever its charge types.

Return type:

dict[str, list[LLMPriceCheckerCurrentPrice]]

smarter.apps.account.llm_price_checker.dollar_amounts(page)[source]

Return the dollar amounts that are written on a page.

Parameters:

page (str) – The page’s text.

Returns:

Each amount, e.g. Decimal("0.05") for $0.05. $3 and $3.00 are equal.

Return type:

set[Decimal]

smarter.apps.account.llm_price_checker.page_markdown(url)[source]

Fetch a pricing page, and return it as Markdown, whose tables keep their rows and columns.

Parameters:

url (str) – The page’s url.

Returns:

The page’s main content, at most LLM_PRICE_CHECKER_MAX_PAGE_CHARACTERS characters.

Return type:

str

Raises:
smarter.apps.account.llm_price_checker.parse_answer(text)[source]

Parse the checker’s answer.

Parameters:

text (str | None) – The answer, which should be one JSON object, perhaps in a code fence.

Returns:

The prices.

Return type:

LLMPriceCheckerAnswer

Raises:

SmarterValueError – If it isn’t a valid answer.