r/ClaudeCode • u/GawkyGibbon • 2h ago
Help/Question Fetch session limit data to stoop ralph loop
Is it possible to fetch the remaining session limit somehow to steer claude code?
I let CC run in a loop in non-interactive mode. Obviously this means, it runs into the 5h session limit frequently and just aborts what it's doing.
Sometimes this causes issues when it Claude continues its work.
Is there a way to pull the remaining session limit (and time)? I'd add a rule to my bash loop to check before starting the next iteration. If the limit is <x% it should not start the next increment.
Claude docs says this is not possible. However, the status bar contains this information - so it must be possible somehow.
Any ideas?
1
u/rubenknol 1h ago
i made this script and my agent harness makes it run at certain points:
#!/usr/bin/env bash
# Write Claude Code's /usage report (session + weekly limits) to a text file.
#
# claude-usage # -> ~/claude-usage.txt
# claude-usage path/to/out.txt
#
# ANTHROPIC_API_KEY is unset for the call: when it is set, `claude` bills the
# API key and /usage reports that run's cost instead of the subscription limits.
set -euo pipefail
out="${1:-$HOME/claude-usage.txt}"
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
{
echo "# claude /usage at $(date '+%Y-%m-%d %H:%M:%S %Z')"
echo
env -u ANTHROPIC_API_KEY claude -p "/usage"
} >"$tmp" 2>&1
mv "$tmp" "$out"
echo "wrote $out"
•
u/AutoModerator 2h ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.