Skip to content

feat(pi): show Pi agents' usage on the Usage page - #1442

Merged
salman1993 merged 1 commit into
sm/pi-harnessfrom
sm/pi-usage-plugin
Oct 20, 2026
Merged

salman1993 merged 1 commit into
sm/pi-harnessfrom
sm/pi-usage-plugin

Conversation

@salman1993

Copy link
Copy Markdown
Contributor

Stacked on #1420, which adds the Pi agent type. Review that first.

Why

The Usage page shows what each agent cost, by harness. It gets its numbers from each agent type's usage hook. Claude, Codex and goose have one. Pi does not, so Pi agents are missing from the page.

Pi records every answer's token counts and cost in the agent's Pi durable storage, through pi-ai's usage. But the Pi host drops usage from each message before sending it to the app, so the app never sees those counts.

What

  • src/bundled/pi/usage.ts (new) turns the agent's Pi conversations into the Usage plugin's SessionUsage, one per Buzz conversation.
  • register gets usage:, as codex/usage.ts and goose/usage.ts are wired in. The Usage plugin does not change.
  • host.mjs keeps usage on the messages its entries request returns.

How

  • Which conversations: the agent's places, which are its Buzz conversations by channel or thread. The channel and thread root come from the conversation's key. The title comes from the first prompt's Content: line.
  • Since: a conversation is read only when its newest Buzz message is on or after since. It is then read in full, so the snapshot is complete.
  • One request per answer: each assistant message gives its responseId, model, and tokens. pi-ai's input already leaves out cached tokens, so it maps straight to Buzz's input. cacheWrite1h is kept too.
  • Cost: pi-ai's cost.total is the recorded cost. pi-ai prices a model it has no rates for at $0, so $0 counts as no recorded cost. The Usage plugin then estimates it or lists the model as unpriced.
  • Agents not running here: they report nothing. Reading opens an agent's storage, and only an agent running on this computer may hold it. This is the same rule sessions follows.

Risk

Low. Pi is new in #1420 and ships off until it is turned on in Settings → Plugins. The host change only adds a field to entries replies; the Activity tab ignores it.

Known gaps:

  • Watch and timer turns run in one-off Pi conversations that belong to no Buzz conversation, so their usage is not counted. Codex and goose also skip sessions with no Buzz channel.
  • A conversation whose newest Buzz message is before since is skipped, even if a long turn kept running past since.

Testing

  • In the desktop app (BUZZ_DEV_VIEWER= just desktop, macOS), Usage page, last 30 days, with one Pi agent (Pip) that ran 4 conversations today:

    • Before (without the usage hook): 4 agents, 43 sessions, 3 harnesses, no Pi.
    • After: 5 agents, 47 sessions, 4 harnesses. Pi shows in the daily cost chart, and Pip shows as "Pi · Claude Opus 5.5" at $2.40. Its largest thread ($2.28, 26 requests) is among the costliest sessions.
    Before After
    screenshot screenshot
  • Real storage: a copy of Pip's storage, served by the real host on Pi durable 1.3.0. All 31 answers came back with usage. readUsage turned them into 4 sessions whose costs add up to the $2.40 above.

  • Live host test on the real 1.3.0 packages: PI_DEPS=<folder pi.sh installed> bin/pnpm exec vitest run src/bundled/pi/host.live.test.ts. It now checks that answers keep their usage, and it fails with the old host.

Not checked: Linux and Windows, light theme and narrow widths of the Usage page, and the Harness filter set to Pi. The Usage page itself has no changes.

Bigger picture

This is the Usage follow-up named in #1420. The other follow-up, Harness defaults for Pi on the agent settings page, is still to come.

🤖 Generated with Claude Code

Pi agents get a `usage` hook, as Claude, Codex and goose have. For each of
the agent's Buzz conversations, it reads the conversation's Pi entries and
reports each answer's tokens, model and cost as pi-ai recorded them. The
channel and thread come from the conversation's key, the title from its
first prompt. Only agents running on this computer are read, as for their
sessions, since reading opens their storage.

The host no longer drops `usage` from the messages it sends; that was the
only place the counts were lost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Salman Mohammed <smohammed@squareup.com>
@salman1993

Copy link
Copy Markdown
Contributor Author

before:

pi-usage-before-crop

after:

pi-usage-after-crop

@salman1993
salman1993 marked this pull request as ready for review October 20, 2026 23:17
@salman1993
salman1993 requested a review from a team as a code owner October 20, 2026 23:17
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T23:24:13.474143Z — Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@salman1993
salman1993 merged commit 3e77104 into sm/pi-harness Oct 20, 2026
35 of 37 checks passed
@salman1993
salman1993 deleted the sm/pi-usage-plugin branch October 20, 2026 23:18

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/bundled/pi/usage.ts

/** A conversation's model requests, its entries oldest first. pi-ai counts a
* request's input without its cached part, and prices the models it knows. */
export function conversationUsage(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the conversion helper private

conversationUsage has no production caller outside this module; it is exported solely so the first two tests can bypass the actual readUsage boundary. This creates a test-only production API and couples coverage to an implementation detail, so behavior-preserving refactors must retain the export. Keep the helper private and exercise these conversion cases through readUsage with the existing fake request boundary instead.

AGENTS.md reference: AGENTS.md:L131-L134

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant