feat: add generation metrics to logs (think, rounds, tokens)

Log think flag, round count, prompt/output token counts per generation.
Change log category from 'usage' to 'generation' for clean MCP filtering.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-02 14:14:41 -04:00
parent 3e42992f67
commit 25d448f896
3 changed files with 21 additions and 3 deletions
@@ -212,7 +212,11 @@ async def run_generation(
t_start = time.monotonic()
timing: dict = {
"think": think,
"tools": [],
"rounds": 0,
"prompt_tokens": None,
"output_tokens": None,
"ttft_ms": None,
"generation_ms": None,
"total_ms": None,
@@ -228,6 +232,7 @@ async def run_generation(
research_completed = False
for _round in range(MAX_TOOL_ROUNDS + 1):
timing["rounds"] = _round + 1
round_tool_calls: list[dict] = []
logger.info("Generation round %d started for conv %d (model=%s)", _round, conv_id, model)
@@ -261,6 +266,12 @@ async def run_generation(
logger.warning("Failed periodic flush for message %d", msg_id, exc_info=True)
last_flush = now
elif chunk.type == "done":
if chunk.prompt_tokens is not None:
timing["prompt_tokens"] = (timing["prompt_tokens"] or 0) + chunk.prompt_tokens
if chunk.output_tokens is not None:
timing["output_tokens"] = (timing["output_tokens"] or 0) + chunk.output_tokens
elif chunk.type == "tool_calls" and chunk.tool_calls:
logger.info("Round %d: model returned %d tool call(s)", _round, len(chunk.tool_calls))
for tc in chunk.tool_calls: