Add two tools from the March 23 operational briefing: - src/infrastructure/claude_quota.py: SQLite-backed tracker for Claude API usage (tokens, cost, calls) per day/month. Exposes current_mode() which returns BURST / ACTIVE / RESTING based on daily spend thresholds, enabling the orchestrator to route inference requests according to the metabolic protocol (issue #972). - scripts/claude_quota_check.sh: CLI wrapper with --mode (print mode only) and --json (machine-readable) flags for quick quota inspection from the shell or CI scripts. - tests/infrastructure/test_claude_quota.py: 19 unit tests covering cost calculation, mode thresholds, store CRUD, and convenience functions. Refs #1074
67 lines
1.6 KiB
Bash
Executable File
67 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# claude_quota_check.sh — Quick CLI check of Claude API quota and metabolic mode.
|
|
#
|
|
# Usage:
|
|
# ./scripts/claude_quota_check.sh # Human-readable report
|
|
# ./scripts/claude_quota_check.sh --mode # Print current mode only (BURST/ACTIVE/RESTING)
|
|
# ./scripts/claude_quota_check.sh --json # JSON output for scripting
|
|
#
|
|
# Refs: #1074, #972
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
|
SRC="${REPO_ROOT}/src"
|
|
|
|
# Ensure we can import the project Python modules
|
|
export PYTHONPATH="${SRC}:${PYTHONPATH:-}"
|
|
|
|
MODE_ONLY=0
|
|
JSON_OUTPUT=0
|
|
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--mode) MODE_ONLY=1 ;;
|
|
--json) JSON_OUTPUT=1 ;;
|
|
-h|--help)
|
|
echo "Usage: $0 [--mode|--json]"
|
|
echo " (no flags) Human-readable quota report"
|
|
echo " --mode Print current metabolic mode only"
|
|
echo " --json JSON output for scripting"
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Unknown flag: $arg" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [[ $MODE_ONLY -eq 1 ]]; then
|
|
python3 - <<'PYEOF'
|
|
from infrastructure.claude_quota import current_mode
|
|
print(current_mode())
|
|
PYEOF
|
|
|
|
elif [[ $JSON_OUTPUT -eq 1 ]]; then
|
|
python3 - <<'PYEOF'
|
|
import json
|
|
from infrastructure.claude_quota import get_quota_store
|
|
store = get_quota_store()
|
|
today = store.today_summary()
|
|
month = store.month_summary()
|
|
print(json.dumps({
|
|
"today": today.as_dict(),
|
|
"month": month.as_dict(),
|
|
"current_mode": today.mode,
|
|
}))
|
|
PYEOF
|
|
|
|
else
|
|
python3 - <<'PYEOF'
|
|
from infrastructure.claude_quota import quota_report
|
|
print(quota_report())
|
|
PYEOF
|
|
fi
|