Status: done
Move round statistics to structured backend events for token counts, cached tokens, TTFT, elapsed time, and per-turn summaries.
The original requirement calls for chat-window round statistics: token count, cache hits, TTFT, and total elapsed time. The MVP UI calculates TTFT/elapsed in the browser and only displays usage chunks. That is useful visually, but not enough for reproducible Agent debugging. Backend-emitted stats should become part of the WebSocket event stream.
This todo covers:
DebugRuntime.run().round_stats WebSocket messages.usage items.round_stats events.This todo does not cover:
cached_tokens.src/agent_lab/application/runtime.pysrc/agent_lab/presentation/static/app.jstests/test_debug_runtime.py and tests/test_websocket_api.py.docs/plans/todos.md.Add an injectable monotonic clock to DebugRuntime:
Clock = Callable[[], float]
DebugRuntime(chat_client, queues=None, registry=None, clock=time.perf_counter)
Track one round_index per ChatAgent call. For each round:
started_at = clock();ttft_ms if unset;prompt_tokens, completion_tokens, total_tokens, and cached_tokens;after the ChatAgent stream completes, emit:
{
"type": "round_stats",
"round_index": 1,
"ttft_ms": 123,
"elapsed_ms": 456,
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30,
"cached_tokens": 5,
"had_event": true
}
Keep existing usage messages for compatibility, but the UI should prefer round_stats for the stats panel. If no token arrives, ttft_ms can be null.
round_stats is emitted after a model turn;DebugRuntime to collect and emit stats through the existing output queue.app.js to update stats from round_stats messages and keep current browser timing only as a fallback before stats arrives.round_stats handling.Run:
uv run pytest tests/test_debug_runtime.py tests/test_websocket_api.py
uv run pytest
If successful, commit implementation files only with:
git commit -m "feat: emit structured round stats"
Result:
uv run pytest tests/test_debug_runtime.py tests/test_websocket_api.py passed: 23 tests, 1 existing Starlette deprecation warning.uv run pytest passed: 28 tests, 1 existing Starlette deprecation warning.round_stats messages with round index, TTFT, elapsed time, token totals, cached tokens, and event presence.round_stats and keeps browser-side timing as a fallback.