Bryan Low

Replacing Pi with Kopi

Two months ago I replaced Codex with pi. This week pi went the same way. It's been replaced by Kopi, a coding agent I wrote on top of PiG, Michael Kinsy's Go port of pi.

The name

Kopi: coffee, in Singapore.

Another bit of Singlish after Kup, with pi hiding inside it.

The joke about coding agents these days is that the job is sending a prompt, going for a coffee, and coming back to click approve. Kopi is named after the middle step. I don't even drink coffee.

Kopi's mascot, a bag of iced kopi tied with a purple string

Kopi peng, iced kopi in a takeaway bag

The problem

Pi is small enough to change, which is why I picked it. So I kept changing it.

What I liked was the idea of a harness I could extend like Neovim, and doing that is how I found out which interactions I cared about. Subagents, plan mode and permissions could each be added on their own. How they worked together was left to me.

Plan mode is a good example. The plan-mode extensions I looked at, pi's own example included, block edits in the session they run in. Mine also had to cover pi-subagents' children, which can run as their own pi processes, so my extension ran a policy broker on a Unix socket that every child connected to for its mode and approvals.

Looking like one tool took the same kind of work. My extension redrew each plugin's rows to match.

By November my extension was around 9,000 lines, with another 17,700 lines of tests. Doing it my way meant leaning on undocumented behaviour in pi and its plugins in 14 places, and the pi version moved about ten times in two months.

Nothing told me when an upgrade broke one, so the only way to know was to check. I ended up with 140 pins, tests that read the installed code and regex-match the exact lines each override relied on.

It worked, but every pi upgrade turned into a job of its own. Twice in October a pi release also broke pi-subagents until it caught up.

Building all of that on pi is how I worked out what I want from a harness, and by now I'd rather own it.

Why not pi's SDK

Pi has an SDK, so the obvious question is why I didn't use it, keep pi's agent, write my own TUI in Node and drop the overrides.

So I went through the 14 and sorted them. 8 were the TUI: the editor, pending messages, hidden reasoning, the working row and so on. Owning the TUI would have made those go away. The other 6 had nothing to do with the TUI.

  • 4 reached into plugins, mostly pi-subagents, the third-party plugin I used for subagents
  • 1 replaced a private method on pi's AgentSession, the agent itself
  • 1 read Codex's usage limits off response headers, which I could have dropped for the usage endpoint

The AgentSession one is the worst of them. Plugins could start runs on their own, such as pi-subagents waking the main agent when a child finished, so the extension checked every run before it began. Pi swallows any error thrown from a before_agent_start handler, so instead of blocking a run there, the extension replaced one of pi's private methods.

function installManagedRun(AgentSession, guard) {
  const prototype = AgentSession.prototype
  const original = prototype._startRun
  prototype._startRun = async function (trigger, onStarted) {
    guard(this._extensionRunner.createContext())
    return original.call(this, trigger, onStarted)
  }
}

Calling ctx.abort() from the handler comes close, but compaction has already run and the prompt still lands as an aborted turn.

So a Node TUI on pi's SDK fixes 8 of the 14, and the plugin ones and the guard only go once every run starts from my own code. By then the TUI and the subagents are mine either way, and all I'm really getting from pi is the agent loop. That would have worked too.

Why PiG and Go

PiG is an independent port, not an official pi project, that checks itself against a pinned pi release. I can use PiG as a library. The agent loop, sessions, providers and login, compaction and retry, the MCP client and skill parsing are all PiG's. Kopi does the rest: the TUI, tools, subagents, permissions, the sandbox and how they fit together. It all builds into one binary, without Node.

Go also helps. Most of my backend work is in Go, so I'm more comfortable in it than in a pile of .mjs files. I also can't patch PiG the way I patched pi. Go doesn't let me replace a private method on someone else's type, so everything goes through PiG's public API, and if an upgrade renames or changes something I call, the build fails instead of a regex test.

PiGKopiBashtool callsTUIApp controllerPrint modepigruntimePermission gateToolsOS sandboxAgent loop, sessions,providers

Outside of tests, only one package, pigruntime, is allowed to import PiG, and the linter enforces it.

pig-boundary:
  files:
    - '$all'
    - '!$test'
    - '!**/internal/pigruntime/**'
  deny:
    - pkg: github.com/MichaelKinsy/PiG
      desc: only internal/pigruntime may import PiG

Inside it, PiG's events get translated into Kopi's own, so the TUI never sees a PiG type.

It's not perfectly clean. There are still a dozen or so comments in pigruntime telling me to recheck them on a PiG bump. However, they're all in one package, and the one PiG upgrade so far, 0.7 to 0.8, changed about 140 lines of Go, mostly in there.

What's built in

Pi calls itself a minimal agent harness, and its site has a list of what it didn't build: no subagents, no permission popups, no plan mode, no background bash. Those are left to extensions, and most of my extension was adding them back.

Kopi keeps pi's idea of a small core, but those are the things I won't work without, so they're built in. The rest stays small. There's no to-do tool, and the prompt Kopi ships only says what each tool is and how it works. How I like to work goes in my own instruction files.

Being built in also means they're part of the TUI, so I get to decide how each one behaves and how they look together.

Routine tool calls still fold into one line, and failures still don't.

Kopi transcript with routine tool calls folded into one line

Routine tool calls fold into one line

Kopi starts in plan mode, and the plan waits for my approval before anything gets written.

Kopi plan approval dialog

Plan approval

Subagents get their own view, so I can see what one is doing without it filling up the main conversation.

Viewing a running subagent in Kopi

Peeking at a running subagent

Workflows came across too. Say ultracode in a prompt and the agent can write a script that starts other agents and combines their results. Its progress lists every agent it started, grouped by phase, and I can open any of them to see what it's doing.

A Kopi workflow in progress

A workflow in progress

So did the approval flow. Bash runs inside an OS sandbox, Seatbelt on macOS and bwrap on Linux, so by default a sandboxed command doesn't need to ask me. The Seatbelt profile is adapted from Codex's and the Linux setup from Anthropic's sandbox-runtime. Every call, subagents' and workflow agents' included, goes through the same rules and plan mode, and one that would ask can go to a classifier model before it reaches me.

There's also MCP, skills, themes, a /usage dialog for subscription limits, and a print mode for piping a diff in.

The trial

Before deleting the pi setup I ran the same kind of trial as last time, with three runs a side instead of one. Six commits I had already shipped, 2 bug fixes, 2 small features, 1 refactor and 1 config change. Each one got the same prompt in a clean clone at the parent commit, through the old pi setup and through Kopi, same model and same effort. I pinned Kopi's subagents to the models pi had used, so every role matched too.

First check was whether it went green on tests and checks the agent never saw. Then a reviewer agent looked at each pair without knowing which was which and picked the one it would merge, once each way round.

Both went green on all 18. No run had to be thrown out, and neither asked me a single question.

On the merge pick pi did better, 7 wins to Kopi's 3 with 8 ties. A win only counted when the reviewer picked the same diff both ways round. None of it was about whether the code worked. Pi's wins were mostly it keeping the docs right, covering an edge case, or staying in scope, which fits it spending more on scouting and review.

Kopi cost 34% less and came in cheaper on every task. It made 88 top-level model calls against 119, and its subagents made 85 against 153.

It was also 32% faster, again on every task. That's from sending the prompt to the last message, without the time a plan sat waiting for approval.

Every figure is the sum of each task's median run. A few things weren't equal, on purpose, because they're part of each setup, such as their instructions and the permission classifier (low effort on pi, minimal on Kopi).

Eighteen runs a side still isn't a benchmark, and it compares my two setups more than the harnesses. Kopi passed the same checks for two thirds of the cost and time, and pi's version was a bit more often the one I'd merge. I'll take that trade.

Closing thoughts

Kopi is early and it's built around how I work, so I'm not taking feature requests. Bug reports are welcome though.

It's on GitHub if you want to take a look, and you can install it with Homebrew.

brew install keyute/tap/kopi

Credit to Michael Kinsy for PiG. Without it, Kopi would be a much bigger project.