pi-provider-litellm 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # pi-provider-litellm
2
2
 
3
- LiteLLM proxy native Provider extension for [Pi](https://pi.dev). Pi 0.81.0+ is required.
3
+ LiteLLM proxy native Provider extension for [Pi](https://pi.dev). Pi 0.83.0+ is required.
4
4
 
5
5
  Discovers models from self-hosted LiteLLM proxies and registers them under Pi providers. The default provider is `litellm`; optional aliases can register additional LiteLLM providers with separate credentials. Supports `/login litellm`, LiteLLM MCP tools, LiteLLM Skills Gateway prompt injection, and Google ADC token auth. Tries `/model/info` first (admin endpoint with rich metadata), falls back to `/v1/models` (OpenAI-compatible) on 401/403/404, then tries `/health` plus per-endpoint `/model/info` for older LiteLLM proxies.
6
6
 
@@ -23,11 +23,13 @@ pi -e npm:pi-provider-litellm
23
23
 
24
24
  ```bash
25
25
  git clone https://github.com/balcsida/pi-provider-litellm.git ~/.pi/agent/extensions/pi-provider-litellm
26
- cd ~/.pi/agent/extensions/pi-provider-litellm
27
- npm ci
28
- npm run clean && npm run build
29
26
  ```
30
27
 
28
+ Pi loads the TypeScript source entrypoint declared in `package.json` `pi.extensions`, so a source install needs no
29
+ build step and no `node_modules`. Install dependencies only to run the test suite or the local checks.
30
+
31
+ The previously published 2.2.0 package was also importable as `pi-provider-litellm` from JavaScript or TypeScript. The source-only package no longer exposes that library import; it is supported only as a Pi extension.
32
+
31
33
  </details>
32
34
 
33
35
  ## Configure
@@ -44,19 +46,25 @@ To configure an API key, run `/login`, choose `Sign in with an API key`, then ch
44
46
 
45
47
  You'll be prompted for the base URL and API key. Credentials are persisted to `~/.pi/agent/auth.json`.
46
48
 
49
+ If a proxy URL is already known — from `providers.<name>.baseUrl`, `LITELLM_BASE_URL`, or a previous login — pi offers it as the first option instead of asking you to retype it. Pick `Enter a different URL…` to point at another proxy.
50
+
47
51
  #### Enterprise SSO login
48
52
 
49
- If your LiteLLM proxy requires SSO/OAuth authentication (enterprise deployments), you can authenticate through LiteLLM's CLI SSO flow:
53
+ If your LiteLLM proxy supports SSO/OAuth authentication, Pi selects its supported browser login flow automatically:
50
54
 
51
55
  1. Run `/login litellm` inside pi and select `Sign in with LiteLLM SSO`
52
- 2. Enter the proxy URL
53
- 3. Pi displays a verification code and opens the LiteLLM SSO login URL automatically
54
- 4. Authenticate in the browser and confirm the displayed code
55
- 5. If your account belongs to multiple teams, select the team for this session in Pi
56
+ 2. Confirm the offered proxy URL, or enter one if this is the first login
57
+ 3. Complete sign-in in the browser Pi opens, then return to Pi
56
58
 
57
- Pi polls the proxy until authentication completes and stores the returned short-lived CLI token. Newer proxies advertise the token lifetime; for older proxies, Pi uses `LITELLM_CLI_JWT_EXPIRATION_HOURS` or LiteLLM's 24-hour default. On LiteLLM versions that gate CLI SSO, start the proxy with `EXPERIMENTAL_UI_LOGIN=True`.
59
+ Pi first checks `/.well-known/litellm-cli-auth`. A proxy exposing the supported CLI-auth contract uses authorization code + PKCE (`S256`) with a temporary `127.0.0.1` callback; the browser must run on the same machine as Pi. Advertised endpoints must stay on the proxy's origin, and discovery, registration, and token requests do not follow HTTP redirects. Invalid discovery stops login.
58
60
 
59
- Older proxies without `/sso/cli/start` fall back to the legacy browser flow: copy the token from the LiteLLM UI, paste it into Pi, and optionally exchange it for a virtual key. Pi reads JWT expiry claims and prompts you to run `/login litellm` again when re-authentication is required.
61
+ Pi stores PKCE access and refresh tokens in `~/.pi/agent/auth.json` with file mode `0600`, refreshes automatically, and saves each rotated token pair. Temporary network failures or HTTP 429/5xx responses allow reuse of the existing access token only until its exact expiry. Rejected or revoked refresh credentials require `/login litellm` again.
62
+
63
+ If discovery returns 404, Pi uses `/sso/cli/start`: confirm the verification code in the browser, then select a team in Pi if prompted. Pi polls for the short-lived CLI token and uses its advertised lifetime, falling back to `LITELLM_CLI_JWT_EXPIRATION_HOURS` or LiteLLM's 24-hour default on older proxies. Only LiteLLM versions that gate this older CLI SSO flow need `EXPERIMENTAL_UI_LOGIN=True` on the proxy.
64
+
65
+ If `/sso/cli/start` returns 404 or 405, copy the token from the LiteLLM UI and paste it into Pi, with an optional virtual-key exchange. Pi reads JWT expiry claims and prompts for login when re-authentication is required.
66
+
67
+ Tokens are stored in Pi's local file, not an OS keychain. `/logout litellm` deletes the local credential only; logging out or signing in again does not revoke the previous session on the proxy because Pi has no provider revocation hook.
60
68
 
61
69
  ### Option B — environment variables
62
70
 
@@ -65,7 +73,7 @@ export LITELLM_BASE_URL="https://litellm.your-domain.com"
65
73
  export LITELLM_API_KEY="sk-..."
66
74
  ```
67
75
 
68
- Stored pi credentials for `litellm` take precedence over `LITELLM_API_KEY`; the environment key is used when no saved credential exists. `LITELLM_BASE_URL` is used when no saved login base URL exists.
76
+ Stored pi credentials for `litellm` take precedence over `LITELLM_API_KEY`; the environment key is used when no saved credential exists. `LITELLM_BASE_URL` is used when no saved login base URL exists. Chat Completions and Responses models use the proxy root plus `/v1`; native Messages uses the proxy root directly.
69
77
 
70
78
  ### Multiple LiteLLM provider aliases
71
79
 
@@ -110,7 +118,7 @@ Provider fields:
110
118
 
111
119
  | Field | Default | Effect |
112
120
  |---|---|---|
113
- | `baseUrl` | `LITELLM_BASE_URL` for `litellm`; required for aliases | LiteLLM proxy URL, with or without `/v1` |
121
+ | `baseUrl` | `LITELLM_BASE_URL` for `litellm`; required for aliases | LiteLLM proxy URL, with or without `/v1`. Must be a full `http`/`https` URL; a provider with no resolvable base URL exposes no models (see [Model host enforcement](#model-host-enforcement)) |
114
122
  | `apiKey` | `LITELLM_API_KEY_HELPER`/`LITELLM_API_KEY` for `litellm`; required for aliases | Pi config value for this provider's key. Use `$ENV_VAR`, `${ENV_VAR}`, `!command`, or a literal key. Escape a literal `$` as `$$`. |
115
123
  | `headers` | `$LITELLM_HEADERS` for `litellm`; unset for aliases | JSON string env reference or inline object of request headers |
116
124
  | `displayName` | provider name | Label shown in Pi UI |
@@ -146,18 +154,28 @@ Treat the configured LiteLLM proxy as trusted: Skills can add instructions to th
146
154
  /model
147
155
  ```
148
156
 
157
+ Every request routed through a configured LiteLLM provider includes Pi's canonical session ID in the
158
+ `x-litellm-session-id` header. This groups Chat Completions, Responses, and native Messages requests in LiteLLM without
159
+ adding transport-specific fields to request bodies.
160
+
161
+ ## Model transport
162
+
163
+ A `/model/info` route group uses native Anthropic `/v1/messages` only when every deployment explicitly reports Chat mode, identifies a Claude backend through an Anthropic-, Bedrock-, or Vertex-capable adapter, and every declared backend identity resolves the same Anthropic compatibility policy. A `claude-<name>-<major>-<minor>` backend id resolves through its `claude-<name>-<major>` entry when the exact id is absent; an unknown generation stays on Chat Completions. When `supported_endpoints` is present, it must include `/v1/messages`; an explicit endpoint list without it keeps the group on Chat Completions. An unresolved routing or base-model identity denies native Messages rather than being discarded from the unanimity check. Mixed-generation Claude groups can use Messages when their serializer policies match, with reasoning levels restricted to their common supported set. Groups with incompatible policies, mixed families, or unknown backend evidence remain on Chat Completions; unanimous explicit Responses mode takes precedence. Routes discovered through `/health` never select native Messages: Messages candidates are downgraded to Chat Completions with reasoning controls closed, while correlated Responses detail remains on Responses. Evidence-free `/v1/models` and wholly health-only `/health` groups take transport and presentation metadata from the bounded Pi catalog lookup: `openai-responses` when the catalog says so, otherwise Chat Completions. They expose no speculative reasoning selector and never select native Messages. Catalog identity such as Amazon Bedrock remains separate from the selected wire protocol.
164
+
165
+ Native Messages authenticates with `x-api-key`; every transport carries the `x-litellm-session-id` header described above.
166
+
149
167
  ## Optional environment variables
150
168
 
151
169
  | Variable | Default | Effect |
152
170
  |---|---|---|
153
- | `LITELLM_API_KEY_HELPER` | unset | Command that prints a fresh LiteLLM bearer token. Takes precedence over `LITELLM_API_KEY`. Registered as a `!command` provider key; Pi re-runs it on every request (the per-request auth path is uncached), so rotating/short-lived tokens stay fresh. |
171
+ | `LITELLM_API_KEY_HELPER` | unset | Command that prints a fresh LiteLLM bearer token. Takes precedence over `LITELLM_API_KEY`. The extension runs it while resolving request auth, and Pi's per-request auth path is uncached, so rotating/short-lived tokens stay fresh. |
154
172
  | `LITELLM_HEADERS` | unset | JSON object of extra headers sent to LiteLLM provider, discovery, MCP, and Skills Gateway requests. Provider aliases can use it with `"headers": "$LITELLM_HEADERS"`. |
155
173
  | `LITELLM_GCLOUD_TOKEN_AUTH` | unset | If set to a non-empty value other than `0`, use Google Application Default Credentials as the LiteLLM bearer token source. This takes precedence over `LITELLM_API_KEY_HELPER` and `LITELLM_API_KEY` when no stored `/login litellm` credential exists. |
156
174
  | `GOOGLE_APPLICATION_CREDENTIALS` | Google default ADC path | Optional path to an ADC JSON file used by `LITELLM_GCLOUD_TOKEN_AUTH`. If unset, the extension checks the default gcloud ADC locations. |
157
- | `LITELLM_OFFLINE` | unset | If `1`, disable all model and MCP discovery, including post-login discovery; use cached models only |
175
+ | `LITELLM_OFFLINE` | unset | If `1`, disable all model and MCP discovery, including post-login discovery; use cached models only when their stored canonical proxy root exactly matches the active credential root, including any path prefix. URL-standard host casing and default ports are canonicalized, but paths remain case-sensitive. |
158
176
  | `LITELLM_DISCOVERY_TIMEOUT_MS` | `5000` | Background and explicit discovery fetch timeout in ms; `0` disables automatic discovery |
159
177
  | `LITELLM_CLI_JWT_EXPIRATION_HOURS` | `24` | CLI SSO token lifetime fallback for older proxies whose poll response omits `expires_in`; mirror a non-default proxy setting locally |
160
- | `LITELLM_VERBOSE_DISCOVERY` | unset | If `1`, enable progress messages during model and MCP discovery (login, refresh, startup); discovery is silent by default |
178
+ | `LITELLM_VERBOSE_DISCOVERY` | unset | If `1`, enable progress messages during model and MCP discovery (login, refresh, startup), including MCP prepared/registered/dropped counts. Progress messages are off by default; MCP safety diagnostics (see below) are always reported regardless of this setting |
161
179
 
162
180
  Only use a trusted `LITELLM_API_KEY_HELPER` or `!command`. Prefer an absolute executable path, keep secrets out of command arguments, and print only the token to stdout without logging it to stderr.
163
181
 
@@ -173,7 +191,7 @@ export LITELLM_BASE_URL="https://litellm.your-domain.com"
173
191
  export LITELLM_GCLOUD_TOKEN_AUTH=1
174
192
  ```
175
193
 
176
- Only `authorized_user` ADC files are supported. Service account JSON files are rejected with a warning. Tokens are cached in memory for 50 minutes and the registered provider key is a Pi `!command`, so request-time auth resolves a fresh token when Pi sends model requests.
194
+ Only `authorized_user` ADC files are supported. Service account JSON files are rejected with a warning. An `authorized_user` file with a missing, empty, or whitespace-only `client_id`, `client_secret`, or `refresh_token` is rejected before any token exchange. Tokens are refreshed in-process when Pi resolves request auth and cached in memory for 50 minutes.
177
195
 
178
196
  ## LiteLLM MCP tools
179
197
 
@@ -182,7 +200,53 @@ If your LiteLLM proxy exposes MCP REST endpoints, this extension discovers tools
182
200
  - `GET /mcp-rest/tools/list`
183
201
  - `POST /mcp-rest/tools/call`
184
202
 
185
- Each discovered tool is registered as a native Pi tool named `mcp_<server>_<tool>`, with simple JSON Schema parameters mapped to Pi/TypeBox parameters. Complex schemas fall back to a single `args` object. MCP discovery runs after Pi refreshes LiteLLM models or after `/login litellm`; extension activation never waits for it. MCP tools run in Pi's parallel tool mode and retry transient failures once.
203
+ An MCP access denial pauses discovery until a successful `/login litellm` (SSO or API key). The pause survives restarts in `~/.pi/agent/litellm-mcp-pauses/`, scoped to the proxy, login, and headers with salted credential fingerprints. A new API key or header configuration has its own scope. Each explicit login creates a fresh opaque `litellmMcpSession` identifier in Pi's stored credential, even when the key is unchanged; automatic OAuth refresh preserves that identifier and its pause. Older Pi processes cannot pause the new login. Pause files contain no credentials or proxy error text. Model discovery and chat continue normally. Grant MCP access on the proxy before signing in again. An explicit `litellm.mcp.enabled: false` setting remains disabled after login.
204
+
205
+ Each discovered tool is registered as a native Pi tool named `mcp_<server>_<tool>_<hash>`, at most 64 characters, using only `[a-z0-9_]`. The trailing 10-character hash is derived from the tool's identity (`server_id`, `server_name`, `name`) and is always present, so a name depends only on that tool and never on which other tools happen to be in the same catalog — adding or removing a sibling never renames a survivor. When the readable prefix would overflow 64 characters it is truncated from the right, so a long server name can leave little or none of the tool name visible; the hash is what distinguishes such tools. Tools from different servers therefore never overwrite each other, and exact duplicate identities are registered once.
206
+
207
+ MCP discovery runs after Pi refreshes LiteLLM models or after `/login litellm`; extension activation never waits for it. Discovery accepts at most a 5 MiB response and registers at most 512 tools. The response must be a JSON array of tools or an object with a `tools` array; an `unexpected_error` or unknown non-null `error` in the object envelope, an object without `tools`, and every other shape are reported as discovery failures rather than as empty catalogs. Failure reports use extension-owned text and never include LiteLLM's proxy-supplied tag or `message`. A `partial_failure` envelope keeps and registers the returned tools while reporting one bounded generated diagnostic. Each tool is isolated during normalization, so one bad tool never hides valid siblings. A tool is accepted only when its schema has a root of exactly `"type": "object"` (a bare `properties` object with no `type`, or a type union such as `["object","null"]`, is refused), plain-object `properties`, at most 16 levels of JSON nesting (about eight nested schema objects when each level uses `properties`), and a serialized size of at most 64 KiB. Descriptions are limited to 4 KiB, and tool labels and result metadata to 256 bytes, each with a truncation marker.
208
+
209
+ ### Which schema a tool ends up with
210
+
211
+ A tool registers with one of two parameter shapes.
212
+
213
+ - **The extension-owned `args` envelope** — a single object-valued `args` property, required at execution. Used when the proxy supplied no schema at all (absent or `{}`), and used as a safe substitute when the supplied schema could not be proven safe (below). Nothing in this shape originates from the proxy.
214
+ - **The supplied schema, passed through unchanged.** Pi validates arguments against it before the call and may first coerce supported primitive values: omitting a property listed in `required` is rejected, while a value of the wrong type is rejected only when it cannot be coerced to the declared type. Unknown extra properties are accepted by default, but an `additionalProperties` schema can validate and coerce them, and constraints such as `additionalProperties: false` or `unevaluatedProperties: false` can reject them. Passing through is not the same as skipping validation. A schema of exactly `{"type": "object"}` with no `properties` therefore accepts any arguments at the top level rather than nesting them under `args`. Real schema properties named `args`, `properties`, or `required` remain intact.
215
+
216
+ An `inputSchema` that is present but is not a JSON object (a string, number, or array) is a malformed tool and is dropped as `invalid-schema`. It is not treated as schemaless, so a proxy emitting junk cannot obtain the permissive envelope.
217
+
218
+ ### Regexes and references in supplied schemas
219
+
220
+ `pattern`, and the keys of `patternProperties`, are compiled into a backtracking regular expression and executed with no time limit, so a proxy-supplied expression can block Pi for minutes on a single tool call. A `$ref` resolves an arbitrary JSON pointer, which means an expression can be parked under any key at all, including data-only positions such as `default` or `examples`. Enumerating "positions where a subschema may appear" is therefore not a sound defence.
221
+
222
+ Instead, the whole supplied document is walked — every key and every value — and the tool keeps its schema only if that walk completes and finds nothing dangerous. A walk stops short, and the tool is degraded, when it finds a `pattern` or `patternProperties` keyword at all — whatever its value type, since refusing to vouch for it does not depend on an upstream type guard staying as it is — a `$dynamicRef` or `$recursiveRef`, a `$ref` pointing outside the document, or a graph too deep or too large to inspect within budget. A local `#/...` pointer is accepted only when the walk covers its target *and* that target resolves to something usable as a subschema, with the reference chain proven acyclic — covering the document proves no expression hides behind a pointer, but not that the pointer names a schema at all. A pointer to a non-schema (`#/description`), a pointer with no target (`#/$defs/missing`), an `$anchor` reference (`#name`), and a mutual `$defs` cycle are each degraded rather than forwarded, because the validator would otherwise throw, exhaust the stack, or produce a tool that can never accept any argument. A key that is an argument *name* rather than a keyword — a property literally called `pattern` or `patternProperties` — is left alone.
223
+
224
+ **Degrading replaces the schema, not the tool.** Because regexes are common in real MCP schemas, a tool in this position still registers, with the `args` envelope in place of its schema, and is reported under the `schema-envelope` class. The compatibility cost is real and worth knowing: such a tool loses validation against the proxy-supplied schema but retains Pi's validation of the extension-owned envelope, so its arguments must be passed as an object under `args` and the model sees a less specific contract than the server documents. The server still validates authoritatively, so an argument that violates the proxy-specific contract becomes a server-side tool error rather than a local one.
225
+
226
+ ### What you see when a tool is missing or degraded
227
+
228
+ Every raw entry the proxy returns is accounted for exactly once, and the counts reconcile: `raw = prepared + dropped + beyond-the-cap`, where *prepared* is what will be registered. If a registration pass is refused partway, the shortfall between prepared and registered is reported separately by that pass's own diagnostic. A tool is **dropped** for one of four reasons — `invalid-tool` (no usable name or server identity), `duplicate-identity`, `invalid-schema`, or `name-collision` — or discarded for being beyond the 512-tool limit (`tool-cap`). A tool is **degraded** rather than dropped under `schema-envelope`. Each class is reported with a count and a bounded sample of names through Pi notifications when a UI is active, or stderr in non-interactive mode. MCP messages emitted before the TUI context is ready are buffered until session start. For example:
229
+
230
+ ```text
231
+ LiteLLM MCP: kept 2 MCP tools but replaced their schema with a safe args envelope, because of a schema that could not be proven free of proxy-supplied regexes or references: mcp_srv_lookup_1c97861c1f, mcp_srv_matcher_3ee4c31a5a.
232
+ LiteLLM MCP: dropped 1 MCP tool with a missing name or server identity: entry_3.
233
+ ```
234
+
235
+ A class is re-reported whenever its membership changes, including a change beyond the names shown in the sample, and stays quiet while unchanged — so a stable proxy does not repeat itself across refreshes, while a changing one is always surfaced. A class that stops occurring is cleared, so its recurrence is reported rather than suppressed. These lines never contain the proxy's schema, its response body, its descriptions, or your API key; only generated names, positional placeholders, and counts. They are reported regardless of `LITELLM_VERBOSE_DISCOVERY`; set that variable to also see per-refresh raw, prepared, enveloped, and registered counts. The enveloped count covers every tool carrying the envelope, whether because the proxy supplied no schema or because its schema was degraded, so it is the number of tools with no proxy-specific argument contract.
236
+
237
+ ### Registration lifecycle
238
+
239
+ Pi registers a tool synchronously, replacing any existing tool of the same name, and exposes no way to unregister. It has no notion of rejecting one tool while accepting another: the only failure is a staleness check that fires once the extension instance has been superseded, and that check is never reset. A refused registration therefore ends the whole pass rather than skipping one tool.
240
+
241
+ So a pass that is refused partway leaves the tools registered up to that point, reports one bounded diagnostic, and makes no further attempt from that extension instance — retrying would re-run discovery on every refresh and could never succeed. A reload creates a fresh instance, which starts clean on its own. Because names are stable and registration replaces by name, any retry that does happen is idempotent.
242
+
243
+ Network discovery is separate and remains retryable: a catalog that yields no registrable tool is not recorded as settled, so a later refresh tries again, and the empty result is reported rather than passing in silence. A tool that disappears from the proxy's catalog stays registered until Pi restarts.
244
+
245
+ MCP tools run in Pi's parallel tool mode. Each side-effecting `POST /mcp-rest/tools/call` is attempted exactly once: timeouts, connection failures, HTTP errors, and malformed responses are returned to Pi as tool errors rather than retried. Pi cancellation aborts an in-flight call and preserves its original cancellation reason. Tool-call response bodies are limited to 5 MiB before JSON parsing, and returned result or error text to 64 KiB with a truncation marker.
246
+
247
+ A passed-through schema's `format` keyword is evaluated, using the validator's own built-in expressions rather than anything the proxy supplies.
248
+
249
+ Tools discovered this way are only as trustworthy as the MCP servers behind your proxy: their descriptions and results are text the model reads.
186
250
 
187
251
  ## LiteLLM Skill Hub
188
252
 
@@ -194,9 +258,9 @@ If your LiteLLM proxy exposes `/claude-code/marketplace.json`, enabled skills ar
194
258
 
195
259
  ## Mocked LiteLLM smoke workflow
196
260
 
197
- The `LiteLLM Smoke` GitHub Actions workflow starts VidaiMock and a real LiteLLM proxy on the runner. LiteLLM exposes OpenAI-compatible and Anthropic routes whose upstreams are served by VidaiMock, then this extension's smoke runner discovers those models through LiteLLM and sends `/v1/chat/completions` requests through the proxy.
261
+ The `LiteLLM Smoke` GitHub Actions workflow starts VidaiMock and a real LiteLLM proxy on the runner. LiteLLM exposes route-distinct Chat, Responses, native Messages, and mixed-deployment models whose upstreams are served by VidaiMock. The smoke runner discovers those models through LiteLLM, asserts each model's expected API, exercises `/v1/chat/completions`, `/v1/responses`, and `/v1/messages`, verifies the expected `x-litellm-response-cost` behavior, and proves endpoint coverage from captured LiteLLM request logs rather than response text.
198
262
 
199
- This keeps the LiteLLM integration path under test but does not call real LLM APIs. No provider API keys or GitHub Models permission are required. The smoke runner also asserts that discovery came from `/model/info` (`LITELLM_SMOKE_EXPECT_SOURCE`) so a silent fallback to `/v1/models` fails the run. The workflow also runs auth checks plus optional Postgres-backed auth checks when `LITELLM_LICENSE` is configured for virtual-key and admin-route behavior, then runs a non-interactive Pi CLI smoke with `--list-models` and `-p` against both the OpenAI-compatible and Anthropic-backed routes, so extension loading, model discovery, and real completion paths are covered without opening the TUI. It also runs an interactive Pi TUI smoke covering `/login litellm` and Pi's native `/model` refresh.
263
+ This keeps the LiteLLM integration path under test but does not call real LLM APIs. No provider API keys or GitHub Models permission are required. The smoke runner also asserts that discovery came from `/model/info` (`LITELLM_SMOKE_EXPECT_SOURCE`) so a silent fallback to `/v1/models` fails the run. The workflow also runs auth checks plus optional Postgres-backed auth checks when `LITELLM_LICENSE` is configured for virtual-key and admin-route behavior, then runs a non-interactive Pi CLI smoke with `--list-models` and `-p` against both the OpenAI-compatible and Anthropic-backed routes, so extension loading, model discovery, and real completion paths are covered without opening the TUI. It also runs an interactive Pi TUI smoke covering `/login litellm` and Pi's native `/model` refresh. VidaiMock returns fixed responses, so the real-proxy smoke does not exercise a model-originated tool call or thinking block; the native Messages compatibility suite covers thinking, tool use, tool results, and replay across turns.
200
264
 
201
265
  ## Development
202
266
 
@@ -208,7 +272,7 @@ npm run check
208
272
  npm run clean && npm run build
209
273
  ```
210
274
 
211
- `npm run check` runs Biome, type checking, and the Vitest suite. Runtime changes must be built before local Pi smoke checks because the extension entrypoint is `./dist/index.js`.
275
+ `npm run check` runs Biome, type checking, the Vitest suite, and the supply-chain package-content guard. Pi installs and local smoke checks load the shipped `src/index.ts` entrypoint directly; `dist/` is verification output only.
212
276
 
213
277
  Before changing package contents or dependency policy, also run:
214
278
 
@@ -217,28 +281,90 @@ npm run supply-chain:guard
217
281
  npm pack --dry-run
218
282
  ```
219
283
 
220
- The published npm package should contain only `dist`, `README.md`, and `LICENSE`.
284
+ The published npm package contains only `src`, `README.md`, `LICENSE`, and the `package.json` npm always includes. Pi loads the TypeScript source entrypoint for both npm and Git installs. The package does not expose a JavaScript or TypeScript library import; load it through Pi.
221
285
 
222
286
  ## Release
223
287
 
224
- Releases are driven by semver tags named `v*.*.*`. The GitHub release workflow installs from the lockfile, runs the checks, builds `dist`, verifies the package tarball, publishes to npm with provenance, and creates a GitHub release.
288
+ Releases are driven by semver tags named `v*.*.*`. The GitHub release workflow installs from the lockfile, then `npm publish` runs `prepublishOnly` to check the source, build `dist` for verification, verify the package contents, and publish to npm with provenance before the workflow creates a GitHub release.
225
289
 
226
290
  Before tagging a release, keep `package.json` and `package-lock.json` versions in sync and verify the dry-run package contents.
227
291
 
228
292
  ## Model catalog
229
293
 
230
- Dynamic catalogs are persisted by Pi in `~/.pi/agent/models-store.json`. Credentials remain in `~/.pi/agent/auth.json`. Legacy `litellm-models*.json` files are ignored and are not deleted.
294
+ Dynamic catalogs are persisted by Pi in `~/.pi/agent/models-store.json`. Credentials remain in `~/.pi/agent/auth.json`. Legacy `litellm-models.json` model caches are ignored and never deleted. `litellm-models-dev.json` is the models.dev cache and is refreshed in place.
295
+
296
+ For every genuine `/model/info` row, including deployment details fetched through `/health`, the extension requests `https://models.dev/api.json` for enrichment and caches the result for 28 days in `~/.pi/agent/litellm-models-dev.json`. Health-only entries without deployment details use the bounded Pi catalog lookup without models.dev enrichment. `PI_OFFLINE` suppresses activation-time discovery and the models.dev request. `LITELLM_OFFLINE=1` also disables LiteLLM discovery; direct discovery callers use only an existing models.dev cache and do not refresh it.
231
297
 
232
298
  Opening `/model` refreshes configured provider catalogs in the background using Pi's native model lifecycle.
233
299
 
300
+ ### Model host enforcement
301
+
302
+ Models this provider dispatches are sent to the root resolved from the active credential. The extension hides a model from `/model` when its stored root, including any path prefix, does not match that credential, and its dispatch-time guard rejects stale, malformed, placeholder, and unsupported models before a request. For accepted models, the request URL is re-derived from the credential's proxy root instead of trusting a configured model URL. Explicit `allowInsecureHttp` settings continue to apply to this validation. Path prefixes are part of the root and remain case-sensitive.
303
+
304
+ Catalog filtering and dispatch are separate. Choosing a model by ID or restoring it from a session can skip filtering. Pi 0.84 routes such a model to the provider only when the provider's current catalog already contains the same `api`. The native `Provider` contract has no separate protocol-capability declaration. If the catalog contains no model for that API, Pi uses its global API implementation instead, bypassing this extension's dispatch-time host guard. The extension deliberately returns an ordinary model array rather than falsifying its contents through an overridden Array method.
305
+
306
+ To keep that fallback safe, resolved auth carries `baseUrl` set to the credential's proxy root. Pi applies it to every request path — the provider guard and the global fallback alike — so a stale, placeholder, or attacker-supplied model `baseUrl` is replaced with the credential root before the request leaves the process. The credential can only ever reach the host it was issued for. A model whose `api` is absent from the catalog may still fail to complete on the fallback path (the global implementation builds a slightly different URL), but it cannot exfiltrate the credential. When no usable root resolves, auth carries no `baseUrl` and the provider guard rejects the request outright.
307
+
308
+ Opening `/model` against the active proxy repopulates the protocols discovery actually selects, which is the supported way to use a model whose `api` is currently absent from the catalog.
309
+
310
+ A model is hidden when:
311
+
312
+ - no base URL resolves from settings or credentials
313
+ - the base URL is invalid or remains the `https://litellm.example.com` placeholder
314
+ - its stored root differs from the active credential root, such as after switching proxies or path-scoped tenants
315
+ - it declares an API this extension does not implement
316
+
317
+ Each distinct availability diagnostic is written once per session on stderr. `LITELLM_OFFLINE=1` does not recover a root mismatch: refresh online against the active proxy first, then return to offline use.
318
+
319
+ ### Protocols and prompt caching
320
+
321
+ Discovery selects native Messages for proven Claude groups under the [Model transport](#model-transport) rules, Responses for OpenAI-family backends, and Chat Completions for other OpenAI-compatible backends. An Azure deployment with an explicit `api_version` older than `2025-03-01-preview` stays on Chat Completions; an unset or newer version uses Responses. Azure is detected from the adapter field, an `azure/` or `azure_ai/` model prefix, or the reported provider; other adapters follow the backend family because LiteLLM bridges `/v1/responses` to Chat Completions when the provider has no native Responses config (`litellm/responses/main.py`, `_bridges_to_chat_completions`). A route whose model prefix and `custom_llm_provider` name different providers is treated as unidentified and stays on Chat Completions, so it also does not get the local 128-tool preflight. When `/model/info` supplies `supported_endpoints`, that list takes precedence, and `mode: "responses"` remains an explicit Responses signal. An evidence-free fallback entry—a bare `/v1/models` id or a `/health` entry with no detail row—takes its protocol from the Pi catalog entry for its id; both evidence-free fallback paths also inherit presentation metadata from the bounded catalog lookup. An unknown id stays on Chat Completions. The route name alone authorizes nothing. A concrete id expanded from wildcard `/model/info` routes inherits the deployment evidence of the wildcard route LiteLLM would select for it, with the requested id substituted into the row's `model` the way LiteLLM serves it; the id is omitted when that selected route is not a published chat-style route. Chat Completions and Responses use `<root>/v1`; native Messages uses the proxy root directly. All three protocols pass through the provider's host guard. When an evidence-free cached `(no metadata)` entry can be enriched from the Pi catalog, it is restored on Responses only for a catalog Responses model and otherwise on Chat Completions, never Anthropic Messages. A model supplied only through `models.json` whose `api` this provider does not implement takes Pi's global API fallback rather than this provider's host guard; the credential-root pin (see [Model host enforcement](#model-host-enforcement)) still keeps the credential on the active proxy.
322
+
323
+ `cacheControlFormat: "anthropic"` applies only to Chat Completions, where Pi adds Anthropic `cache_control` markers. The Responses transport has a different compatibility type and uses native `prompt_cache_key` (plus supported retention fields), so Responses models must not receive `cacheControlFormat`. Freshly discovered OpenAI-family models forced onto Chat Completions are rejected locally when a request contains more than 128 tools; route them through Responses or reduce enabled extensions. Moonshot request-side reasoning suppression follows LiteLLM's routing provider, not `model_info.base_model`; response-side `<think>` normalization follows discovered Kimi-family evidence. Opaque aliases retain display normalization without sending Moonshot-only parameters to Azure- or Bedrock-hosted Kimi models. Strict tool-message repair requires every deployment to identify Kimi; Gemini effort normalization likewise requires consistent backend-family evidence.
324
+
325
+ Discovery policy is versioned in Pi's persisted model catalog. After upgrading, an online refresh replaces legacy entries. Offline legacy entries remain usable without a diagnostic; the extension reports the discovery-version mismatch once only when an attempted online refresh fails. To force rediscovery or roll back across this policy change, delete the `litellm` entry from `~/.pi/agent/models-store.json` (or delete the file) and refresh once online.
326
+
327
+ ### Deployment groups and metadata authority
328
+
329
+ LiteLLM may load-balance one public `model_name` across deployments with different backends or model versions. The extension reduces `/model/info` rows conservatively before publishing one Pi model. A route group that mixes a chat-style deployment with an explicitly incompatible mode such as embedding is withheld entirely and reported with a bounded diagnostic.
330
+
331
+ - Responses is selected only when every deployment supports it under the protocol rules above; a deployment that requires Chat keeps the group on Chat.
332
+ - Vision and reasoning are advertised only when every routable deployment resolves them as supported. An explicit `supports_reasoning: false` suppresses all thinking controls, even when catalog or router effort metadata exists. Catalog thinking controls are intersected per level: a level is exposed only when every deployment maps it to the same value, while disagreement or absence becomes an explicit denial. Reasoning selectors additionally require an accepted wire carrier on every deployment. Public effort lists are intersected, explicit LiteLLM denials win, and `xhigh`/`max` need unanimous explicit support. Null or absent standard-effort flags have no opinion.
333
+ - Context and output limits use the minimum resolved value across deployments.
334
+ - Each displayed base-price field uses the maximum only when every deployment resolves that field; unresolved fields remain zero and the model name is suffixed with ` (incomplete metadata)`.
335
+ - When complete base pricing comes from the catalog under unanimous catalog authority, tiered pricing is the conservative worst-case envelope. Its usable finite non-negative thresholds are the sorted union of every deployment's thresholds, and each price field at each interval is the maximum applicable rate across all deployments, floored by each deployment's base rate. Differing tier breakpoints are therefore preserved rather than treated as incompatible. An explicit `/model/info` base-price field replaces catalog pricing for that field at every threshold; catalog tiers remain for unaffected fields and are omitted only when every base-price field is explicit.
336
+ - Reduced route groups omit tier ladders when complete base-pricing or catalog authority is unavailable. Wildcard expansion retains every known tier from matching groups even when another match has incomplete metadata: suppressing a complete sibling's higher tier could understate the known worst-case rate. The ` (incomplete metadata)` marker remains because the resulting envelope is only as complete as the available ladders.
337
+ - Wildcard routes expand through `/v1/models`; dropped exact or wildcard groups suppress matching expanded ids.
338
+ - Expanded ids inherit conservative metadata bounds from every matching wildcard group, including the same union-threshold, maximum-applicable-rate tier envelope. Protocol and backend-family evidence follow the most-specific route LiteLLM selects, with every deployment of that selected route voting together.
339
+ - Catalog metadata is accepted only from unanimous deployment identities resolved from `model_info.base_model` first and `litellm_params.model` second; the public `model_name` route is never backend authority. `litellm_params.custom_llm_provider` is provider evidence for the selected backend model: a non-generic provider that conflicts with the model prefix withholds identity, while generic transport adapters are ignored. Recognized provider prefixes remain identity evidence even when the named model is absent from that provider's catalog. Different concrete catalog models conflict even within one provider; an unresolved Azure deployment name may still use its resolved `base_model`. Conflicts across a group trigger the bounded ambiguous-authority diagnostic; ambiguous groups are not matched across all Pi provider catalogs. Cross-host Claude routes spanning providers such as Vertex and Bedrock may intentionally trigger this withholding: explicit router metadata remains usable, but the route does not borrow either host's catalog metadata.
340
+ - `/v1/models` and `/health` do not provide deployment-level backend identity. For `/v1/models`, an unqualified ID is resolved only within an explicitly recognized `owned_by` provider or through the bounded alias rules documented here (currently Anthropic aliases), and is never searched across every Pi provider catalog. Both `/v1/models` fallback entries and `/health` entries without deployment detail take their protocol from the matching Pi catalog model: `openai-responses` selects Responses, and every other or missing catalog API selects Chat. Wholly health-only groups also inherit presentation metadata from that bounded Pi catalog lookup; a bare row mixed with deployment details grants no catalog authority to the group. A route name substituted for an unreadable deployment `model_name` also denies router-reported thinking levels. Unresolved evidence-free entries retain the ` (no metadata)` marker; groups containing deployment details remain permanently incomplete when their evidence is insufficient.
341
+
342
+ The ` (no metadata)` suffix is reserved for unresolved, evidence-free entries mapped by the full `/v1/models` fallback or wholly health-only `/health` groups. During wildcard expansion after a successful `/model/info` response, IDs that match no surviving wildcard route are discarded. A matched wildcard expansion inherits its matching groups' authority and is either unmarked or marked ` (incomplete metadata)`, never ` (no metadata)`. A recognized `owned_by` provider that conflicts with a provider-qualified model ID instead receives ` (incomplete metadata)`, permanently withholding cache enrichment rather than falling through to the ID prefix. Only an unchanged evidence-free sentinel shape is eligible for bounded catalog enrichment on a later cache read, when it also refreshes provider compatibility metadata from the model ID, adopts Responses only for a catalog `openai-responses` model, and otherwise uses Chat.
343
+
344
+ The ` (incomplete metadata)` suffix marks reduced `/model/info` groups, incomplete matched wildcard expansions, or `/health` groups with deployment details and permanently prevents route-name cache enrichment. It means at least one metadata field is unknown, including cache pricing that the proxy omitted; known input/output prices may still be shown alongside the suffix.
345
+
346
+ ### Reasoning controls
347
+
348
+ Reasoning levels require deployment evidence for the parameter Pi sends: `reasoning_effort` for effort levels or `thinking` for a native thinking switch. Public route names never authorize these controls. Evidence-free fallback models retain catalog presentation metadata but expose no speculative selector.
349
+
350
+ Kimi K2.5/K2.6 use an on/off thinking switch; K2.7 Code/Highspeed is always thinking. Kimi K3 and DeepSeek V4 use public effort evidence with LiteLLM overrides. Responses levels are translated to valid `reasoning.effort` values; Chat-only compatibility fields never reach Responses models. Legacy cached level maps remain usable while discovery refreshes, and stored Moonshot compatibility can restore safe display normalization.
351
+
352
+ The development probe runs against minimized snapshots with `npm run probe:proxy -- --snapshot tests/fixtures/proxy/prod-model-info-2026-09-04.json`. A bounded live matrix is available with `--live --base-url <proxy-url> --max-requests 100`; its request bodies use the discovered carrier and request policy.
353
+
234
354
  ## Troubleshooting
235
355
 
236
356
  | Symptom | Likely cause |
237
357
  |---|---|
238
358
  | "no credentials" warning at startup | Env vars not set and no OAuth credential — run `/login litellm` |
359
+ | `No models available` at startup, gone after a restart | A Pi startup race, not discovery — see [`No models available` at startup](#no-models-available-at-startup) |
239
360
  | "discovered no models" | Proxy returned an empty list — check pi's startup log and verify `/model/info`, `/v1/models`, or `/health` responds |
240
361
  | `/model/info` returning 401/403/404 | Expected behavior with virtual keys — extension falls back to `/v1/models` |
241
- | Discovery times out | Increase `LITELLM_DISCOVERY_TIMEOUT_MS` or set `LITELLM_OFFLINE=1` to fall back on cached models |
362
+ | Discovery times out | Increase `LITELLM_DISCOVERY_TIMEOUT_MS` or set `LITELLM_OFFLINE=1` to fall back on cached models. Offline mode does not recover a root mismatch, including a different path prefix — see [Model host enforcement](#model-host-enforcement) |
363
+ | A provider shows no models | The base URL is missing, invalid, still the placeholder, or its full root, including any path prefix, differs from the root in the cached catalog. Check stderr and see [Model host enforcement](#model-host-enforcement) |
364
+ | A configured or restored model fails to complete but never leaks the credential | Its `api` is absent from the current provider catalog, so Pi used global API fallback with the credential root pinned. Open `/model` against the active proxy to repopulate the protocol — see [Model host enforcement](#model-host-enforcement) |
365
+ | `LiteLLM discovery: ... route group(s) have missing or conflicting deployment provider evidence` | One or more deployments lack resolvable backend authority or resolve to different providers or concrete catalog models. Add consistent `litellm_params.model`, `model_info.base_model`, or adapter metadata; catalog-derived limits, pricing, and reasoning metadata are withheld meanwhile. This may be intentional for cross-host routes such as Vertex/Bedrock Claude. |
366
+ | `LiteLLM discovery: ... route group(s) mix chat-style and explicitly incompatible deployment modes` | A public route can select both a chat-style deployment and an incompatible deployment such as an embedding model. Give the embedding deployment its own `model_name`. |
367
+ | A model is marked ` (incomplete metadata)` | `/model/info` or `/health` did not provide enough authoritative metadata. Explicit fields remain usable, but unknown cost fields are shown as zero and route-name cache enrichment stays disabled. |
242
368
  | `401 Token expired` | Set `LITELLM_API_KEY_HELPER`. |
243
369
  | No models with gcloud auth | Verify `gcloud auth application-default login` has been run or set `GOOGLE_APPLICATION_CREDENTIALS` to an `authorized_user` ADC file |
244
370
  | Enterprise SSO waits for token insertion | The proxy returned 404/405 for `/sso/cli/start`, so Pi used the legacy flow — upgrade LiteLLM or paste the UI token |
@@ -246,8 +372,30 @@ Opening `/model` refreshes configured provider catalogs in the background using
246
372
  | Enterprise SSO login shows "virtual key generation failed" | The LiteLLM instance may lack a database (`/key/generate` requires one), your user account may lack key-generation permission, or the request timed out; the JWT is used directly as a fallback |
247
373
  | Enterprise SSO token prompt fails with "SSO token is required" | The token field was left empty — paste the token copied from the LiteLLM UI |
248
374
  | MCP tools not showing | Verify the proxy exposes `/mcp-rest/tools/list` and open `/model` after fixing the proxy |
375
+ | Some MCP tools missing or unvalidated | Check Pi notifications (stderr in non-interactive mode) for `LiteLLM MCP:` messages. Dropped tools are reported under `invalid-tool`, `duplicate-identity`, `invalid-schema`, `name-collision`, or `tool-cap`, each with a count and a bounded sample of generated names. A tool reported under `schema-envelope` is still present but uses the `args` envelope instead of its own schema. A refused registration pass is reported once. Set `LITELLM_VERBOSE_DISCOVERY=1` for per-refresh raw/prepared/enveloped/registered counts |
249
376
  | Skills not affecting prompts | Verify the proxy exposes `/claude-code/marketplace.json` or `/v1/skills` and returns enabled skills |
250
377
 
378
+ ### `No models available` at startup
379
+
380
+ Pi 0.84.0 and later can finish startup before the provider availability snapshot is written, so the
381
+ initial model pick sees nothing even though discovery succeeded. The warning is intermittent and a
382
+ restart usually clears it. Nothing is wrong with the catalog: `/model` in the same session still
383
+ lists every discovered model, and picking one there fixes that session.
384
+
385
+ Scoping models keeps selection off that path, because Pi resolves the scope with its own awaited
386
+ availability pass before it picks a model. In `~/.pi/agent/settings.json`:
387
+
388
+ ```json
389
+ {
390
+ "enabledModels": ["litellm/*"]
391
+ }
392
+ ```
393
+
394
+ Your `defaultProvider` and `defaultModel` still win as long as they match a pattern. `/model` then
395
+ opens on the scoped list, with a toggle inside the picker for the full catalog.
396
+
397
+ The extension cannot set this for you — Pi reads `enabledModels` before extensions activate.
398
+
251
399
  ## License
252
400
 
253
401
  MIT — see [LICENSE](./LICENSE).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-provider-litellm",
3
- "version": "2.2.1",
3
+ "version": "3.0.0",
4
4
  "description": "LiteLLM proxy provider extension for Pi",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -20,22 +20,14 @@
20
20
  "provider",
21
21
  "proxy"
22
22
  ],
23
- "main": "./dist/index.js",
24
- "types": "./dist/index.d.ts",
25
- "exports": {
26
- ".": {
27
- "types": "./dist/index.d.ts",
28
- "import": "./dist/index.js"
29
- }
30
- },
31
23
  "files": [
32
- "dist",
24
+ "src",
33
25
  "README.md",
34
26
  "LICENSE"
35
27
  ],
36
28
  "pi": {
37
29
  "extensions": [
38
- "./dist/index.js"
30
+ "./src/index.ts"
39
31
  ],
40
32
  "image": "https://raw.githubusercontent.com/balcsida/pi-provider-litellm/refs/heads/main/assets/pi_litellm_gallery.png"
41
33
  },
@@ -46,17 +38,18 @@
46
38
  "typecheck": "tsgo --noEmit",
47
39
  "lint": "biome check --error-on-warnings .",
48
40
  "format": "biome check --write .",
49
- "test": "vitest --run",
41
+ "test": "vitest --run --dir tests",
42
+ "probe:proxy": "tsx scripts/probe-proxy.ts",
50
43
  "supply-chain:guard": "tsx scripts/supply-chain-guard.ts",
51
- "check": "npm run lint && npm run typecheck && npm test",
44
+ "check": "npm run lint && npm run typecheck && npm test && npm run supply-chain:guard",
52
45
  "prepublishOnly": "npm run check && npm run clean && npm run build && npm run supply-chain:guard"
53
46
  },
54
47
  "engines": {
55
48
  "node": ">=22.19.0"
56
49
  },
57
50
  "peerDependencies": {
58
- "@earendil-works/pi-ai": ">=0.81.0",
59
- "@earendil-works/pi-coding-agent": ">=0.81.0"
51
+ "@earendil-works/pi-ai": ">=0.83.0",
52
+ "@earendil-works/pi-coding-agent": ">=0.83.0"
60
53
  },
61
54
  "peerDependenciesMeta": {
62
55
  "@earendil-works/pi-ai": {
@@ -74,12 +67,14 @@
74
67
  },
75
68
  "devDependencies": {
76
69
  "@biomejs/biome": "^2.4.15",
77
- "@earendil-works/pi-ai": "^0.84.2",
78
- "@earendil-works/pi-coding-agent": "^0.84.2",
79
- "@kitlangton/terminal-control": "0.6.0",
70
+ "@earendil-works/pi-ai": "^0.85.1",
71
+ "@earendil-works/pi-coding-agent": "^0.85.1",
72
+ "@kitlangton/terminal-control": "1.2.1",
80
73
  "@types/node": "^26.0.0",
81
74
  "@typescript/native-preview": "7.0.0-dev.20260707.2",
75
+ "es-module-lexer": "^3.0.2",
82
76
  "tsx": "^4.21.0",
83
- "vitest": "^4.1.7"
77
+ "typebox": "1.3.30",
78
+ "vitest": "^5.0.0"
84
79
  }
85
80
  }
@@ -0,0 +1,100 @@
1
+ export const LITELLM_DISCOVERY_VERSION = 2 as const;
2
+
3
+ export type BackendFamily = "claude" | "deepseek" | "gemini" | "kimi" | "openai";
4
+
5
+ export interface BackendIdentityRow {
6
+ model_name?: string;
7
+ litellm_params?: { model?: string; custom_llm_provider?: string };
8
+ model_info?: { base_model?: string; litellm_provider?: string };
9
+ }
10
+
11
+ export interface BackendIdentity {
12
+ provider?: string;
13
+ modelId: string;
14
+ qualifiedId: string;
15
+ family?: BackendFamily;
16
+ }
17
+
18
+ const GENERIC_ADAPTERS = new Set(["azure", "azure_ai", "custom_openai", "openai_like"]);
19
+ const GENERIC_CATALOG_PROVIDERS = new Set([...GENERIC_ADAPTERS, "openai", "text-completion-openai"]);
20
+ // Providers whose name alone settles the family. `openai` is deliberately absent: LiteLLM
21
+ // uses that provider for any OpenAI-compatible server, so it says nothing about the model.
22
+ const PROVIDER_FAMILIES: Readonly<Record<string, BackendFamily>> = {
23
+ anthropic: "claude",
24
+ deepseek: "deepseek",
25
+ gemini: "gemini",
26
+ moonshot: "kimi",
27
+ moonshotai: "kimi",
28
+ };
29
+ // Prefixes whose keyword match is trustworthy across the whole "prefix/modelId" string because
30
+ // the prefix itself names a vendor, not just some unrelated string that happens to contain one.
31
+ const KNOWN_VENDOR_PREFIXES = new Set([...Object.keys(PROVIDER_FAMILIES), "openai"]);
32
+ // `o\d` (OpenAI's o1/o3/o4-mini reasoning models) is only trustworthy at the start of the id or
33
+ // right after a provider path segment — an interior "-o1-" is as likely to be an unrelated
34
+ // product's own version marker (e.g. "custom-o1-clone").
35
+ const OPENAI_FAMILY_PATTERN = /(?:^|[./_-])(?:openai|gpt|codex)(?:$|[./_:-])|(?:^|\/)o\d(?:$|[./_:-])/i;
36
+
37
+ export function isRecord(value: unknown): value is Record<string, unknown> {
38
+ return typeof value === "object" && value !== null && !Array.isArray(value);
39
+ }
40
+
41
+ function wireString(value: unknown): string | undefined {
42
+ const trimmed = typeof value === "string" ? value.trim() : undefined;
43
+ return trimmed && trimmed !== "undefined" ? trimmed : undefined;
44
+ }
45
+
46
+ function semanticFamily(id: string): BackendFamily | undefined {
47
+ const value = id.toLowerCase();
48
+ if (/(?:^|[./_-])(?:anthropic|claude|opus|sonnet|haiku|fable)(?:$|[./_:-])/.test(value)) return "claude";
49
+ if (/(?:^|[./_-])(?:moonshotai|moonshot|kimi)(?:$|[./_:-])/.test(value)) return "kimi";
50
+ if (/(?:^|[./_-])deepseek(?:$|[./_:-])/.test(value)) return "deepseek";
51
+ if (/(?:^|[./_-])gemini(?:$|[./_:-])/.test(value)) return "gemini";
52
+ if (isOpenAIBackend(value)) return "openai";
53
+ return undefined;
54
+ }
55
+
56
+ export function isOpenAIBackend(id: string): boolean {
57
+ return OPENAI_FAMILY_PATTERN.test(id);
58
+ }
59
+
60
+ export function resolveBackendIdentity(row: BackendIdentityRow): BackendIdentity | undefined {
61
+ const raw =
62
+ wireString(row.model_info?.base_model) ?? wireString(row.litellm_params?.model) ?? wireString(row.model_name);
63
+ if (!raw) return undefined;
64
+
65
+ const slash = raw.indexOf("/");
66
+ const prefix = slash > 0 ? raw.slice(0, slash).trim().toLowerCase() : undefined;
67
+ const prefixProvider = prefix && !GENERIC_ADAPTERS.has(prefix) ? prefix : undefined;
68
+ // `custom_llm_provider` is how LiteLLM routes an unprefixed model. It is provider evidence
69
+ // in its own right, so a prefix that names a different provider is a conflict, not a tiebreak.
70
+ const custom = wireString(row.litellm_params?.custom_llm_provider)?.toLowerCase();
71
+ const customProvider = custom && !GENERIC_ADAPTERS.has(custom) ? custom : undefined;
72
+ if (prefixProvider && customProvider && prefixProvider !== customProvider) return undefined;
73
+ const provider = prefixProvider ?? customProvider;
74
+ const modelId = slash > 0 ? raw.slice(slash + 1) : raw;
75
+ // Scan the full "prefix/modelId" string only when the prefix is itself a whole known vendor
76
+ // name (e.g. "openai/production"); otherwise scan modelId alone, so an unrelated custom
77
+ // prefix that merely contains a vendor substring (e.g. "deepseek-proxy/gpt-4-turbo") can't
78
+ // contaminate the keyword match with its own name.
79
+ const scanTarget = !provider || KNOWN_VENDOR_PREFIXES.has(provider) ? raw : modelId;
80
+ const family = semanticFamily(scanTarget) ?? (provider ? PROVIDER_FAMILIES[provider] : undefined);
81
+ return {
82
+ ...(provider ? { provider } : {}),
83
+ modelId,
84
+ qualifiedId: provider ? `${provider}/${modelId}` : modelId,
85
+ ...(family ? { family } : {}),
86
+ };
87
+ }
88
+
89
+ export function resolveCatalogProvider(
90
+ row: BackendIdentityRow,
91
+ identity = resolveBackendIdentity(row),
92
+ ): string | undefined {
93
+ if (identity?.provider && !GENERIC_CATALOG_PROVIDERS.has(identity.provider)) return identity.provider;
94
+ const reported = wireString(row.model_info?.litellm_provider)?.toLowerCase();
95
+ const custom = wireString(row.litellm_params?.custom_llm_provider)?.toLowerCase();
96
+ if (custom && reported && GENERIC_CATALOG_PROVIDERS.has(reported)) {
97
+ return custom;
98
+ }
99
+ return identity?.provider ?? reported ?? custom;
100
+ }
package/src/cost.ts ADDED
@@ -0,0 +1,52 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+
3
+ export function setupLiteLLMCostTracking(pi: ExtensionAPI, providerNames: string[]): void {
4
+ const knownProviders = new Set(providerNames);
5
+ // Keyed by provider: with multiple LiteLLM providers active, overlapping
6
+ // requests interleave, so a single shared slot lets one provider's response
7
+ // (or a headerless one clearing it) clobber another's pending cost.
8
+ const pendingResponseCosts = new Map<string, number>();
9
+
10
+ pi.on("after_provider_response", (event, ctx) => {
11
+ // Global Pi hook: responses from other providers must not feed LiteLLM cost state.
12
+ if (!ctx.model?.provider || !knownProviders.has(ctx.model.provider)) return;
13
+ const costHeader = event.headers?.["x-litellm-response-cost"] ?? event.headers?.["X-Litellm-Response-Cost"];
14
+ if (costHeader) {
15
+ const cost = Number.parseFloat(String(costHeader));
16
+ if (!Number.isNaN(cost)) {
17
+ pendingResponseCosts.set(ctx.model.provider, cost);
18
+ return;
19
+ }
20
+ }
21
+ pendingResponseCosts.delete(ctx.model.provider);
22
+ });
23
+
24
+ pi.on("message_end", async (event) => {
25
+ if (event.message.role !== "assistant" || !knownProviders.has(event.message.provider)) return;
26
+
27
+ const usage = event.message.usage;
28
+ if (!usage) return;
29
+
30
+ const totalCost = pendingResponseCosts.get(event.message.provider);
31
+ if (totalCost === undefined) return;
32
+ pendingResponseCosts.delete(event.message.provider);
33
+
34
+ const totalTokens = usage.input + usage.output + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0);
35
+ const fraction = (tokens: number): number => (totalTokens > 0 ? tokens / totalTokens : 0);
36
+ return {
37
+ message: {
38
+ ...event.message,
39
+ usage: {
40
+ ...usage,
41
+ cost: {
42
+ input: totalCost * fraction(usage.input),
43
+ output: totalCost * fraction(usage.output),
44
+ cacheRead: totalCost * fraction(usage.cacheRead ?? 0),
45
+ cacheWrite: totalCost * fraction(usage.cacheWrite ?? 0),
46
+ total: totalCost,
47
+ },
48
+ },
49
+ },
50
+ };
51
+ });
52
+ }