feat(pi-ext-metered-model): @agentic-kit/pi-ext-metered-model — meter a pi session through the gateway - #1725
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 3 of the additive
@agentic-kit/*series (plan): a pi extension that makesagentic-serverthe session's model provider, so a coding-agent run lands ininference_loglike every other metered call instead of a parallel accounting scheme.No custom streaming code is needed — the gateway already speaks OpenAI's
/v1/chat/completions, one of pi's built-inapitypes, so the whole thing is a provider config plus a one-shot model selection:Three things that aren't obvious from the code:
session_start.pi.setModeltakes a resolvedModel, and the registry that resolves it (ctx.modelRegistry.find(provider, id)) is only reachable from an event context —session_startis the first one and fires before the first turn. It throws if the model is absent after registration or if pi refuses it for lack of credentials, rather than leaving the session on whatever model it had.databaseId, relative/non-httpgatewayUrl, agatewayUrlending in/v1(pi appends its own →/v1/v1/chat/completions), empty model list, and aselectModelthat isn't one of the registered ids — that last one is the failure the package exists to prevent, since it would leave pi quietly running an unmetered model.headersare merged underX-Database-Id/X-Entity-Id/X-Actor-Id, andapiKeyfalls back to therunToken(then a placeholder) purely to satisfy pi's "models need a key" check — the gateway authenticates on identity. Header trust boundary is documented in the README: off-cluster hosts must sendrunTokenand let the ingress pin identity.Cost fields default to zeros: the gateway is the pricing authority, and zeros mean "not priced client-side", not free.
This is the cloud lane only; the local/own-keys self-reporting lane is the next package (
pi-ext-usage-report).Verification
pnpm test(25 tests: identity/url/model builders directly, extension driven through a fakeExtensionAPI— no gateway or credentials needed),pnpm build,pnpm lint, andpnpm install --frozen-lockfile. Added to theagenticCI batch inrun-tests.yaml.Developed on top of the
pi-ext-run-logbranch (#1724, CI green); targetsmain.Link to Devin session: https://app.devin.ai/sessions/450ce6d6659c47759c184ae6ec19a2a8
Requested by: @pyramation