fix(ai): price anthropic 1h cache writes at 2x input (#5738)

anthropic 1h cache writes were billed at the 5m rate; split the bucket and price the 1h portion correctly
This commit is contained in:
Bucky
2026-06-15 15:21:14 +08:00
committed by GitHub
parent a851968170
commit 0be5bb6c96
4 changed files with 93 additions and 1 deletions

View File

@@ -37,10 +37,13 @@ export function getModels<TProvider extends KnownProvider>(
}
export function calculateCost<TApi extends Api>(model: Model<TApi>, usage: Usage): Usage["cost"] {
// Anthropic charges 2x base input for 1h cache writes.
const longWrite = usage.cacheWrite1h ?? 0;
const shortWrite = usage.cacheWrite - longWrite;
usage.cost.input = (model.cost.input / 1000000) * usage.input;
usage.cost.output = (model.cost.output / 1000000) * usage.output;
usage.cost.cacheRead = (model.cost.cacheRead / 1000000) * usage.cacheRead;
usage.cost.cacheWrite = (model.cost.cacheWrite / 1000000) * usage.cacheWrite;
usage.cost.cacheWrite = (model.cost.cacheWrite * shortWrite + model.cost.input * 2 * longWrite) / 1000000;
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
return usage.cost;
}