Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.19.222
1.19.223
4 changes: 3 additions & 1 deletion docs/architecture/workspaces/workspace-agent-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ no turn in the window reported it; `daily_tokens` counts only known values.

With `thread_id` it returns `since`, `until`, `days`, and that thread's
Analytics `groups` unchanged, ordered by day and hour. A thread without usage
returns an empty list. `limit` is rejected with `thread_id`.
returns an empty list. `day=YYYY-MM-DD` narrows the rows to one of the listed
`days`, which keeps a busy thread's response small; another date is rejected,
as is `day` without `thread_id`. `limit` is rejected with `thread_id`.

## One-time reminders

Expand Down
4 changes: 2 additions & 2 deletions host/bootstrap/agent-home/agents_claude.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ is required. Write and test the durable script under agent home first.
### Token usage

`GET /agent/usage` ranks threads by tokens over seven UTC days (`limit` 1–200,
default 50); `?thread_id=ID` returns that thread's hourly rows. The four
buckets are disjoint; null means unmeasured.
default 50); `?thread_id=ID` returns that thread's hourly rows, or one `day`
with `&day=YYYY-MM-DD`. The four buckets are disjoint; null means unmeasured.

## Links

Expand Down
14 changes: 11 additions & 3 deletions host/runtime/workspace/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

The default view ranks threads over the same seven UTC days so an agent can
find heavy spenders without loading every hourly row; `thread_id` returns one
thread's hourly rows unchanged. Counts are best-effort measurements.
thread's hourly rows unchanged, optionally for one `day`. Counts are
best-effort measurements.
"""

from __future__ import annotations
Expand All @@ -22,19 +23,26 @@
def route_agent(method: str, query: dict[str, list[str]]) -> dict[str, Any]:
if method != "GET":
raise WorkspaceError(HTTPStatus.BAD_REQUEST, "agent usage accepts only GET")
extra = sorted(set(query) - {"limit", "thread_id"})
extra = sorted(set(query) - {"limit", "thread_id", "day"})
if extra:
raise WorkspaceError(HTTPStatus.BAD_REQUEST, f"unexpected usage query field: {extra[0]}")
thread_id = one(query, "thread_id")
raw_limit = one(query, "limit")
day = one(query, "day")
if thread_id is not None and raw_limit is not None:
raise WorkspaceError(HTTPStatus.BAD_REQUEST, "limit applies only without thread_id")
if thread_id is None and day is not None:
raise WorkspaceError(HTTPStatus.BAD_REQUEST, "day applies only with thread_id")
if raw_limit is not None and (not raw_limit.isdigit() or not 1 <= int(raw_limit) <= MAX_LIMIT):
raise WorkspaceError(HTTPStatus.BAD_REQUEST, f"limit must be between 1 and {MAX_LIMIT}")
report = state.usage_report()
window = {key: report[key] for key in ("since", "until", "days")}
if thread_id is not None:
groups = [group for group in report["groups"] if group["thread_id"] == thread_id]
# A busy thread's week is hundreds of hourly rows; one day is at most 24 per model.
if day is not None and day not in report["days"]:
raise WorkspaceError(HTTPStatus.BAD_REQUEST, "day must be one of the report's days")
groups = [group for group in report["groups"]
if group["thread_id"] == thread_id and day in (None, group["day"])]
groups.sort(key=lambda group: (group["day"], group["hour"], group["runtime"], group["model"]))
return {**window, "groups": groups}
threads = _threads(report)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_token_usage.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading