Skip to content

feature: Pi support — detection, settings, parsed events, token and cost accounting #1458

Closed

Description

@huang195

Feature Description

Pi (earendil-works/pi, formerly
badlogic/pi-mono) is a terminal coding agent. Cortex should support it as fully as Claude
Code, OpenCode, Codex and Bob: one agentop configure pi command to route it, its own row in
the Agents pane, its own sessions, correct tokens and cost, and a guide.

Today Pi can only run under agentop exec -- pi, which is the "any other agent" fallback.
Even there it does not work well. Read off Pi's source on main (latest release v1.3.0,
2026-10-15):

  1. Cortex does not recognise Pi. Pi's User-Agent is pi (<platform> <release>; <arch>),
    for example pi (darwin 25.6.0; arm64) (packages/ai/src/utils/pi-user-agent.ts). It has
    no /version, and core/pipeline/client.go has no entry for it, so Pi gets no agent row
    and no version.
  2. Pi on a Claude subscription looks like Claude Code. When Pi signs in to Anthropic with
    OAuth (/login with a Pro or Max plan), it sends user-agent: claude-cli/<version> and
    x-app: cli (packages/ai/src/api/anthropic-messages.ts). Cortex would then put Pi's
    traffic and spend in Claude Code's row. Pi has to be told apart from real Claude Code by
    some other signal.
  3. Pi sends no session id by default. Pi has a session id (PI_SESSION_ID, the id
    /session shows), but it sends it only as a session-affinity header: x-session-id for
    OpenRouter, and x-session-affinity, x-client-request-id and session_id in the
    openai format. These go out only when the model's compat.sendSessionAffinityHeaders
    is set, which by default is true only for OpenRouter. With direct Anthropic or OpenAI,
    Cortex has no session id and groups Pi's traffic by process instead of by conversation.
    (Separately, core/session/idheader.go, core/config/config.go and cmd/cortex/local.go
    say "Pi (Inflection AI)" sends X-Session-Id. Inflection's Pi is a different product,
    and this Pi sends that header only to OpenRouter. Fix those comments as part of this
    issue.)
  4. Some of Pi's wire formats are not parsed. Pi has an API module per provider:
    anthropic-messages, openai-completions, openai-responses, openai-codex-responses,
    azure-openai-responses, google-generative-ai, google-vertex,
    bedrock-converse-stream, mistral-conversations, and its own pi-messages. Cortex parses
    the Anthropic Messages, OpenAI Chat Completions and Responses formats. With any other
    provider, Pi's requests are proxied but not counted.
  5. Nothing routes it. Pi reads HTTP_PROXY / HTTPS_PROXY (undici's
    EnvHttpProxyAgent). It also has an httpProxy setting in ~/.pi/agent/settings.json
    that is applied as those variables at startup, and does not override them if they are
    already set (commit 9105085, for Support httpProxy in settings.json for EnvHttpProxyAgent earendil-works/pi#5790). Pi runs on Node 22.19+, so the
    CA comes from NODE_EXTRA_CA_CERTS, which Node reads only at process start, so a settings
    file cannot supply it. Pi's agent directory is ~/.pi/agent, or PI_CODING_AGENT_DIR.

Proposed Solution

The same checklist as OpenCode (#941) and Codex (#942):

  • Detect an installed Pi and locate its settings (PI_CODING_AGENT_DIR, else ~/.pi/agent)
  • agentop configure pi enable | disable | status: route Pi through localhost:47600
    and the Cortex CA. Ask before editing, refuse to overwrite a proxy someone else set,
    and support revert. The CA needs a mechanism, because httpProxy alone cannot supply it
  • Recognise Pi's User-Agent as agent pi, including in the OAuth case where it sends
    claude-cli/…, so Pi's spend never lands in Claude Code's row
  • Group Pi's traffic by Pi's own session, not by process
  • Traffic is parsed into typed events, not merely proxied, for the providers Pi is
    commonly used with. Any provider that is not parsed is listed in the guide
  • Token counts and cost are correct for each parsed wire format
  • Session and cost metrics aggregate alongside the other agents (agentop cost --by agent,
    the Agents pane, the inference-router's agent names)
  • A tool inventory for Pi, or an explicit statement that tool pruning is unsupported for it
  • Pi is covered everywhere the other agents are: agentop configure with no arguments,
    its status report, and agentop uninstall's unroute step
  • A row in the root README's Supported agents table

Required artifact: a doc for Pi

This issue does not close on code alone. The doc is a deliverable like the code: it
ships in the same PR, or in a linked PR merged before this issue closes. A PR description or
a comment here does not count.

It is docs/agents/pi.md in this repo, linked from the root README's Supported agents
table and from docs/README.md. It follows the outline of docs/agents/opencode.md and
docs/agents/codex.md:

  • Enable and revert: the agentop configure pi commands, which file each one
    writes, and how to undo it by hand
  • CA trust: what Pi needs to trust the Cortex CA, and the symptom when it does not
  • What Cortex shows for it: which events are typed, which token and cost fields are
    filled, which providers and wire formats are parsed, and what is partial or unsupported
    (e.g. tool pruning)
  • Verified depth: the Pi versions, providers and platforms actually tested, and what
    was not
  • Known issues specific to Pi

cmd/agentop/README.md can carry the command reference too; it does not replace this page.

Additional Context

Everything above was read from Pi's source and docs, not captured from a live Pi through
Cortex. The first step is to run Pi under agentop exec against Anthropic (API key and OAuth)
and OpenAI, and record its real User-Agents, headers and paths, the way the OpenCode and Codex
entries in core/pipeline/client.go were captured.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions