kiro 0.1.29: what Kiro's meteringEvents say a request cost goes on as usage.credit (#823)

Kiro gives no token counts, only what each request cost in credits: a
meteringEvent ({"unit": "credit", "usage": 0.091}) near the end of its
reply. The plugin read past it, so magpie's Usage page reckoned Kiro's
cost from estimated tokens with no cache (yetone/magpie#4328, #3761).

The credits of a request's meteringEvents are now summed (a hosted web
search's turns are one request) and said as usage.credit in the last
message_delta, streamed, or in the message's usage, as WorkBuddy's
usage.credit is said on the Chat path. A reply with none says no credit;
one Kiro says cost 0 says 0. An event in another unit, or with a usage
that isn't a number of at least 0, is left out. magpie reads the field on
/v1/messages once the host half of yetone/magpie#4328 is in; until then
it is passed over.

Run at 57a4d35e on mote (Bun 1.4.2, HOME a temp dir): bun test
packages/kiro 144 pass, 0 fail; bun scripts/check.mjs kiro ok; bun test
(whole repo) 6679 pass, 0 fail. Merged onto main e7004d93: kiro 144 pass,
check ok. CI pr run 42349117220 green, attempt 1. npm had 0.1.28.
Broken by hand, each red: meteringEvent ignored (4 tests), last event
kept instead of summed, any unit taken, a negative taken, a string usage
taken, a search's turns not summed, a search keeping only its last turn,
a stated 0 dropped.
Not tried: magpie's ledger with the host half (not merged); a real Kiro
reply with more than one meteringEvent.
This commit is contained in:
caozhenyuan
2026-11-07 15:27:27 +08:00
committed by GitHub
parent e7004d9354
commit 4200fed099
5 changed files with 60 additions and 2 deletions
+7
View File
@@ -253,6 +253,13 @@ With overage on (`overageConfiguration.overageStatus` `ENABLED`), the
month's window counts against the overage cap (`overageCap`), which is the
month's whole limit; with it off, against the plan's credits.
Each request's cost is what Kiro's `meteringEvent`s say, in credits, summed
over the request (a web search's turns included). It goes on as
`usage.credit` in the last `message_delta`, or in the message's `usage`, for
magpie's ledger (yetone/magpie#4328). Kiro gives no token counts, so the
input is reckoned from how full the context is, and the cache is unknown
(`magpie_cache_unknown`).
### Identity Center credit regions
An IAM Identity Center account has a profile, and its own month of credits
+35
View File
@@ -93,3 +93,38 @@ test("a reply whose metadataEvent counts tokens tells them, cache included", asy
expect(u).toEqual({ input_tokens: 120, output_tokens: 3, cache_read_input_tokens: 9000, cache_creation_input_tokens: 0 })
}
})
// What a request cost is the one figure Kiro gives: its meteringEvent, in
// credits (yetone/magpie#4328). It goes on as usage.credit, which magpie's
// ledger reads as the plan's credits.
test("Kiro's meteringEvent goes on as the request's credit, buffered and streamed", async () => {
for (const s of [false, true]) {
const u = await finalUsage(await ask(kiroReply(), s), s)
expect(u.credit).toBe(0.09107998018242124)
}
})
test("several meteringEvents are summed; none, or one not in credits, says no credit", async () => {
for (const s of [false, true]) {
const two = stream(
ev("assistantResponseEvent", { content: "OK" }),
ev("meteringEvent", { unit: "credit", unitPlural: "credits", usage: 0.25 }),
ev("meteringEvent", { unit: "credit", unitPlural: "credits", usage: 0.5 }),
)
expect((await finalUsage(await ask(two, s), s)).credit).toBe(0.75)
const none = stream(ev("assistantResponseEvent", { content: "OK" }), ev("metadataEvent", { stopReason: "END_TURN" }))
expect(await finalUsage(await ask(none, s), s)).not.toHaveProperty("credit")
const odd = stream(
ev("assistantResponseEvent", { content: "OK" }),
ev("meteringEvent", { unit: "token", usage: 900 }),
ev("meteringEvent", { unit: "credit", usage: "0.3" }),
ev("meteringEvent", { unit: "credit", usage: -1 }),
)
expect(await finalUsage(await ask(odd, s), s)).not.toHaveProperty("credit")
}
})
test("a request Kiro says cost nothing says credit 0", async () => {
const free = stream(ev("assistantResponseEvent", { content: "OK" }), ev("meteringEvent", { unit: "credit", usage: 0 }))
expect((await finalUsage(await ask(free, false), false)).credit).toBe(0)
})
+9 -1
View File
@@ -1542,7 +1542,9 @@ async function* events(body, model, budget) {
// kiro-cli 2.32's own trace log shows its MetadataEvent with every token
// count None, while a repeated prompt is billed about half the credits,
// so Kiro does cache and doesn't say how much
const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reported: false }
// credits: what Kiro's meteringEvents say the request cost, summed;
// undefined when it sent none
const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reported: false, credits: undefined }
let pct = 0
let said = 0
let failed = ""
@@ -1601,6 +1603,8 @@ async function* events(body, model, budget) {
}
} else if (kind === "contextUsageEvent") {
if (m.contextUsagePercentage > 0) pct = m.contextUsagePercentage
} else if (kind === "meteringEvent") {
if (Number.isFinite(m.usage) && m.usage >= 0 && (!m.unit || /^credit/i.test(m.unit))) usage.credits = (usage.credits ?? 0) + m.usage
} else if (["error", "throttlingError", "validationError", "serviceUnavailableError", "internalServerException"].includes(kind)) {
failed = exception(kind, f.payload)
break
@@ -1656,6 +1660,9 @@ const anthropicUsage = (u) => ({
: { input_tokens: u.input, output_tokens: u.output, magpie_cache_unknown: true }),
// the hosted web searches made (searching)
...(u.searches ? { server_tool_use: { web_search_requests: u.searches } } : {}),
// what Kiro said the request cost in credits (its meteringEvents), as
// magpie reads a plan's credits (yetone/magpie#4328)
...(u.credits !== undefined ? { credit: u.credits } : {}),
})
// quotaWords are magpie's (internal/gateway/fallback.go): how a vendor says
@@ -2155,6 +2162,7 @@ async function* searching(creds, auth, req, model, budget, signal, hosted, first
usage.cacheRead += stop.usage.cacheRead
usage.cacheWrite += stop.usage.cacheWrite
usage.reported ||= stop.usage.reported
if (stop.usage.credits !== undefined) usage.credits = (usage.credits ?? 0) + stop.usage.credits
for (const b of said)
if (b.type === "tool_use")
try {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-kiro-auth",
"version": "0.1.28",
"version": "0.1.29",
"description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
"type": "module",
"main": "./opencode.mjs",
+8
View File
@@ -154,6 +154,14 @@ test("a search the model asks for is Kiro's MCP web_search, its pages given back
// kiro-cli's agent adds __tool_use_purpose to its tools: the model may say
// why it searches. That is kiro-cli's, not Anthropic's: the caller's
// server_tool_use has the query alone, and only the query is searched for.
test("a search's turns are one request: their credits are summed", async () => {
const metered = (n) => ev("meteringEvent", { unit: "credit", unitPlural: "credits", usage: n })
const s = kiro({ turns: [[say("Let me look. "), use("tu1", "web_search", { query: "magpie release" }), metered(0.25)], [say("Magpie 2 is out."), metered(0.5)]] })
const m = await (await ask({ tools: [SEARCH] })).json()
expect(s.kiro.length).toBe(2)
expect(m.usage.credit).toBe(0.75)
})
test("a call's __tool_use_purpose is neither searched for nor shown, nor sent back", async () => {
const s = kiro({ turns: [[use("tu1", "web_search", { query: "bun 2", __tool_use_purpose: "check the release" })], [say("ok")]] })
const m = await (await ask({ tools: [SEARCH] })).json()