Skip to content

Commit d19fb91

Browse files
authored
fix(opencode): price long-context steps at the catalog tier, including config-named models (#34)
Config-named models keep catalog tiers through the provider merge; a config context_over_200k rides as a 200K context tier above the catalog tiers below it; the catalog converter sets experimentalOver200K only when the model has no tiers, so the 200K-272K band on gpt-5.x prices at base as OpenRouter bills it. Tracks anomalyco#44224.
1 parent 765cb6f commit d19fb91

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,10 @@ function cost(c: ModelsDev.Model["cost"]): Model["cost"] {
11771177
tier: item.tier,
11781178
}))
11791179
}
1180-
if (c?.context_over_200k) {
1180+
// NOTE (Gabe, 2026-09-02): models.dev spells a model's single long-context tier as
1181+
// context_over_200k even when its tiers put the threshold elsewhere (272K for gpt-5.x), and
1182+
// Session.getUsage falls back to this field below the tier size, so tiers own the threshold.
1183+
if (c?.context_over_200k && !result.tiers?.length) {
11811184
result.experimentalOver200K = {
11821185
cache: {
11831186
read: c.context_over_200k.cache_read ?? 0,
@@ -1422,6 +1425,9 @@ const layer = Layer.effect(
14221425
if (model.id && model.id !== modelID) return modelID
14231426
return existingModel?.name ?? modelID
14241427
})
1428+
// NOTE (Gabe, 2026-09-02): a config context_over_200k prices everything above 200K, but
1429+
// Session.getUsage consults tiers first, so it rides as a 200K tier above the catalog's lower ones.
1430+
const over200k = model.cost?.context_over_200k ? cost(model.cost).experimentalOver200K : undefined
14251431
const parsedModel: Model = {
14261432
id: ModelV2.ID.make(modelID),
14271433
api: {
@@ -1468,6 +1474,14 @@ const layer = Layer.effect(
14681474
read: model?.cost?.cache_read ?? existingModel?.cost?.cache.read ?? 0,
14691475
write: model?.cost?.cache_write ?? existingModel?.cost?.cache.write ?? 0,
14701476
},
1477+
// NOTE (Gabe, 2026-09-02): config cannot express tiers, so an entry that only names a model keeps the catalog's.
1478+
tiers: over200k
1479+
? [
1480+
...(existingModel?.cost?.tiers ?? []).filter((item) => item.tier.size < 200_000),
1481+
{ ...over200k, tier: { type: "context", size: 200_000 } },
1482+
]
1483+
: existingModel?.cost?.tiers,
1484+
experimentalOver200K: over200k ?? existingModel?.cost?.experimentalOver200K,
14711485
},
14721486
options: mergeDeep(existingModel?.options ?? {}, model.options ?? {}),
14731487
limit: {

‎packages/opencode/test/provider/provider.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { Config } from "@/config/config"
1515
import { Env } from "../../src/env"
1616
import { Plugin } from "../../src/plugin/index"
1717
import { Provider } from "@/provider/provider"
18+
import { Session } from "@/session/session"
19+
import { Usage } from "@opencode-ai/llm"
1820

1921
import { RuntimeFlags } from "@/effect/runtime-flags"
2022
import { Filesystem } from "@/util/filesystem"
@@ -935,6 +937,95 @@ it.instance(
935937
},
936938
)
937939

940+
it.instance(
941+
"config entry that only names a model keeps the database over-200k pricing",
942+
Effect.gen(function* () {
943+
yield* set("OPENROUTER_API_KEY", "test-api-key")
944+
const providers = yield* list
945+
const model = providers[ProviderV2.ID.make("openrouter")].models["openai/gpt-5.4"]
946+
expect(model.cost.input).toBe(2.5)
947+
expect(model.cost.tiers).toEqual([
948+
{ input: 5, output: 22.5, cache: { read: 0.5, write: 0 }, tier: { type: "context", size: 272_000 } },
949+
])
950+
// NOTE (Gabe, 2026-09-02): OpenRouter's override for gpt-5.x starts at 272K, so the legacy
951+
// over-200k field must not price the 200K-272K band at the tier rate.
952+
expect(model.cost.experimentalOver200K).toBeUndefined()
953+
const price = (inputTokens: number) =>
954+
Session.getUsage({ model, usage: new Usage({ inputTokens, outputTokens: 0, totalTokens: inputTokens }) }).cost
955+
expect(price(250_000)).toBeCloseTo(0.625, 6)
956+
expect(price(300_000)).toBeCloseTo(1.5, 6)
957+
}),
958+
{
959+
config: {
960+
provider: {
961+
openrouter: {
962+
models: { "openai/gpt-5.4": { options: {} } },
963+
},
964+
},
965+
},
966+
},
967+
)
968+
969+
it.instance(
970+
"config over-200k cost overrides the database over-200k pricing",
971+
Effect.gen(function* () {
972+
yield* set("OPENROUTER_API_KEY", "test-api-key")
973+
const providers = yield* list
974+
const model = providers[ProviderV2.ID.make("openrouter")].models["openai/gpt-5.4"]
975+
expect(model.cost.experimentalOver200K).toEqual({
976+
input: 7,
977+
output: 30,
978+
cache: { read: 0.7, write: 0 },
979+
})
980+
// NOTE (Gabe, 2026-09-02): the catalog's 272K tier would otherwise outrank the configured
981+
// over-200k price in Session.getUsage for every context above 272K.
982+
expect(model.cost.tiers).toEqual([
983+
{ input: 7, output: 30, cache: { read: 0.7, write: 0 }, tier: { type: "context", size: 200_000 } },
984+
])
985+
const priced = Session.getUsage({
986+
model,
987+
usage: new Usage({ inputTokens: 300_000, outputTokens: 0, totalTokens: 300_000 }),
988+
})
989+
expect(priced.cost).toBeCloseTo(2.1, 6)
990+
}),
991+
{
992+
config: {
993+
provider: {
994+
openrouter: {
995+
models: { "openai/gpt-5.4": { cost: { input: 3.5, output: 15, context_over_200k: { input: 7, output: 30, cache_read: 0.7 } } } },
996+
},
997+
},
998+
},
999+
},
1000+
)
1001+
1002+
it.instance(
1003+
"config over-200k cost keeps the database tiers below 200k",
1004+
Effect.gen(function* () {
1005+
yield* set("OPENROUTER_API_KEY", "test-api-key")
1006+
const providers = yield* list
1007+
const model = providers[ProviderV2.ID.make("openrouter")].models["x-ai/grok-4"]
1008+
expect(model.cost.tiers).toEqual([
1009+
{ input: 6, output: 30, cache: { read: 0, write: 0 }, tier: { type: "context", size: 128_000 } },
1010+
{ input: 9, output: 45, cache: { read: 0, write: 0 }, tier: { type: "context", size: 200_000 } },
1011+
])
1012+
const price = (inputTokens: number) =>
1013+
Session.getUsage({ model, usage: new Usage({ inputTokens, outputTokens: 0, totalTokens: inputTokens }) }).cost
1014+
expect(price(100_000)).toBeCloseTo(0.3, 6)
1015+
expect(price(150_000)).toBeCloseTo(0.9, 6)
1016+
expect(price(300_000)).toBeCloseTo(2.7, 6)
1017+
}),
1018+
{
1019+
config: {
1020+
provider: {
1021+
openrouter: {
1022+
models: { "x-ai/grok-4": { cost: { input: 3, output: 15, context_over_200k: { input: 9, output: 45 } } } },
1023+
},
1024+
},
1025+
},
1026+
},
1027+
)
1028+
9381029
it.instance(
9391030
"completely new provider not in database can be configured",
9401031
Effect.gen(function* () {

0 commit comments

Comments
 (0)