1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! Whether a failed subscription call means the Claude plan is OUT OF USAGE (its five-hour or weekly window is
//! exhausted until a reset), as opposed to a transient per-minute 429 or an overload. Only the former may route
//! the call to API credits ([`super::fallback`]); a transient 429 or 529 keeps the existing retry behaviour.
//!
//! Signals, strongest first:
//! - Claude Code's own `rate_limit_event` with `rate_limit_info.status == "rejected"` (and `resetsAt`, epoch
//! seconds or ms). Recorded shape: `packages/ai/test/fixtures/fake-claude.mjs` (`rate_limit` scenario) and
//! `packages/coding-agent/test/fixtures/fake-claude-agent.mjs` (`USAGE_LIMIT`), read by `classifyFailure` in
//! `packages/ai/src/api/claude-code-cli.ts`.
//! - The upstream's `anthropic-ratelimit-unified-status: rejected` header on the relayed answer, with
//! `anthropic-ratelimit-unified-reset` (epoch seconds). ASSUMPTION: not recorded anywhere in this repository; it is
//! the header Claude Code derives its `rate_limit_event` from. `rejected` is unambiguous, so it is matched as is.
//! - A 429 (`rate_limit` native code or relayed status 429) whose text says the plan's limit is used up:
//! `usage limit`, `limit reached`, `out of (extra) usage` (the TypeScript `classifyFailure` pattern; the
//! recorded CLI text `Claude AI usage limit reached|<epoch>`), or `hit your ... limit` (the recorded native text
//! `You've hit your session limit ยท resets 3:45pm` of the Hermes plugin tests). Without a 429 no text counts, so a
//! tool or prompt that merely mentions a limit never flips routing.
use ClaudeError;
use Value;
/// What the failed call reported besides its error text.
/// Epoch seconds or milliseconds as milliseconds (the `resetText` rule of `claude-code-cli.ts`).
/// `Claude AI usage limit reached|1790700600`: the reset after the pipe.
/// `Some(reset)` when `error` is the plan's usage limit (`reset` in epoch ms when reported), else `None`.
/// `error` as [`ClaudeError::UsageLimit`] when it is the plan's usage limit; unchanged otherwise.