Skip to content

Commit 5347285

Browse files
authored
improve(models): source pricing from hosted catalog (#114060)
* feat(model-catalog): serve hosted fallback pricing * refactor(config): retire client pricing bootstrap settings * refactor(gateway): delete client pricing refresh runtime * docs(models): explain hosted catalog pricing * fix(model-catalog): preserve pricing privacy and aliases * fix(model-catalog): fingerprint pricing eligibility * fix(model-catalog): harden pricing endpoint checks * fix(model-catalog): materialize source-safe pricing aliases * fix(model-catalog): keep unknown pricing fallbacks safe * fix(model-catalog): reject zero-only hosted prices * fix(model-catalog): fail closed without pricing policy metadata * refactor(utils): extract usage pricing normalization * fix(model-catalog): rebuild policy-owned pricing namespaces * test(model-catalog): type publisher cost fixtures * chore(config): regenerate schema baselines * fix(utils): keep raw pricing tiers private
1 parent 0951d0d commit 5347285

55 files changed

Lines changed: 1114 additions & 3782 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎config/max-lines-baseline.txt‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,6 @@ src/gateway/gateway-models.profiles.live.test.ts
730730
src/gateway/managed-image-attachments.test.ts
731731
src/gateway/managed-image-attachments.ts
732732
src/gateway/mcp-http.test.ts
733-
src/gateway/model-pricing-cache.test.ts
734-
src/gateway/model-pricing-cache.ts
735733
src/gateway/node-registry.test.ts
736734
src/gateway/node-registry.ts
737735
src/gateway/openai-http.test.ts
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"core": 2306,
2+
"core": 2304,
33
"channel": 3630,
44
"plugin": 3556
55
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
b5a60d7a0cfb860835eb4f38acb6421db736c76121a891311f37dede4c26e5a4 config-baseline.json
2-
e51de41a04b71b26f3ed5edd8e663982f2dee84026b7ecfab6ef75ecb2e7eb4d config-baseline.core.json
1+
8a8f87d6d3b350de300c9ce32d628fcd2f6e90b3e1dfb8f2f6ff915f56e83642 config-baseline.json
2+
2079c0b778fafecfcb2bea1a2eb6d54fbbe00f44959a0a3a0393c5c60e1f9089 config-baseline.core.json
33
af6ca0e70007113462270d46fa14ef0551e577e2fa157d2b9f6d0a93d36f96f1 config-baseline.channel.json
44
d47f7eb4268480e99ab85b6c1b5ee07f6c570ce0555e3db682ec26037014b47e config-baseline.plugin.json

‎docs/concepts/models.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ The hosted file is published from the public
231231
Its scheduled workflow refreshes from OpenClaw's shipped plugin manifests and
232232
pricing sources; every catalog content change is preserved as a public commit.
233233

234-
Run `openclaw models refresh` for an immediate check, or disable every hosted
235-
catalog request with `models.catalogRefresh.enabled: false`. A self-hosted mirror
236-
can be selected with an HTTPS `models.catalogRefresh.url` (or localhost HTTP
237-
for testing); see
234+
Run `openclaw models refresh` for an immediate metadata and pricing check, or
235+
disable every hosted catalog request with `models.catalogRefresh.enabled:
236+
false`. When disabled, pricing stays at bundled and explicitly configured
237+
values. A self-hosted mirror can be selected with an HTTPS
238+
`models.catalogRefresh.url` (or localhost HTTP for testing); see
238239
[configuration reference](/gateway/configuration-reference#models).
239240

240241
Custom providers configured under `models.providers` are written into `models.json` under the agent directory (default `~/.openclaw/agents/<agentId>/agent/models.json`). Provider-plugin catalogs are stored separately as generated plugin-owned catalog shards and load automatically. This file is merged with config by default; set `models.mode: "replace"` to use only your configured providers.

‎docs/gateway/configuration-reference.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ The `models` root also owns global model-catalog behavior.
6464
```json5
6565
{
6666
models: {
67-
// Optional. Default: true. Requires a Gateway restart when changed.
68-
pricing: { enabled: false },
6967
// Optional. Hosted catalog updates default on.
7068
catalogRefresh: {
7169
enabled: true,
@@ -81,19 +79,21 @@ The `models` root also owns global model-catalog behavior.
8179
local model servers. OpenClaw probes the configured health endpoint, starts
8280
the absolute `command` when needed, waits for readiness, then sends the model
8381
request. See [Local model services](/gateway/local-model-services).
84-
- `models.pricing.enabled`: controls the background pricing bootstrap that
85-
starts after sidecars and channels reach the Gateway ready path. When `false`,
86-
the Gateway skips OpenRouter and LiteLLM pricing-catalog fetches; configured
87-
`models.providers.*.models[].cost` values still work for local cost estimates.
8882
- `models.catalogRefresh.enabled`: controls the hosted model catalog refresh
8983
(default: `true`). Set it to `false` to prevent all remote catalog requests;
90-
only catalog data shipped in the installed release is then used.
84+
model metadata and pricing then stay at the values shipped in the installed
85+
release or declared under `models.providers.*.models[].cost`.
9186
- `models.catalogRefresh.url`: optional HTTPS mirror override (plain HTTP is
9287
accepted only for explicit localhost testing). The Gateway
9388
checks in the background at startup and every six hours. A downloaded catalog
9489
applies on the next Gateway restart; a release whose bundled catalog is newer
9590
always wins.
9691

92+
Pricing updates ship in the same hosted catalog file as model metadata. The
93+
retired `models.pricing` toggle is removed automatically by `openclaw doctor
94+
--fix`; use `models.catalogRefresh.enabled: false` when OpenClaw must avoid all
95+
hosted catalog traffic.
96+
9797
## MCP
9898

9999
OpenClaw-managed MCP server definitions live under `mcp.servers` and are

‎docs/plugins/manifest.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ See [Plugins](/tools/plugin) for the full plugin system guide, and [Capability m
147147
| `providerCatalogEntry` | No | `string` | Lightweight provider-catalog module path, relative to the plugin root, for manifest-scoped provider catalog metadata that can be loaded without activating the full plugin runtime. |
148148
| `modelSupport` | No | `object` | Manifest-owned shorthand model-family metadata used to auto-load the plugin before runtime. |
149149
| `modelCatalog` | No | `object` | Declarative model catalog metadata for providers owned by this plugin. This is the control-plane contract for future read-only listing, onboarding, model pickers, aliases, and suppression without loading plugin runtime. |
150-
| `modelPricing` | No | `object` | Provider-owned external pricing lookup policy. Use it to opt local/self-hosted providers out of remote pricing catalogs or map provider refs to OpenRouter/LiteLLM catalog ids without hardcoding provider ids in core. |
150+
| `modelPricing` | No | `object` | Provider-owned hosted-pricing publication policy. Use it to opt local/self-hosted providers out of published pricing or map provider refs to OpenRouter/LiteLLM catalog ids without hardcoding provider ids in core. |
151151
| `modelIdNormalization` | No | `object` | Provider-owned model-id alias/prefix cleanup that must run before provider runtime loads. |
152152
| `providerEndpoints` | No | `object[]` | Manifest-owned endpoint host/baseUrl metadata for provider routes that core must classify before provider runtime loads. |
153153
| `providerRequest` | No | `object` | Cheap provider-family and request-compatibility metadata used by generic request policy before provider runtime loads. |
@@ -1133,7 +1133,7 @@ OpenClaw derives `trustedDirs` for manifest presets from the plugin root and, fo
11331133

11341134
## modelPricing reference
11351135

1136-
Use `modelPricing` when a provider needs control-plane pricing behavior before runtime loads. The Gateway pricing cache reads this metadata without importing provider runtime code.
1136+
Use `modelPricing` when the hosted catalog publisher needs provider-specific pricing-key behavior. The publisher reads this metadata without importing provider runtime code.
11371137

11381138
```json
11391139
{
@@ -1156,11 +1156,11 @@ Use `modelPricing` when a provider needs control-plane pricing behavior before r
11561156

11571157
Provider fields:
11581158

1159-
| Field | Type | What it means |
1160-
| ------------ | ----------------- | -------------------------------------------------------------------------------------------------- |
1161-
| `external` | `boolean` | Set `false` for local/self-hosted providers that should never fetch OpenRouter or LiteLLM pricing. |
1162-
| `openRouter` | `false \| object` | OpenRouter pricing lookup mapping. `false` disables OpenRouter lookup for this provider. |
1163-
| `liteLLM` | `false \| object` | LiteLLM pricing lookup mapping. `false` disables LiteLLM lookup for this provider. |
1159+
| Field | Type | What it means |
1160+
| ------------ | ----------------- | --------------------------------------------------------------------------------------------- |
1161+
| `external` | `boolean` | Set `false` for local/self-hosted providers that should never use published external pricing. |
1162+
| `openRouter` | `false \| object` | OpenRouter publication-key mapping. `false` disables OpenRouter matching for this provider. |
1163+
| `liteLLM` | `false \| object` | LiteLLM publication-key mapping. `false` disables LiteLLM matching for this provider. |
11641164

11651165
Source fields:
11661166

‎docs/reference/token-use.md‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,10 @@ auth: non-API-key providers such as `aws-sdk` can show estimated cost when
183183
their configured model entry includes local pricing and the provider
184184
returns usage metadata.
185185

186-
After sidecars and channels reach the Gateway ready path, OpenClaw starts an
187-
optional background pricing bootstrap for configured model refs that do not
188-
already have local pricing. That bootstrap fetches remote OpenRouter and
189-
LiteLLM pricing catalogs. Set `models.pricing.enabled: false` to skip those
190-
catalog fetches on offline or restricted networks; explicit
186+
Pricing updates ship in the hosted model catalog alongside model metadata.
187+
OpenClaw does not fetch OpenRouter or LiteLLM directly. Set
188+
`models.catalogRefresh.enabled: false` to disable hosted catalog traffic on
189+
offline or restricted networks; bundled pricing and explicit
191190
`models.providers.*.models[].cost` entries still drive local cost estimates.
192191

193192
## Cache TTL and pruning impact

‎packages/model-catalog-core/src/remote-catalog-bundle.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ const validBundle = {
2525
],
2626
},
2727
},
28+
pricing: {
29+
"openai/gpt-external": { input: 2.5, output: 10, cacheRead: 1.25 },
30+
},
2831
} as const;
2932

3033
describe("remote model catalog bundle", () => {
@@ -44,6 +47,11 @@ describe("remote model catalog bundle", () => {
4447
expect(anthropic.models[0]).not.toHaveProperty("baseUrl");
4548
expect(anthropic.models[0]).not.toHaveProperty("headers");
4649
expect(anthropic.models[0]?.compat).toEqual({ nested: {} });
50+
expect(parsed.pricing?.["openai/gpt-external"]).toEqual({
51+
input: 2.5,
52+
output: 10,
53+
cacheRead: 1.25,
54+
});
4755
});
4856

4957
it("rejects unsupported versions, invalid timestamps, and malformed providers", () => {
@@ -64,5 +72,17 @@ describe("remote model catalog bundle", () => {
6472
providers: { anthropic: { models: [{ id: " duplicate " }, { id: "duplicate" }] } },
6573
}),
6674
).toThrow("duplicate model id: duplicate");
75+
expect(() =>
76+
parseRemoteModelCatalogBundle({
77+
...validBundle,
78+
pricing: { "openai/bad": { input: -1, output: 2 } },
79+
}),
80+
).toThrow();
81+
expect(() =>
82+
parseRemoteModelCatalogBundle({
83+
...validBundle,
84+
pricing: { "openai/bad": { input: 1, output: 2, baseUrl: "https://bad.test" } },
85+
}),
86+
).toThrow();
6787
});
6888
});

‎packages/model-catalog-core/src/remote-catalog-bundle.ts‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,41 @@ import type { ModelCatalogProvider } from "./model-catalog-types.js";
55
export const REMOTE_CATALOG_MAX_FUTURE_SKEW_MS = 24 * 60 * 60_000;
66

77
const stringMapSchema = z.record(z.string(), z.string());
8+
const pricingTierSchema = z
9+
.object({
10+
input: z.number().finite().nonnegative(),
11+
output: z.number().finite().nonnegative(),
12+
cacheRead: z.number().finite().nonnegative(),
13+
cacheWrite: z.number().finite().nonnegative(),
14+
range: z.union([
15+
z.tuple([z.number().finite().nonnegative()]),
16+
z.tuple([z.number().finite().nonnegative(), z.number().finite().nonnegative()]),
17+
]),
18+
})
19+
.strict();
20+
821
const costSchema = z
922
.object({
1023
input: z.number().finite().nonnegative().optional(),
1124
output: z.number().finite().nonnegative().optional(),
1225
cacheRead: z.number().finite().nonnegative().optional(),
1326
cacheWrite: z.number().finite().nonnegative().optional(),
14-
tieredPricing: z
15-
.array(
16-
z.object({
17-
input: z.number().finite().nonnegative(),
18-
output: z.number().finite().nonnegative(),
19-
cacheRead: z.number().finite().nonnegative(),
20-
cacheWrite: z.number().finite().nonnegative(),
21-
range: z.union([
22-
z.tuple([z.number().finite().nonnegative()]),
23-
z.tuple([z.number().finite().nonnegative(), z.number().finite().nonnegative()]),
24-
]),
25-
}),
26-
)
27-
.optional(),
27+
tieredPricing: z.array(pricingTierSchema).optional(),
2828
})
2929
.strict();
3030

31+
const hostedPricingSchema = z
32+
.object({
33+
input: z.number().finite().nonnegative(),
34+
output: z.number().finite().nonnegative(),
35+
cacheRead: z.number().finite().nonnegative().optional(),
36+
cacheWrite: z.number().finite().nonnegative().optional(),
37+
tieredPricing: z.array(pricingTierSchema).optional(),
38+
})
39+
.strict();
40+
41+
export type RemoteModelCatalogPricing = z.infer<typeof hostedPricingSchema>;
42+
3143
const modelSchema = z.object({
3244
id: z.string().trim().min(1),
3345
name: z.string().optional(),
@@ -89,6 +101,7 @@ export const remoteModelCatalogBundleSchema = z
89101
minVersion: z.string().trim().min(1).optional(),
90102
sourceCommit: z.string().trim().min(1),
91103
providers: z.record(z.string().trim().min(1), remoteModelCatalogProviderSchema),
104+
pricing: z.record(z.string().trim().min(1), hostedPricingSchema).optional(),
92105
})
93106
.strict();
94107

‎scripts/publish-model-catalog.d.mts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ export type ModelCatalogBundleSummary = {
2525
providers: number;
2626
models: number;
2727
costModels: number;
28+
pricingEntries: number;
29+
};
30+
31+
export type PublishedModelPricing = {
32+
input: number;
33+
output: number;
34+
cacheRead?: number;
35+
cacheWrite?: number;
36+
tieredPricing?: Array<{
37+
input: number;
38+
output: number;
39+
cacheRead: number;
40+
cacheWrite: number;
41+
range: [number] | [number, number];
42+
}>;
2843
};
2944

3045
export type PublishedModelCatalogBundle = {
@@ -39,6 +54,7 @@ export type PublishedModelCatalogBundle = {
3954
[key: string]: unknown;
4055
}
4156
>;
57+
pricing?: Record<string, PublishedModelPricing>;
4258
};
4359

4460
export function parsePublishModelCatalogArgs(args: string[]): PublishModelCatalogArgs;
@@ -59,5 +75,6 @@ export function enrichModelCatalogPricing(options: {
5975
bundle: PublishedModelCatalogBundle;
6076
manifests: ModelCatalogManifestInput[];
6177
fetchImpl?: typeof fetch;
62-
}): Promise<number>;
78+
validateBundle?: (bundle: unknown) => PublishedModelCatalogBundle;
79+
}): Promise<{ modelsEnriched: number; pricingEntries: number }>;
6380
export function serializeModelCatalogBundle(bundle: PublishedModelCatalogBundle): string;

0 commit comments

Comments
 (0)