feat(pi-ext-usage-report): @agentic-kit/pi-ext-usage-report — self-report a pi session's usage to /v1/usage - #1726
Merged
Merged
Conversation
…ith pi entries stored verbatim
A pi extension that drains pi's append-only session into an @agentic-kit/run-log store — verbatim, in order, same code locally and in the cloud (only runId and the store differ). Draining is index-based because the session is append-only; the read position is keyed to the session header id so a switch/fork re-mirrors and the store's idempotency absorbs the duplicates. A drain advances only after a successful append, and store failures are rethrown into pi's dispatch by default rather than silently dropping entries.
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This was referenced Aug 14, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Package 4 of the additive
@agentic-kit/*series (planning issue #1657). The local / own-provider-key metering lane: a pi extension that turns each assistant message's usage into aPOST /v1/usageon the existing gateway, so a run whose model calls don't pass throughagentic-serverstill lands ininference_log. No gateway change — that endpoint already exists.The complement of
@agentic-kit/pi-ext-metered-model(#1725), which is authoritative because the gateway does the counting; this one is self-reported, so it is for usage visibility and reconciliation, not tamper-proof billing. The README states that boundary.Three decisions worth reading the code for:
input_tokensincludes cache tokens. pi reports{input, output, cacheRead, cacheWrite, totalTokens, cost};inputexcludes cache hits, so reporting it alone under-reports a long agent session by an order of magnitude:A turn that never reached the provider (no
usage) reports nothing rather than a row of zeros; a failed turn is reported (status: 'error', provider message aserror_type) because those tokens were spent.Dedupe on
responseId. pi can emitmessage_endtwice for one response (rewritten message, replay on resume), which would bill twice; messages without aresponseIdare reported as-is.Delivery is queued, not awaited in the handler — an HTTP round trip per assistant message would sit directly in the agent's turn latency. Failures are not lost: the first is retained and rethrown from
flush(), which also runs onsession_shutdown, so a host that shuts down cleanly fails loudly instead of silently dropping usage.onErroropts into non-fatal delivery.sinkreplaces HTTP entirely (in-process metering, a queue, a test double). Identity headers and gateway-URL validation are reused from@agentic-kit/pi-ext-metered-modelrather than re-derived, so both lanes send the sameX-Database-Id/X-Entity-Id/X-Actor-Id/bearer contract and both reject agatewayUrlending in/v1at construction.Tests: 28 across report shaping, the HTTP sink (injected
fetch), and queue/failure semantics — no gateway or credentials needed. Registered in theagenticCI shard.This branch is stacked on #1723 → #1724 → #1725, so its diff against
maincarries those packages until they merge.Link to Devin session: https://app.devin.ai/sessions/450ce6d6659c47759c184ae6ec19a2a8
Requested by: @pyramation