LLM API token costs in SQL, offline. llm_cost(model, input_tokens, output_tokens) over a dated, source-cited price table of 146 models from 10 providers
Compatibility Matrix
This table covers all DuckDB releases that have not reached end-of-life, including LTS releases, and shows the 2 most recent patch releases of each. Older patch releases are not listed, but the extension may be available for them as well.
| Platform | 1.4.4 | 1.4.5 | 1.5.5 | 1.5.6 | 2.0.0 |
|---|---|---|---|---|---|
| Linux AMD64 | |||||
| Linux ARM64 | |||||
| macOS AMD64 | |||||
| macOS ARM64 | |||||
| Windows AMD64 | |||||
| Windows ARM64 | |||||
| WebAssembly |
Installing and Loading
INSTALL llm_prices FROM community;
LOAD llm_prices;
Example
-- One request: 1,200 input and 350 output tokens on gpt-4o, in USD
SELECT llm_cost('gpt-4o', 1200, 350);
-- With 18,000 input tokens read from the prompt cache
SELECT llm_cost('claude-sonnet-4-5', 2000, 400, 18000);
-- Spend per model from a usage log
SELECT llm_model_id(model) AS model, count(*) AS requests,
sum(llm_cost(model, input_tokens, output_tokens, cached_tokens)) AS usd
FROM 'usage/*.parquet'
GROUP BY ALL
ORDER BY usd DESC;
-- The price table, USD per 1M tokens
SELECT provider, model, input, output, cache_read, checked
FROM llm_prices()
WHERE status = 'listed'
ORDER BY output;
About llm_prices
Prices LLM API usage inside DuckDB. The extension bundles the llm-prices table: one row per model for Anthropic, OpenAI, Google, DeepSeek, Mistral, xAI, Cohere, Groq, Together AI and Fireworks AI, each read from the provider's pricing page and dated. Prices are compiled in, so nothing is fetched at query time, no key is needed, and it works in DuckDB-Wasm.
| Function | Returns |
|---|---|
llm_cost(model, input_tokens, output_tokens, cached_tokens := 0) |
USD cost of one request as DOUBLE, NULL for a model the table does not price |
llm_model_id(model, provider := '') |
the table's model id for a raw id, for joins |
llm_prices() |
one row per model: every price per 1M tokens, aliases, status, source URL and check date |
llm_prices_version() |
the date the bundled table was checked |
input_tokens are uncached input tokens (for OpenAI usage, prompt_tokens minus
prompt_tokens_details.cached_tokens) and cached_tokens are prompt-cache reads.
llm_cost also takes cache_write_5m_tokens, cache_write_1h_tokens, batch := true
for Batch API prices, and provider := 'groq' to pick one provider's row. Long-context
prices apply automatically above a model's threshold. Model ids match in any case and
with provider prefixes (anthropic/, openrouter/), Bedrock and Vertex ids, dated
snapshots and -latest aliases.
It pairs with extensions that call models from SQL (ai, open_prompt, llm) and with
usage exports from Langfuse, Helicone, gateways or OpenTelemetry gen_ai spans.
New llm-prices releases ship as version bumps of this extension.
Added Functions
| function_name | function_type | description | comment | examples |
|---|---|---|---|---|
| llm_cost | scalar | USD cost of one LLM API request from its token counts, priced offline from the bundled llm-prices table. input_tokens are uncached input tokens (for OpenAI usage: prompt_tokens minus cached_tokens); cached_tokens are input tokens read from the prompt cache. Long-context prices apply when the prompt is over the model's threshold. Accepts provider prefixes (anthropic/, openrouter/), Bedrock and Vertex ids, dated snapshots and aliases. Returns NULL for a model the table does not price. | NULL | [SELECT llm_cost('gpt-4o', 1200, 350), SELECT llm_cost('claude-sonnet-4-5', 2000, 400, 18000), SELECT model, sum(llm_cost(model, input_tokens, output_tokens)) AS usd FROM requests GROUP BY model, SELECT llm_cost('openai/gpt-oss-120b', 1000, 1000, provider := 'groq')] |
| llm_model_id | scalar | The model id of the llm_prices() row that a provider-prefixed, dated, Bedrock, Vertex or aliased id resolves to, for joining usage logs to llm_prices(). NULL when the table has no such model. | NULL | [SELECT llm_model_id('us.anthropic.claude-sonnet-4-5-20250929-v1:0')] |
| llm_prices | table | Every model in the bundled llm-prices table, one row each: provider, model id, aliases, status, and its prices in USD per 1M tokens (input, output, cache read, cache writes per lifetime, batch, long-context tier), with the provider pricing page it was read from and the date it was checked. | NULL | [FROM llm_prices(), SELECT provider, arg_min(model, output) AS model, min(output) AS usd_per_1m FROM llm_prices() WHERE status = 'listed' GROUP BY provider] |
| llm_prices_version | scalar | The date (YYYY-MM-DD) the bundled llm-prices table was checked against the providers' pricing pages. | NULL | [SELECT llm_prices_version()] |
Overloaded Functions
This extension does not add any function overloads.
Added Types
This extension does not add any types.
Added Settings
| name | description | input_type | scope | aliases | typed_value |
|---|---|---|---|---|---|
| temp_directory | Set the directory to which temporary files are written. | VARCHAR | GLOBAL | [] | post.db.tmp |