You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 017e585
Browse filesBrowse the repository at this point in the historyBrowse files
fix(cli): scope squad cost to the shell's Squad session (#2348)
Plain `squad cost` read usage from the ledger, which only the
interactive shell writes, but took the session start from the newest
Scribe session log in .squad/log/, which the shell never writes. With
no log it reported every session; a log written by another host during
a shell run hid that run's earlier turns.
- the ledger records get an optional squadSessionId; the shell stamps
turn starts and usage with its persisted session id (the one /resume
restores);
- squad cost scopes to the Squad session of the newest ledger row, and
falls back to the session-log cutoff when that row has no id;
- the summary counts Squad sessions instead of SDK sessions;
- cost-tracking.md describes the ledger and shows the real output.
USAGE_LEDGER_VERSION stays 1; older readers ignore the new field.
Closes#2347
Co-authored-by: Tamir Dresher <tamir.dresher@gmail.com>
`squad cost` now finds the current session from the usage ledger instead of Scribe's session logs. The interactive shell stamps every usage record with its session id (the one `/resume` restores), as a new optional `squadSessionId` field, and plain `squad cost` reports the records that share the newest one. Before, the boundary was the newest `.squad/log/*.md` file, which the shell never writes: with no session log, plain `squad cost` reported every session, and a log written by another host during a shell run hid that run's earlier turns. The summary also counts Squad sessions instead of SDK sessions, so one shell run with three agents no longer shows as four sessions. Ledgers written before this change fall back to the old cutoff, and older CLIs still read the new records.
Copy file name to clipboardExpand all lines: docs/src/content/docs/features/cost-tracking.md
+27-22Lines changed: 27 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,52 +1,57 @@
1
1
# Token Usage & Cost Tracking
2
2
3
-
Squad can track token usage and estimated cost for each agent spawn, roll that data up by session, and expose it through orchestration logs, terminal summaries, and telemetry backends.
3
+
Squad records token usage and estimated cost for each turn in the interactive shell, summarizes it by session with `squad cost`, and can export token metrics to telemetry backends.
4
4
5
5
---
6
6
7
7
## Overview
8
8
9
-
-Squad tracks token usage (input/output tokens) and estimated cost per agent spawn
10
-
-Usage data is recorded in orchestration logs and available via `squad cost` CLI
9
+
-The interactive shell (`squad` with no arguments) records input tokens, output tokens and estimated cost for every coordinator and agent turn
10
+
-`squad cost` reports that usage for the current session, for every session, or for one agent
11
11
- Optional budget limits can be configured per agent or per session
12
12
13
13
---
14
14
15
15
## How It Works
16
16
17
-
- The `CostTracker` class (`packages/squad-sdk/src/runtime/cost-tracker.ts`) accumulates token data
18
-
- Each orchestration log entry includes a **Token usage** row
17
+
- Each shell turn appends JSON Lines records to `.squad/log/usage.jsonl`: one when the turn starts and one with its usage when it completes
18
+
- Each record names the SDK session that ran the turn and the Squad session it belongs to. The Squad session is the shell session, the one `/resume` restores, so it covers the coordinator and every agent the shell talks to
19
+
- Hosts that run the coordinator themselves, such as Copilot CLI and VS Code, don't write this ledger
20
+
- The `CostTracker` class (`packages/squad-sdk/src/runtime/cost-tracker.ts`) accumulates token data for telemetry
19
21
- OTel metrics (`squad.tokens.input`, `squad.tokens.output`, `squad.tokens.cost`) are emitted when telemetry is enabled
20
22
21
-
The orchestration log template stores usage in a markdown table row like this:
22
-
23
-
```md
24
-
|**Token usage**| 12,450 in / 3,200 out — $0.0234 |
25
-
```
23
+
When the ledger has no data, `squad cost` falls back to older orchestration log entries that contain a **Token usage** row and labels the totals as estimates.
26
24
27
25
---
28
26
29
27
## Viewing Costs
30
28
31
29
```bash
32
30
squad cost # current session costs
33
-
squad cost --all # all historical costs
34
-
squad cost --agent fenster # costs for specific agent
31
+
squad cost --all # all recorded costs
32
+
squad cost --agent fenster #one agent's costs in the current session
35
33
```
36
34
35
+
The current session is the Squad session of the newest record in the ledger. If that record has no Squad session because an older Squad wrote it, `squad cost` counts the records after the newest timestamped session log in `.squad/log/` instead, or all records when there is none.
0 commit comments