check-session-cache.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env bash
  2. # Quick script to check cache usage for a session
  3. if [ -z "$1" ]; then
  4. echo "Usage: $0 <session-id>"
  5. echo "Example: $0 ses_507d7a4afffeTTBCw1ncvOy7IR"
  6. exit 1
  7. fi
  8. SESSION_ID="$1"
  9. MSG_DIR="$HOME/.local/share/opencode/storage/message/$SESSION_ID"
  10. if [ ! -d "$MSG_DIR" ]; then
  11. echo "Session not found: $SESSION_ID"
  12. exit 1
  13. fi
  14. echo "==================================="
  15. echo "Cache Analysis for: $SESSION_ID"
  16. echo "==================================="
  17. echo ""
  18. for msg_file in "$MSG_DIR"/*.json; do
  19. if [ ! -f "$msg_file" ]; then
  20. continue
  21. fi
  22. MSG_NAME=$(basename "$msg_file" .json)
  23. ROLE=$(jq -r '.role' "$msg_file")
  24. if [ "$ROLE" = "assistant" ]; then
  25. echo "Message: $MSG_NAME"
  26. echo "Role: $ROLE"
  27. echo "Tokens:"
  28. jq '.tokens' "$msg_file"
  29. INPUT=$(jq -r '.tokens.input' "$msg_file")
  30. OUTPUT=$(jq -r '.tokens.output' "$msg_file")
  31. CACHE_READ=$(jq -r '.tokens.cache.read' "$msg_file")
  32. CACHE_WRITE=$(jq -r '.tokens.cache.write' "$msg_file")
  33. TOTAL=$((INPUT + OUTPUT + CACHE_READ + CACHE_WRITE))
  34. echo ""
  35. echo "Total displayed: $TOTAL tokens"
  36. echo "Actual cost equiv: ~$((INPUT + OUTPUT + (CACHE_READ / 10) + (CACHE_WRITE * 125 / 100))) tokens"
  37. echo ""
  38. echo "-----------------------------------"
  39. echo ""
  40. fi
  41. done