Skip to content

Experience-Driven Black-Box Cost Control for Coding Agents

Pi: side calls can load a provider's extension and run a named pi

Aaronb6ff310c11e6
diff --git a/plugin/README.md b/plugin/README.md
index 56fd4dd..fa9cb62 100644
--- a/plugin/README.md
+++ b/plugin/README.md
@@ -289,6 +289,12 @@ after the loop guard's redirect gets a notice: press **Esc** to abort it, or Ctr
**3. Work normally.** See [What to expect](#what-to-expect).
+**Model calls.** The proposer and the loop guard's judge run `pi --print` with no extensions,
+on your default model (`paid_model` names another). If that model's provider comes from an
+extension (a company gateway, a package's provider), list the extension's file in
+`pi_extensions` so the calls can reach it; it loads with no tools. If the first `pi` on your PATH
+is a wrapper the calls should not run, set `pi_cli` to the real one.
+
**Update.** `pi update --extensions` (plain `pi update` does not update extensions).
**Uninstall.** `pi remove git:github.com/microsoft/TokenEconomizer` (the source you installed
@@ -517,6 +523,8 @@ of the preset. A value you set with `loopguard.<name>` wins over both. Defaults
| Key | Default | Values |
|---|---|---|
| `paid_model` | `""` | model for the plugin's own calls; empty: `haiku` on Claude Code, the agent's own default model on Codex and Pi, on Copilot `COPILOT_PROVIDER_MODEL_ID` or `COPILOT_MODEL` (unless it is `auto`), else a BYOK provider's Haiku, else Copilot's own default |
+| `pi_extensions` | `""` | Pi only: extension files (comma-separated paths) the plugin's own calls load, for a model whose provider an extension registers |
+| `pi_cli` | `""` | Pi only: the `pi` the plugin's own calls run; empty: the first on PATH |
| `shadow` | `false` | decide and log everything, act on nothing |
| `mode` | `auto` | `interactive` or `headless` to override what the plugin detects |
diff --git a/tests/test_live_core.py b/tests/test_live_core.py
index 53173e4..5c9e3d4 100644
--- a/tests/test_live_core.py
+++ b/tests/test_live_core.py
@@ -979,6 +979,38 @@ def test_a_pi_side_call_is_a_bare_run_on_the_persons_own_pi(monkeypatch):
assert calls[-1][0][-4:-2] == ["--model", "openrouter/anthropic/claude-haiku-4.5"]
+def test_a_pi_side_call_loads_the_provider_extensions_and_the_pi_the_person_names(
+ monkeypatch, tmp_path):
+ """A model whose provider an extension registers is not there under `--no-extensions`; Pi
+ still loads an explicit `--extension`, and `--no-tools` keeps the extension's tools out."""
+ import subprocess
+ from tokeneconomizer.plugin import sidecall
+ calls = []
+
+ def fake_run(cmd, **kw):
+ calls.append(cmd)
+ return subprocess.CompletedProcess(
+ cmd, 0, stdout="\n".join(json.dumps(e) for e in PI_JSON_RUN), stderr="")
+
+ monkeypatch.setattr(sidecall.subprocess, "run", fake_run)
+ monkeypatch.chdir(tmp_path)
+ cfg = live_config.load_config(str(tmp_path / "te"), env={
+ "TE_PI_EXTENSIONS": "ext/gateway.ts, builtin:web", "TE_PI_CLI": "/opt/pi/bin/pi",
+ "TE_PAID_MODEL": "gateway/claude-haiku"})
+ assert not cfg.warnings
+ text, _ = sidecall.runner_for("pi", cfg)("propose facts")
+ cmd = calls[-1]
+ assert text == "- use python -m pytest" and cmd[0] == "/opt/pi/bin/pi"
+ assert "--no-extensions" in cmd and "--no-tools" in cmd
+ assert [cmd[i + 1] for i, a in enumerate(cmd) if a == "--extension"] == [
+ os.path.join(os.getcwd(), "ext", "gateway.ts"), "builtin:web"] # the call runs elsewhere
+ assert cmd[-4:] == ["--model", "gateway/claude-haiku", "--", "propose facts"]
+ # unset, the call is what it was: the first `pi` on PATH, no extension
+ sidecall.runner_for("pi", live_config.LiveConfig())("propose facts")
+ assert calls[-1][0] == "pi" and "--extension" not in calls[-1]
+
+
def test_a_copilot_notice_is_a_progress_line_before_the_hook_output():
import io
from tokeneconomizer.plugin import hook
diff --git a/tokeneconomizer/plugin/config.py b/tokeneconomizer/plugin/config.py
index b3dc71c..058a0c9 100644
--- a/tokeneconomizer/plugin/config.py
+++ b/tokeneconomizer/plugin/config.py
@@ -45,7 +45,10 @@ person can tune by hand:
* loopguard_nudges -- redirects per prompt-turn before the guard only notifies;
* loopguard.<name> -- one guard threshold, pinned over the preset and over what the
repository's history learned (LOOPGUARD_KEYS);
- * repomap, repomap_tokens, repomap_python, repomap_timeout -- the opt-in repository map.
+ * repomap, repomap_tokens, repomap_python, repomap_timeout -- the opt-in repository map;
+ * pi_extensions, pi_cli -- on Pi, the extensions the plugin's own model calls load (a
+ custom provider's, whose model they could not reach otherwise;
+ comma-separated paths), and the `pi` they run.
The names before the mechanisms were renamed are read too, so an existing config file keeps
working: `inject` (microskills), `untool` (toolprune), `spiral` (loopguard), with their
@@ -161,6 +164,8 @@ class LiveConfig:
repomap_python: str = ""
repomap_timeout: int = 8
paid_model: str = ""
+ pi_extensions: list = field(default_factory=list)
+ pi_cli: str = ""
shadow: bool = False
vocabulary: str = "auto"
warnings: list = field(default_factory=list)
@@ -270,7 +275,8 @@ _ENV = {"TE_MODE": "mode", "TE_SHADOW": "shadow",
"TE_LOOPGUARD": "loopguard", "TE_LOOPGUARD_AGGR": "loopguard_aggr",
"TE_LOOPGUARD_NUDGES": "loopguard_nudges",
"TE_REPOMAP": "repomap", "TE_REPOMAP_TOKENS": "repomap_tokens",
- "TE_REPOMAP_PYTHON": "repomap_python", "TE_REPOMAP_TIMEOUT": "repomap_timeout"}
+ "TE_REPOMAP_PYTHON": "repomap_python", "TE_REPOMAP_TIMEOUT": "repomap_timeout",
+ "TE_PI_EXTENSIONS": "pi_extensions", "TE_PI_CLI": "pi_cli"}
_ENV_THRESHOLDS = ("TE_SPIRAL_THRESHOLDS", "TE_LOOPGUARD_THRESHOLDS")
diff --git a/tokeneconomizer/plugin/sidecall.py b/tokeneconomizer/plugin/sidecall.py
index a6ea89c..d13cd9d 100644
--- a/tokeneconomizer/plugin/sidecall.py
+++ b/tokeneconomizer/plugin/sidecall.py
@@ -25,7 +25,9 @@ so no extra API key is needed and the call is billed where the session is:
* Pi: `pi --print --mode json` with nothing loaded but the model -- no tools, extensions,
skills, prompt templates, context files, MCP servers or saved session. With no `paid_model`
it runs on the person's own default provider and model, under whatever login Pi holds for
- it (a subscription signed in with `/login`, or an API key).
+ it (a subscription signed in with `/login`, or an API key). A model whose provider an
+ extension registers needs that extension loaded (`pi_extensions`); `pi_cli` names the `pi`
+ to run, where the first on PATH is a wrapper the calls should not go through.
* opencode: `opencode run --format json` in a throwaway directory, on the person's default
model, with `bash`, `edit`, `webfetch` and `websearch` denied, no other plugin (`--pure` on
@@ -221,11 +223,23 @@ PI_FLAGS = ("--print", "--mode", "json", "--no-session", "--no-tools", "--no-ext
"--no-skills", "--no-prompt-templates", "--no-context-files", "--offline")
-def pi_call(prompt, model="", cli="pi", timeout=TIMEOUT_S):
+def pi_extension_args(extensions):
+ """`--extension <path>` for each extension a side call loads despite `--no-extensions` (Pi
+ loads explicit ones still): a provider registered by an extension -- a company gateway, a
+ package's -- has no model in the call otherwise. Paths are made absolute, since the call runs
+ in a throwaway directory; `builtin:<name>` is passed as it is. `--no-tools` keeps any tools
+ such an extension adds out of the call."""
+ return [a for p in extensions or () for a in (
+ "--extension",
+ p if p.startswith("builtin:") else os.path.abspath(os.path.expanduser(p)))]
+
+
+def pi_call(prompt, model="", cli="pi", timeout=TIMEOUT_S, extensions=()):
model_args = ["--model", model] if model else []
+ ext_args = pi_extension_args(extensions)
with tempfile.TemporaryDirectory(prefix="te-side-") as cwd:
for extra in (["--no-mcp"], []):
- cmd = [cli, *PI_FLAGS, *extra, *model_args, "--", prompt]
+ cmd = [cli, *PI_FLAGS, *extra, *ext_args, *model_args, "--", prompt]
out = subprocess.run(cmd, capture_output=True, encoding="utf-8", errors="replace",
timeout=timeout, env=_env(), cwd=cwd,
stdin=subprocess.DEVNULL)
@@ -374,7 +388,10 @@ def runner_for(host_name, config):
when micro-skills is on, the loop guard's judge when the guard is."""
model = config.paid_model
if host_name == "pi":
- return lambda prompt, _m="agent": pi_call(prompt, model=model)
+ cli = os.path.expanduser(config.pi_cli) if config.pi_cli else "pi"
+ extensions = list(config.pi_extensions)
+ return lambda prompt, _m="agent": pi_call(prompt, model=model, cli=cli,
+ extensions=extensions)
if host_name == "opencode":
return lambda prompt, _m="agent": opencode_call(prompt, model=model)
if host_name == "codex":