| name | usage-tracker-1-basic-usage-logging |
| description | Sub-skill of usage-tracker: 1. Basic Usage Logging (+1). |
| version | 1.0.0 |
| category | _core |
| type | reference |
| scripts_exempt | true |
1. Basic Usage Logging (+1)
1. Basic Usage Logging
Pipe-delimited log format for easy parsing:
#!/bin/bash
USAGE_LOG="${HOME}/.workspace-hub/usage.log"
USAGE_DIR="$(dirname "$USAGE_LOG")"
mkdir -p "$USAGE_DIR"
if [[ ! -f "$USAGE_LOG" ]]; then
cat > "$USAGE_LOG" << EOF
# Usage Log
# Format: TIMESTAMP|CATEGORY|ITEM|VALUE|METADATA
# Created: $(date)
EOF
fi
log_usage() {
local category="$1"
local item="$2"
local value="${3:-1}"
local metadata="${4:-}"
local timestamp=$(date '+%Y-%m-%d_%H:%M:%S')
echo "${timestamp}|${category}|${item}|${value}|${metadata}" >> "$USAGE_LOG"
}
log_usage "model" "opus" "1" "task:architecture"
log_usage "model" "sonnet" "1" "task:implementation"
log_usage "api" "requests" "100" "endpoint:/data"
log_usage "tokens" "input" "2500" "model:opus"
2. Usage Aggregation
Aggregate usage by time period:
#!/bin/bash
get_usage_period() {
local period="$1"
local category="${2:-}"
local filter_date
case "$period" in
today)
filter_date=$(date +%Y-%m-%d)
;;
week)
filter_date=$(date -d '7 days ago' +%Y-%m-%d)
;;
month)
filter_date=$(date -d '30 days ago' +%Y-%m-%d)
;;
*)
filter_date=$(date +%Y-%m-%d)
;;
esac
if [[ -n "$category" ]]; then
grep "$filter_date" "$USAGE_LOG" 2>/dev/null | grep "|${category}|" || true
else
grep "$filter_date" "$USAGE_LOG" 2>/dev/null || true
fi
}
count_by_category() {
local period="$1"
get_usage_period | \
awk -F | \
-k2 -nr
}
() {
period=
category=
get_usage_period | \
awk -F | \
-k2 -nr
}
() {
period=
category=
get_usage_period | \
awk -F
}