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
13 changes: 13 additions & 0 deletions packages/opencode/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,18 @@ const layer = Layer.effect(
if (model.id && model.id !== modelID) return modelID
return existingModel?.name ?? modelID
})
const longContext = model.cost?.context_over_200k
const longContextCost = longContext
? {
input: longContext.input,
output: longContext.output,
cache: {
read: longContext.cache_read ?? 0,
write: longContext.cache_write ?? 0,
},
}
: existingModel?.cost.experimentalOver200K
const optionalLongContext = longContextCost ? { experimentalOver200K: longContextCost } : {}
const parsedModel: Model = {
id: ModelV2.ID.make(modelID),
api: {
Expand Down Expand Up @@ -1493,6 +1505,7 @@ const layer = Layer.effect(
read: model?.cost?.cache_read ?? existingModel?.cost?.cache.read ?? 0,
write: model?.cost?.cache_write ?? existingModel?.cost?.cache.write ?? 0,
},
...optionalLongContext,
},
options: mergeDeep(existingModel?.options ?? {}, model.options ?? {}),
limit: {
Expand Down
70 changes: 70 additions & 0 deletions packages/opencode/test/provider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { Config } from "@/config/config"
import { Env } from "../../src/env"
import { Plugin } from "../../src/plugin/index"
import { Provider } from "@/provider/provider"
import { Session as SessionNs } from "@/session/session"
import { Usage } from "@opencode-ai/llm"

import { RuntimeFlags } from "@/effect/runtime-flags"
import { Filesystem } from "@/util/filesystem"
Expand Down Expand Up @@ -978,6 +980,74 @@ it.instance(
},
)

it.instance(
"uses configured Grok long-context pricing for local session estimates",
Effect.gen(function* () {
yield* set("XAI_API_KEY", "test-api-key")
const providers = yield* list
const model = providers[ProviderV2.ID.make("xai")].models["grok-4.5"]
expect(model.cost.experimentalOver200K).toEqual({
input: 4,
output: 12,
cache: { read: 1, write: 0 },
})
const estimate = (inputTokens: number) =>
SessionNs.getUsage({
model,
usage: new Usage({ inputTokens, outputTokens: 0, totalTokens: inputTokens }),
}).cost
expect(estimate(199_000)).toBe(0.398)
expect(estimate(200_000)).toBe(0.4)
expect(estimate(201_000)).toBe(0.804)
}),
{
config: {
provider: {
xai: {
models: {
"grok-4.5": {
cost: {
input: 2,
output: 6,
cache_read: 0.5,
context_over_200k: { input: 4, output: 12, cache_read: 1 },
},
},
},
},
},
},
},
)

it.instance(
"preserves Grok long-context pricing when config omits it",
Effect.gen(function* () {
yield* set("XAI_API_KEY", "test-api-key")
const providers = yield* list
const model = providers[ProviderV2.ID.make("xai")].models["grok-4.5"]
expect(model.cost.experimentalOver200K).toEqual({
input: 4,
output: 12,
cache: { read: 1, write: 0 },
})
const result = SessionNs.getUsage({
model,
usage: new Usage({ inputTokens: 201_000, outputTokens: 0, totalTokens: 201_000 }),
})
expect(result.cost).toBe(0.804)
}),
{
config: {
provider: {
xai: {
models: { "grok-4.5": {} },
},
},
},
},
)

it.instance(
"completely new provider not in database can be configured",
Effect.gen(function* () {
Expand Down
Loading