Skip to content

Commit e0d28b5

Browse files
synthaloriansteipete
authored andcommitted
fix(anthropic): honor configured model pricing
1 parent e07d75d commit e0d28b5

2 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎extensions/anthropic/index.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Anthropic tests cover index plugin behavior.
2+
import { calculateCost, type Usage } from "openclaw/plugin-sdk/llm";
23
import type {
34
ProviderResolveDynamicModelContext,
45
ProviderRuntimeModel,
@@ -859,6 +860,70 @@ describe("anthropic provider replay hooks", () => {
859860
expect(normalized?.cost).toEqual((resolved as ProviderRuntimeModel).cost);
860861
});
861862

863+
it.each(["claude-sonnet-5", "claude-opus-5"])(
864+
"uses operator-configured %s pricing for assistant usage",
865+
async (modelId) => {
866+
const provider = await registerSingleProviderPlugin(anthropicPlugin);
867+
const configuredCost = { input: 777, output: 888, cacheRead: 999, cacheWrite: 666 };
868+
const config: NonNullable<ProviderResolveDynamicModelContext["config"]> = {
869+
models: {
870+
providers: {
871+
anthropic: {
872+
baseUrl: "https://api.anthropic.com",
873+
models: [
874+
{
875+
id: modelId,
876+
name: modelId,
877+
reasoning: true,
878+
input: ["text", "image"],
879+
cost: configuredCost,
880+
contextWindow: 1_000_000,
881+
maxTokens: 128_000,
882+
},
883+
],
884+
},
885+
},
886+
},
887+
};
888+
const discoveredModel = provider.resolveDynamicModel?.({
889+
config,
890+
provider: "anthropic",
891+
modelId,
892+
modelRegistry: createModelRegistry([]),
893+
} as ProviderResolveDynamicModelContext);
894+
expect(discoveredModel).toBeDefined();
895+
896+
const configuredModel = {
897+
...(discoveredModel as ProviderRuntimeModel),
898+
cost: configuredCost,
899+
};
900+
const resolvedModel =
901+
provider.normalizeResolvedModel?.({
902+
config,
903+
provider: "anthropic",
904+
modelId,
905+
model: configuredModel,
906+
} as never) ?? configuredModel;
907+
const usage: Usage = {
908+
input: 1_000,
909+
output: 1_000,
910+
cacheRead: 1_000,
911+
cacheWrite: 1_000,
912+
totalTokens: 4_000,
913+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
914+
};
915+
916+
calculateCost(resolvedModel as Parameters<typeof calculateCost>[0], usage);
917+
918+
expect(resolvedModel.cost).toEqual(configuredCost);
919+
expect(usage.cost.input).toBeCloseTo(0.777);
920+
expect(usage.cost.output).toBeCloseTo(0.888);
921+
expect(usage.cost.cacheRead).toBeCloseTo(0.999);
922+
expect(usage.cost.cacheWrite).toBeCloseTo(0.666);
923+
expect(usage.cost.total).toBeCloseTo(3.33);
924+
},
925+
);
926+
862927
it("resolves Claude Mythos 5 with its direct-only mandatory-adaptive contract", async () => {
863928
const provider = await registerSingleProviderPlugin(anthropicPlugin);
864929
const resolved = provider.resolveDynamicModel?.({

‎extensions/anthropic/register.runtime.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,10 +631,11 @@ function supportsAnthropicNativeMaxEffort(modelId: string): boolean {
631631
return supportsClaudeNativeMaxEffort({ id: modelId }) || isAnthropicMythosPreviewModel(modelId);
632632
}
633633

634-
function hasConfiguredModelContextOverride(
634+
function hasConfiguredModelOverride(
635635
config: ProviderNormalizeResolvedModelContext["config"],
636636
provider: string,
637637
modelId: string,
638+
override: "context" | "cost",
638639
): boolean {
639640
const providers = config?.models?.providers;
640641
if (!providers || typeof providers !== "object") {
@@ -657,8 +658,10 @@ function hasConfiguredModelContextOverride(
657658
continue;
658659
}
659660
if (
660-
(typeof model?.contextTokens === "number" && model.contextTokens > 0) ||
661-
(typeof model?.contextWindow === "number" && model.contextWindow > 0)
661+
override === "cost"
662+
? model?.cost !== undefined
663+
: (typeof model?.contextTokens === "number" && model.contextTokens > 0) ||
664+
(typeof model?.contextWindow === "number" && model.contextWindow > 0)
662665
) {
663666
return true;
664667
}
@@ -681,7 +684,7 @@ function applyAnthropicFixedContextWindow(params: {
681684
if (fixedContextWindow === undefined) {
682685
return undefined;
683686
}
684-
if (hasConfiguredModelContextOverride(params.config, params.provider, params.modelId)) {
687+
if (hasConfiguredModelOverride(params.config, params.provider, params.modelId, "context")) {
685688
return undefined;
686689
}
687690
const exactContextWindow = isAnthropicExact1MClaude5Model(params.contractModelId);
@@ -878,8 +881,10 @@ function normalizeAnthropicResolvedModel(
878881
contractModelId,
879882
model: thinkingLevelModel,
880883
}) ?? thinkingLevelModel;
884+
// Provider catalog defaults must not replace explicit operator pricing.
881885
const pricingModel =
882-
normalizeLowercaseStringOrEmpty(ctx.provider) === PROVIDER_ID
886+
normalizeLowercaseStringOrEmpty(ctx.provider) === PROVIDER_ID &&
887+
!hasConfiguredModelOverride(ctx.config, ctx.provider, ctx.modelId, "cost")
883888
? (applyAnthropicOpus5Cost({
884889
modelId: contractModelId,
885890
model: contextWindowModel,

0 commit comments

Comments
 (0)