-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcosts.sh
More file actions
executable file
·180 lines (155 loc) · 5.77 KB
/
Copy pathcosts.sh
File metadata and controls
executable file
·180 lines (155 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
set -euo pipefail
# Force C numeric locale so `bc`, `awk`, and `printf '%f'` all
# parse and format decimals with `.` regardless of the user's
# LC_NUMERIC. See dashboard.sh header for details.
export LC_NUMERIC=C
# Print cost and usage summary for swarm containers.
# Usage: ./costs.sh [--json]
SWARM_DIR="$(cd "$(dirname "$0")" && pwd)"
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
cat <<HELP
Usage: $0 [--json]
Print cost and usage summary for swarm containers
(numbered agents and the post-process container).
Options:
--json Output as JSON instead of a formatted table.
-h, --help Show this help message.
Reads stats from running or stopped agent containers via
docker cp. Requires Docker access.
HELP
exit 0
fi
source "$SWARM_DIR/lib/check-deps.sh"
check_deps git jq docker bc
# shellcheck source=lib/project.sh
source "$SWARM_DIR/lib/project.sh"
REPO_ROOT="$(git rev-parse --show-toplevel)"
PROJECT="$(swarm_project_id "$(basename "$REPO_ROOT")")"
IMAGE_NAME="${PROJECT}-agent"
JSON_MODE=false
if [ "${1:-}" = "--json" ]; then
JSON_MODE=true
fi
read_agent_stats() {
local name=$1 agent_id=$2
local stats_file="agent_logs/stats_agent_${agent_id}.tsv"
local tmpf="/tmp/.swarm-stats-${name}.tsv"
docker cp "${name}:/workspace/${stats_file}" "$tmpf" 2>/dev/null || true
if [ ! -s "$tmpf" ]; then
rm -f "$tmpf"
echo "0 0 0 0 0 0 0"
return
fi
awk -F'\t' '{
cost += $2; tok_in += $3; tok_out += $4;
cache += $5; dur += $7; turns += $9; sessions++
} END {
printf "%s %d %d %d %d %d %d\n",
cost, tok_in, tok_out, cache, dur, turns, sessions
}' "$tmpf"
rm -f "$tmpf"
}
format_tokens() {
local n=${1:-0}
if [ "$n" -ge 1000000 ]; then
printf '%.1fM' "$(echo "$n / 1000000" | bc -l)"
elif [ "$n" -ge 1000 ]; then
printf '%.0fk' "$(echo "$n / 1000" | bc -l)"
else
printf '%d' "$n"
fi
}
format_duration() {
local s=$1
if [ "$s" -ge 3600 ]; then
printf '%dh %02dm' $((s / 3600)) $(((s % 3600) / 60))
elif [ "$s" -ge 60 ]; then
printf '%dm %02ds' $((s / 60)) $((s % 60))
else
printf '%ds' "$s"
fi
}
containers=$(docker ps -a --filter "name=${IMAGE_NAME}-" \
--format '{{.Names}}' 2>/dev/null | sort -V || true)
if [ -z "$containers" ]; then
echo "No swarm containers found." >&2
exit 1
fi
total_cost=0
total_in=0
total_out=0
total_cache=0
total_dur=0
total_turns=0
total_sessions=0
json_agents="["
if ! $JSON_MODE; then
printf "%-4s %-20s %9s %10s %10s %6s %8s %8s\n" \
"#" "Model" "Cost" "Input" "Output" "Cache" "Turns" "Time"
printf "%s\n" "$(printf '%.0s─' $(seq 1 82))"
fi
first_json=true
for cname in $containers; do
agent_id="${cname##*-}"
state=$(docker inspect -f '{{.State.Status}}' "$cname" 2>/dev/null || echo "?")
env_dump=$(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' \
"$cname" 2>/dev/null || true)
model=$(printf '%s' "$env_dump" | grep '^CLAUDE_MODEL=' | head -1 | cut -d= -f2- || true)
model="${model:-unknown}"
effort=$(printf '%s' "$env_dump" | grep '^CLAUDE_CODE_EFFORT_LEVEL=' | head -1 | cut -d= -f2- || true)
short="${model/claude-/}"
if [ -n "$effort" ]; then
eff_tag="${effort:0:1}"
short="${short} [${eff_tag^^}]"
fi
stats=$(read_agent_stats "$cname" "$agent_id")
a_cost=$(echo "$stats" | awk '{print $1}')
a_in=$(echo "$stats" | awk '{print $2}')
a_out=$(echo "$stats" | awk '{print $3}')
a_cache=$(echo "$stats" | awk '{print $4}')
a_dur=$(echo "$stats" | awk '{print $5}')
a_turns=$(echo "$stats" | awk '{print $6}')
a_sessions=$(echo "$stats" | awk '{print $7}')
total_cost=$(echo "$total_cost + $a_cost" | bc)
total_in=$((total_in + a_in))
total_out=$((total_out + a_out))
total_cache=$((total_cache + a_cache))
total_dur=$((total_dur + a_dur))
total_turns=$((total_turns + a_turns))
total_sessions=$((total_sessions + a_sessions))
dur_s=$((a_dur / 1000))
if $JSON_MODE; then
$first_json || json_agents="${json_agents},"
first_json=false
id_json="${agent_id}"
if ! [[ "$agent_id" =~ ^[0-9]+$ ]]; then
id_json="\"${agent_id}\""
fi
json_agents="${json_agents}{\"id\":${id_json},\"model\":\"${model}\",\"effort\":\"${effort}\",\"state\":\"${state}\","
json_agents="${json_agents}\"cost_usd\":${a_cost},\"input_tokens\":${a_in},"
json_agents="${json_agents}\"output_tokens\":${a_out},\"cache_read_tokens\":${a_cache},"
json_agents="${json_agents}\"duration_ms\":${a_dur},\"turns\":${a_turns},\"sessions\":${a_sessions}}"
else
printf "%-4s %-20s \$%8.4f %10s %10s %6s %8s %8s\n" \
"$agent_id" "$short" "$a_cost" \
"$(format_tokens "$a_in")" "$(format_tokens "$a_out")" \
"$(format_tokens "$a_cache")" "$a_turns" "$(format_duration "$dur_s")"
fi
done
json_agents="${json_agents}]"
if $JSON_MODE; then
total_cost_json=$(printf '%.6f' "$total_cost")
printf '{"agents":%s,"total":{"cost_usd":%s,"input_tokens":%d,"output_tokens":%d,"cache_read_tokens":%d,"duration_ms":%d,"turns":%d,"sessions":%d}}\n' \
"$json_agents" "$total_cost_json" "$total_in" "$total_out" \
"$total_cache" "$total_dur" "$total_turns" "$total_sessions"
else
printf "%s\n" "$(printf '%.0s─' $(seq 1 82))"
t_dur_s=$((total_dur / 1000))
printf "%-4s %-20s \$%8.4f %10s %10s %6s %8s %8s\n" \
"" "TOTAL" "$total_cost" \
"$(format_tokens "$total_in")" "$(format_tokens "$total_out")" \
"$(format_tokens "$total_cache")" "$total_turns" "$(format_duration "$t_dur_s")"
printf "\n%d agents, %d sessions, \$%.4f total cost\n" \
"$(echo "$containers" | wc -l)" "$total_sessions" "$total_cost"
fi