Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions packages/opencode/src/provider/pollinations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function isChatLLM(record: Record<string, unknown>) {

function toEntry(record: Record<string, unknown>, id: string): ModelEntry {
const pricing = isRecord(record.pricing) ? record.pricing : {}
const pricingUnits = isRecord(record.pricing_units) ? record.pricing_units : {}
const capabilities = strings(record.capabilities)
const community = record.community === true
const free = record.paid_only !== true && isFree(record)
Expand All @@ -140,10 +141,10 @@ function toEntry(record: Record<string, unknown>, id: string): ModelEntry {
contextLength: positive(record.context_length),
outputLimit: positive(topProvider.max_completion_tokens),
cost: {
input: perMillion(pricing.promptTextTokens),
output: perMillion(pricing.completionTextTokens),
cacheRead: perMillion(pricing.promptCachedTokens),
cacheWrite: perMillion(pricing.promptCacheWriteTokens),
input: perMillion(maxTokenPrice(pricing, pricingUnits, "prompt", ["promptCachedTokens", "promptCacheWriteTokens"])),
output: perMillion(maxTokenPrice(pricing, pricingUnits, "completion")),
cacheRead: perMillionTokenPrice(pricing, pricingUnits, "promptCachedTokens"),
cacheWrite: perMillionTokenPrice(pricing, pricingUnits, "promptCacheWriteTokens"),
},
}
}
Expand Down Expand Up @@ -192,6 +193,37 @@ function price(value: unknown) {
return typeof number === "number" && Number.isFinite(number) && number >= 0 ? number : undefined
}

/**
* Model.cost has one rate per input/output token. Use the highest published
* token-denominated rate for each side so reasoning and multimodal token usage
* is not priced at a lower text-only rate. Custom billed units (for example
* megapixels) and per-request pricing_adjustments cannot be represented by
* that schema and are therefore outside this conservative estimate.
*/
function maxTokenPrice(
pricing: Record<string, unknown>,
units: Record<string, unknown>,
prefix: "prompt" | "completion",
excluded: string[] = [],
) {
return Math.max(
0,
...Object.entries(pricing).flatMap(([field, value]) => {
if (!field.startsWith(prefix) || !field.endsWith("Tokens") || excluded.includes(field)) return []
return isTokenPriced(field, units) ? [price(value) ?? 0] : []
}),
)
}

function perMillionTokenPrice(pricing: Record<string, unknown>, units: Record<string, unknown>, field: string) {
return isTokenPriced(field, units) ? perMillion(pricing[field]) : 0
}

function isTokenPriced(field: string, units: Record<string, unknown>) {
const unit = isRecord(units[field]) && typeof units[field].unit === "string" ? units[field].unit : undefined
return unit === undefined || /token/i.test(unit)
}

function perMillion(value: unknown) {
return (price(value) ?? 0) * PER_MILLION
}
Expand Down
24 changes: 24 additions & 0 deletions packages/opencode/test/provider/pollinations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,30 @@ describe("pollinations model list", () => {
expect(entry?.name).toBe("Open Chat (community)")
})

test("uses the highest token rate per side and ignores non-token billed units", () => {
const [entry] = parseModelList({
data: [
{
...gpt,
pricing: {
...gpt.pricing,
promptImageTokens: "0.0000004",
completionReasoningTokens: "0.000002",
completionImageTokens: "0.00000099",
},
pricing_units: {
completionImageTokens: { unit: "megapixel", quantity: 1_000_000 },
},
},
],
})
if (!entry) throw new Error("entry missing")
const model = toModel(entry)
expect(model.cost.input).toBeCloseTo(0.4, 10)
expect(model.cost.output).toBeCloseTo(2, 10)
expect(model.cost.cache.read).toBeCloseTo(0.015, 10)
})

test("converts per-token Pollen prices into per-million cost figures", () => {
const [entry] = parseModelList({ data: [gpt] })
if (!entry) throw new Error("entry missing")
Expand Down
Loading