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
3 changes: 3 additions & 0 deletions apps/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"test": "TZ=UTC vitest",
"typecheck": "tsgo --noEmit"
},
"optionalDependencies": {
"better-sqlite3": "catalog:runtime"
},
"devDependencies": {
"@ccusage/internal": "workspace:*",
"@ccusage/terminal": "workspace:*",
Expand Down
48 changes: 47 additions & 1 deletion apps/opencode/src/cost-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ const MODEL_ALIASES: Record<string, string> = {
'gemini-3-pro-high': 'gemini-3-pro-preview',
};

function normalizeClaudeModelName(modelName: string): string {
return modelName.replace(/^claude-(opus|sonnet|haiku)-(\d+)\.(\d+)$/, 'claude-$1-$2-$3');
}

function resolveModelName(modelName: string): string {
return MODEL_ALIASES[modelName] ?? modelName;
const normalizedModelName = normalizeClaudeModelName(modelName);
return MODEL_ALIASES[normalizedModelName] ?? normalizedModelName;
}

/**
Expand Down Expand Up @@ -40,3 +45,44 @@ export async function calculateCostForEntry(

return Result.unwrap(result, 0);
}

if (import.meta.vitest != null) {
describe('calculateCostForEntry', () => {
it('normalizes dotted Claude model names before pricing lookup', async () => {
let lookedUpModel: string | undefined;
const fetcher = {
calculateCostFromTokens: async (
_tokens: {
input_tokens: number;
output_tokens: number;
cache_creation_input_tokens?: number;
cache_read_input_tokens?: number;
},
modelName?: string,
) => {
lookedUpModel = modelName;
return Result.succeed(1.25);
},
} as unknown as LiteLLMPricingFetcher;

const cost = await calculateCostForEntry(
{
timestamp: new Date('2026-03-10T00:00:00.000Z'),
sessionID: 'ses_test',
usage: {
inputTokens: 100,
outputTokens: 200,
cacheCreationInputTokens: 0,
cacheReadInputTokens: 0,
},
model: 'claude-opus-4.5',
costUSD: null,
},
fetcher,
);

expect(cost).toBe(1.25);
expect(lookedUpModel).toBe('claude-opus-4-5');
});
});
}
Loading