Skip to content

Commit f8f59b4

Browse files
committed
fix(agents): carry credits-required recovery facts through the rate-limit digest
The pinned Agent SDK adds canUserPurchaseCredits and hasChargeableSavedPaymentMethod to rate_limit_event for credits_required rejections. Keep both so a billing failure tells the operator whether buying credits or a saved card can recover the turn.
1 parent b28d410 commit f8f59b4

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎src/agents/cli-output-contracts.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export type CliSubscriptionRateLimit = {
4545
overageDisabledReason?: string;
4646
isUsingOverage?: boolean;
4747
errorCode?: string;
48+
/** Set with `credits_required`: whether buying credits or a saved card can recover the turn. */
49+
canUserPurchaseCredits?: boolean;
50+
hasChargeableSavedPaymentMethod?: boolean;
4851
};
4952

5053
/** Normalized result from a CLI-backed model provider turn. */

‎src/agents/cli-output-records.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ const claudeCliRateLimitRecordSchema = z.object({
523523
overageDisabledReason: z.string().optional(),
524524
isUsingOverage: z.boolean().optional(),
525525
errorCode: z.string().optional(),
526+
canUserPurchaseCredits: z.boolean().optional(),
527+
hasChargeableSavedPaymentMethod: z.boolean().optional(),
526528
unifiedWindows: z
527529
.record(
528530
z.string(),

‎src/agents/cli-runner/log.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export function formatCliSubscriptionRateLimitDigest(rateLimit: CliSubscriptionR
3838
if (rateLimit.errorCode) {
3939
parts.push(`error=${rateLimit.errorCode}`);
4040
}
41+
if (rateLimit.canUserPurchaseCredits !== undefined) {
42+
parts.push(`canPurchaseCredits=${rateLimit.canUserPurchaseCredits}`);
43+
}
44+
if (rateLimit.hasChargeableSavedPaymentMethod !== undefined) {
45+
parts.push(`savedPaymentMethod=${rateLimit.hasChargeableSavedPaymentMethod}`);
46+
}
4147
if (rateLimit.isUsingOverage) {
4248
parts.push("usingOverage");
4349
}

‎src/agents/cli-runner/output-error.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vitest";
22
import type { CliOutput, CliSubscriptionRateLimit } from "../cli-output-contracts.js";
3+
import { parseClaudeCliRateLimit } from "../cli-output-records.js";
34
import { formatCliSubscriptionRateLimitDigest } from "./log.js";
45
import { createCliOutputFailoverError } from "./output-error.js";
56

@@ -53,6 +54,34 @@ describe("createCliOutputFailoverError", () => {
5354
expect(error?.rawError).toBe(BASE_ERROR);
5455
});
5556

57+
it("carries credits-required recovery facts from the stream event into the billing error", () => {
58+
const rateLimit = parseClaudeCliRateLimit({
59+
backend: { command: "claude", output: "jsonl", jsonlDialect: "claude-stream-json" },
60+
providerId: "claude-cli",
61+
parsed: {
62+
type: "rate_limit_event",
63+
rate_limit_info: {
64+
status: "rejected",
65+
errorCode: "credits_required",
66+
canUserPurchaseCredits: true,
67+
hasChargeableSavedPaymentMethod: false,
68+
},
69+
},
70+
});
71+
const output: CliOutput = { text: "", errorText: BASE_ERROR, diagnostics: { rateLimit } };
72+
73+
const error = createCliOutputFailoverError({
74+
output,
75+
provider: "claude-cli",
76+
model: "claude-sonnet-4-6",
77+
});
78+
79+
expect(error?.reason).toBe("billing");
80+
expect(error?.message).toBe(
81+
`${BASE_ERROR} Subscription rate limit: rateLimit=rejected error=credits_required canPurchaseCredits=true savedPaymentMethod=false.`,
82+
);
83+
});
84+
5685
it("does not append facts to unrelated failures", () => {
5786
const output: CliOutput = {
5887
text: "",

0 commit comments

Comments
 (0)