feat(coding-agent): Virtual models by mitsuhiko · Pull Request #10035 · earendil-works/pi

· GitHub

5 min read Original article ↗

@mitsuhiko changed the title Virtual models feat(coding-agent): Virtual models

Sep 28, 2026
…a physical model

Extensions register virtual models with pi.registerVirtualModel() (SDK code with createVirtualProvider()). The selection is recorded in model_change entries and restored on resume; assistant messages record the physical model and the new AssistantMessage.thinkingLevel. Agent-loop requests route in prepareRequest; summaries route before they are sized, and other direct requests route in ModelRuntime.streamSimple. Limits for compaction and context usage come from the physical model of the latest response.

Interactive mode shows the routed model in the footer, a notice when the responding model changes, and the selected and routed models in /session. The jev-router example plans on Codex Sol or Terra and switches to Luna after the first edit.
Detect virtual models by api, share latest-response lookup and routing helpers, drop the unused physical pass-through in resolveModel, remove the redundant /session model block, and tighten tests and the jev-router example.
…correctly

- Route auto-retries and overflow compact-and-retry as `retry`, reporting the
  failed request's model as `previous`, so a retry stays on the model routed
  for the turn instead of the previous turn's model.
- Resolve the branch selection with getBranchSelection(): a virtual
  model_change holds until the next one, otherwise the latest physical
  response wins. Resume uses it and skips failed routing messages, so an
  unregistered virtual model falls back to the last physical responder.
- Record the selection before a run when the branch implies another one,
  e.g. after tree navigation left the latest model_change on another branch.
Move thinking level recording into pi-ai so Models.streamSimple and the compat streamSimple record it, not just the coding-agent runtime. Defer recording a resume model override to the next prompt instead of writing on open. Warm the prompt cache only for requests to the selected model, which excludes routed virtual model requests and extension redirects.

@mitsuhiko

…er state

Virtual models are kept apart from providers and overlaid onto any provider's catalog, so a provider can list several virtual models next to physical ones. pi.unregisterVirtualModel() and ModelRuntime.registerVirtualModel() replace createVirtualProvider.

Routers receive failed retries as request.failed, separate from previous, and can return state that Pi stores on the session branch as pi.virtual-model-state entries. Requests routed to a model with a smaller context window compact first. The jev-router example keeps its phase as router state.

@mitsuhiko

The route reason came from the last message, so custom messages added after the user's prompt (before_agent_start, nextTurn) made the first request a continuation. Track whether a user message was added since the last routed request instead.
Derive the user route reason from the context instead of a session flag, inline routing into prepareRequest, always check compaction against the routed model, give direct requests no router state, and drop the redundant unrouted stream guard in ModelRuntime.
The footer already shows the routed model.
…nt loop

Stamp the requested thinking level on the final assistant message in the agent loop instead of wrapping three streamSimple paths in pi-ai. Every stream function the agent uses now records it.
…op redundant model_change entries

Direct virtual requests no longer forward apiKey, headers and env to a routed
model of another provider. Physical selections are no longer compared against
responses, which appended a model_change on every prompt while prepareRequest
redirected requests.
…rs on resume

registerVirtualModel only scheduled an availability refresh, so a provider of
only virtual models (e.g. jev/auto) was not configured yet when session restore
checked auth, and resume fell back to the default model.
…cting for compaction checks

Router state was compared with JSON.stringify, which depends on key order and
throws on values it cannot serialize. Returning request.state or undefined now
keeps the state; any other value is stored.

Compaction checks under a virtual selection projected the session again on
every request. The check between turns is skipped for virtual selections,
since prepareRequest checks the routed model, and that check reuses the
projection the request was built from. A two-request run now projects 4 times
instead of 8.

@mitsuhiko

@mitsuhiko

oskarfreye pushed a commit to draht-dev/draht that referenced this pull request

Oct 1, 2026
Cherry-picked from upstream 540e174c7. extensions/types.ts conflicted where draht's checkpoints API and getCheckpointManager runtime state sit next to upstream's new registerVirtualModel/unregisterVirtualModel API and pendingVirtualModelRegistrations/createContext state; both kept. sdk.ts restores the selection from upstream's getBranchSelection while keeping draht's restored context window, now read via existingSession.model?.contextWindow since the session context model can be null. PI_MODEL/PI_REASONING_LEVEL in the new docs became DRAHT_*, and "Pi" prose in the touched docs, comments and the jev-router example became draht; the pi.virtual-model-state custom entry type stays as a wire identifier.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

jyn514 pushed a commit to jyn514/pi that referenced this pull request

Oct 9, 2026
This adds experimental support for virtual models. A virtual model is a catalog entry that an extension registers with pi.registerVirtualModel(). It does not talk to a provider itself; for every request it picks a physical model and thinking level. The motivation is to make routing policies pluggable: plan on a strong model and implement on a cheap one, pick a model with a classifier, or move to a larger window when the context grows. Today this needs model switching by hand or hacks in prepareRequest.

The selection stays virtual: model_change records it, resume restores it, and each response records the physical model and thinking level that produced it. Router state can be stored on the session branch, so it follows /tree and forks and survives compaction. Context limits, compaction and image sizing use the physical model a request was routed to. examples/extensions/jev-router.ts shows a full router that uses the Jev classifier to pick Sol or Terra for planning and hands off to Luna after the first edit. docs/virtual-models.md describes the API.