GeezerVison is showing github.com — fix(console): openai usage normalization and tier threshold config (#… · koji/opencode@e289456
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/console/app/src/routes/zen/util/handler.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1023,7 +1023,8 @@ export async function handler( | 1023 | 1023 | modelInfo.costPeak && isPeakPricing(new Date()) | 1024 | 1024 | ? modelInfo.costPeak | 1025 | 1025 | : modelInfo.cost200K && | 1026 | | - inputTokens + (cacheReadTokens ?? 0) + (cacheWrite5mTokens ?? 0) + (cacheWrite1hTokens ?? 0) > 200_000 | | 1026 | + inputTokens + (cacheReadTokens ?? 0) + (cacheWrite5mTokens ?? 0) + (cacheWrite1hTokens ?? 0) > | | 1027 | + modelInfo.cost200K.threshold | 1027 | 1028 | ? modelInfo.cost200K | 1028 | 1029 | : modelInfo.cost | 1029 | 1030 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/console/app/src/routes/zen/util/provider/openai.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,7 +51,10 @@ export const openaiHelper: ProviderHelper = ({ workspaceID }) => ({ | 51 | 51 | const cacheReadTokens = usage.input_tokens_details?.cached_tokens ?? undefined | 52 | 52 | const cacheWriteTokens = usage.input_tokens_details?.cache_write_tokens ?? undefined | 53 | 53 | return { | 54 | | - inputTokens: inputTokens - (cacheReadTokens ?? 0), | | 54 | + // OpenAI's input_tokens includes both cached_tokens and cache_write_tokens; | | 55 | + // each is billed separately at its own rate. Clamp to zero so a provider | | 56 | + // reporting overlapping detail fields cannot drive input cost negative. | | 57 | + inputTokens: Math.max(0, inputTokens - (cacheReadTokens ?? 0) - (cacheWriteTokens ?? 0)), | 55 | 58 | outputTokens, | 56 | 59 | reasoningTokens, | 57 | 60 | cacheReadTokens, | |
Collapse file Copy file name to clipboardExpand all lines: packages/console/app/test/providerUsage.test.ts +19-3Lines changed: 19 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,7 @@ describe("provider usage extraction", () => { | 26 | 26 |
| 27 | 27 | expect(providers.google.normalizeUsage(usage)).toEqual({ | 28 | 28 | inputTokens: 6, | 29 | | - outputTokens: 3, | | 29 | + outputTokens: 5, | 30 | 30 | reasoningTokens: 2, | 31 | 31 | cacheReadTokens: 4, | 32 | 32 | cacheWrite5mTokens: undefined, | @@ -42,7 +42,7 @@ describe("provider usage extraction", () => { | 42 | 42 |
| 43 | 43 | expect(providers.google.normalizeUsage(usageParser.retrieve())).toEqual({ | 44 | 44 | inputTokens: 6, | 45 | | - outputTokens: 3, | | 45 | + outputTokens: 5, | 46 | 46 | reasoningTokens: 2, | 47 | 47 | cacheReadTokens: 4, | 48 | 48 | cacheWrite5mTokens: undefined, | @@ -73,7 +73,23 @@ describe("provider usage extraction", () => { | 73 | 73 | ) | 74 | 74 |
| 75 | 75 | expect(providers.openai.normalizeUsage(usageParser.retrieve())).toEqual({ | 76 | | - inputTokens: 6, | | 76 | + inputTokens: 3, | | 77 | + outputTokens: 2, | | 78 | + reasoningTokens: undefined, | | 79 | + cacheReadTokens: 4, | | 80 | + cacheWrite5mTokens: 3, | | 81 | + cacheWrite1hTokens: undefined, | | 82 | + }) | | 83 | + }) | | 84 | + | | 85 | + test("clamps input tokens when detail fields overlap", () => { | | 86 | + const usageParser = providers.openai.createUsageParser() | | 87 | + usageParser.parse( | | 88 | + 'event: response.completed\ndata: {"response":{"usage":{"input_tokens":5,"input_tokens_details":{"cached_tokens":4,"cache_write_tokens":3},"output_tokens":2}}}', | | 89 | + ) | | 90 | + | | 91 | + expect(providers.openai.normalizeUsage(usageParser.retrieve())).toEqual({ | | 92 | + inputTokens: 0, | 77 | 93 | outputTokens: 2, | 78 | 94 | reasoningTokens: undefined, | 79 | 95 | cacheReadTokens: 4, | |
Collapse file Copy file name to clipboardExpand all lines: packages/console/core/src/model.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,11 +19,17 @@ export namespace ZenData { | 19 | 19 | cacheWrite1h: z.number().optional(), | 20 | 20 | }) | 21 | 21 |
| | 22 | + // Long-context tier. The flip threshold defaults to 200_000 for backward | | 23 | + // compatibility with existing ZEN_MODELS secrets. | | 24 | + const ModelCostTierSchema = ModelCostSchema.extend({ | | 25 | + threshold: z.number().default(200_000), | | 26 | + }) | | 27 | + | 22 | 28 | const ModelSchema = z.object({ | 23 | 29 | name: z.string(), | 24 | 30 | cost: ModelCostSchema, | 25 | 31 | costMultiplier: z.number().default(1), | 26 | | - cost200K: ModelCostSchema.optional(), | | 32 | + cost200K: ModelCostTierSchema.optional(), | 27 | 33 | costPeak: ModelCostSchema.optional(), | 28 | 34 | allowAnonymous: z.boolean().optional(), | 29 | 35 | byokProvider: z.enum(["openai", "anthropic", "google"]).optional(), | |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, test } from "bun:test" | | 2 | +import { ZenData } from "../src/model" | | 3 | + | | 4 | +const base = { | | 5 | + zenModels: { | | 6 | + "gpt-5.6-sol": { | | 7 | + name: "GPT-5.6 Sol", | | 8 | + cost: { input: 2, output: 10 }, | | 9 | + costMultiplier: 1, | | 10 | + providers: [{ id: "openai", model: "gpt-5.6-sol" }], | | 11 | + }, | | 12 | + }, | | 13 | + liteModels: {}, | | 14 | + providers: { openai: { api: "https://api.openai.com/v1", apiKey: "test" } }, | | 15 | +} | | 16 | + | | 17 | +const entry = (data: ReturnType<typeof ZenData.validate>) => { | | 18 | + const value = data.zenModels["gpt-5.6-sol"] | | 19 | + return Array.isArray(value) ? value[0] : value | | 20 | +} | | 21 | + | | 22 | +describe("ZenData cost200K threshold", () => { | | 23 | + test("defaults to 200_000 when not configured", () => { | | 24 | + const data = ZenData.validate({ | | 25 | + ...base, | | 26 | + zenModels: { | | 27 | + "gpt-5.6-sol": { | | 28 | + ...base.zenModels["gpt-5.6-sol"], | | 29 | + // The secret arrives as parsed JSON in production, so untyped here too. | | 30 | + cost200K: JSON.parse('{"input":4,"output":15}'), | | 31 | + }, | | 32 | + }, | | 33 | + }) | | 34 | + expect(entry(data).cost200K?.threshold).toBe(200_000) | | 35 | + }) | | 36 | + | | 37 | + test("accepts an explicit 272_000 threshold", () => { | | 38 | + const data = ZenData.validate({ | | 39 | + ...base, | | 40 | + zenModels: { | | 41 | + "gpt-5.6-sol": { | | 42 | + ...base.zenModels["gpt-5.6-sol"], | | 43 | + cost200K: { input: 4, output: 15, threshold: 272_000 }, | | 44 | + }, | | 45 | + }, | | 46 | + }) | | 47 | + expect(entry(data).cost200K?.threshold).toBe(272_000) | | 48 | + }) | | 49 | +}) |
|