Nutzungslimits in headless Claude-Code-Runs (claude -p) erkennen
Wer claude -p unbeaufsichtigt per Cron ausführt, kennt teure Fehler: ein abgebrochener Run wegen Usage-Limit wird einfach vom nächsten Slot erneut gestartet. Statt Text per grep zu durchsuchen, sollte die Ausgabe als JSON-Result verarbeitet werden.

Wenn man claude -p unbeaufsichtigt ausführt, ist der teuerste Fehler derjenige, bei dem ein Fehler aufgrund eines Nutzungslimits vom nächsten Cron-Slot erneut versucht wird. Der zweitärgerlichste ist das Gegenteil: stundenlanges Anhalten, weil ein erfolgreicher Lauf "rate limit" erwähnte.
Führe den Befehl mit --output-format json aus. Das finale Ergebnisobjekt enthält is_error, result und num_turns. Entscheide in dieser Reihenfolge:
Behandle zusätzlich den Fall, in dem die CLI ein Limit mit Exit-Code 0, aber is_error: true meldet.
Setze zuerst einen Stub namens claude in den PATH, der vorbereitete Ergebnisse ausgibt (ok, limit, "hit your limit" mit rc 0, Erfolg, der "rate limit" erwähnt, einfacher Absturz), und prüfe die Logdatei deines Launchers. Unser Selftest macht genau das: https://github.com/spikeyjr/agent-autopilot-kit/blob/main/ops/selftest.sh
Der vollständige Launcher (Lock, tägliches Limit, Timeout, Entfernen der API-Keys, Stoppdatum) ist MIT-lizenziert: https://github.com/spikeyjr/agent-autopilot-kit
Ich habe die Prüfungen als kostenlose Claude-Code-Skill namens cron-preflight verpackt (zahl, was du willst, 0 Dollar sind in Ordnung). Sie liest deine Crontab und deinen Launcher und meldet zu zehn typischen Fallstricken im Headless-Betrieb, ob sie bestanden oder nicht bestanden haben, einschließlich der Frage, ob eine Nutzungslimit-Meldung weitere Läufe stoppt. Der Download-Link befindet sich im README unter den optionalen Extras, neben einer kostenpflichtigen Einrichtungsanleitung und einem kostenpflichtigen Ops-Skills-Paket.
Offenlegung: Ich pflege das Repository und verkaufe die kostenpflichtigen Artikel; dieser Beitrag wurde mit KI-Unterstützung verfasst. Inoffiziell, keine affiliation mit Anthropic.
Automatisch übersetzt. Das Original auf Englisch ist unten aufklappbar.
Original (Englisch) anzeigen
If you run claude -p unattended, the most expensive bug is the one where a usage-limit failure gets retried by the next cron slot. The second most annoying is the opposite: pausing for hours because a successful run mentioned "rate limit".
Run with --output-format json. The final result object has is_error, result, and num_turns. Decide in this order:
Also handle the case where the CLI reports a limit with exit code 0 but is_error: true.
Put a stub claude first on PATH that prints canned results (ok, limit, "hit your limit" with rc 0, success that mentions "rate limit", plain crash) and assert on your launcher's log. Our selftest does exactly that: https://github.com/spikeyjr/agent-autopilot-kit/blob/main/ops/selftest.sh
The full launcher (lock, daily cap, timeout, API-key stripping, stop date) is MIT: https://github.com/spikeyjr/agent-autopilot-kit
I packaged the checks as a free Claude Code skill, cron-preflight (pay what you want, $0 is fine). It reads your crontab and launcher and reports pass/fail on ten headless pitfalls, including whether a usage-limit message stops further runs. The download link is in the README's optional extras, next to a paid setup guide and a paid ops skills pack.
Disclosure: I maintain the repo and sell the paid items; this post was drafted with AI assistance. Unofficial, not affiliated with Anthropic.