Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fix-models-dev-reasoning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ff-ai': patch
---

Fix models.dev price lookup for model IDs with `-reasoning` suffix by retrying without the suffix on 404
23 changes: 19 additions & 4 deletions packages/ai/src/pricing/model-price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ class ModelsDevData extends Schema.Class<ModelsDevData>('ff-ai/ModelsDevData')({
}
}

const fetchModelsDev = (model: ModelInput) =>
const modelsDevUrl = (provider: string, modelId: string) =>
`https://raw.githubusercontent.com/sst/models.dev/refs/heads/dev/providers/${provider}/models/${modelId}.toml`;

const fetchModelsDevByUrl = (url: string) =>
Effect.gen(function* () {
const url = `https://raw.githubusercontent.com/sst/models.dev/refs/heads/dev/providers\
/${ModelInput.getProvider(model)}/\
models/${ModelInput.getModelId(model)}.toml`;
const response = yield* HttpClient.get(url);
const text = yield* response.text;
if (response.status === 404) {
Expand All @@ -94,6 +94,21 @@ models/${ModelInput.getModelId(model)}.toml`;
return yield* Schema.decodeUnknown(ModelsDevData)(parsed);
});

/** models.dev treats reasoning as default (no suffix), so retry without it on 404 */
const fetchModelsDev = (model: ModelInput) =>
Effect.gen(function* () {
const provider = ModelInput.getProvider(model);
const modelId = ModelInput.getModelId(model);
const result = yield* fetchModelsDevByUrl(modelsDevUrl(provider, modelId));
if (result != null) return result;

if (modelId.endsWith('-reasoning')) {
const stripped = modelId.replace(/-reasoning$/, '');
return yield* fetchModelsDevByUrl(modelsDevUrl(provider, stripped));
}
return null;
});

const calcCost = (token: number, pricePerMillion: PricePerMillion) =>
Usd.make((token * pricePerMillion) / 1_000_000);

Expand Down
Loading