Track LLM token usage in reports#244
Conversation
Signed-off-by: Jacob <lecoqjacob@gmail.com>
3bb9d24 to
622ab7d
Compare
rng1995
left a comment
There was a problem hiding this comment.
[Automated SkillSpector Review]
Requesting changes. Usage capture works for LLMAnalyzerBase, but the report still undercounts the real TP4 graph LLM call. Instrument the direct chat_completion() path and add a regression asserting provider usage is included in metadata.llm_usage.
| ): | ||
| result = node(state) | ||
| assert result["llm_call_log"] == [{"node": "mcp_tool_poisoning", "ok": True, "error": None}] | ||
| assert result["llm_call_log"] == [llm_call_record("mcp_tool_poisoning", ok=True)] |
There was a problem hiding this comment.
Blocking: this expectation bakes in zero tokens for TP4 even though production mcp_tool_poisoning.py calls chat_completion() directly and records a successful LLM call. That path bypasses the new raw-response usage extraction, so report totals are incomplete. Instrument chat_completion/TP4 and assert the provider's nonzero usage metadata here.
There was a problem hiding this comment.
Fixed in 8b45e81. chat_completion_with_usage() now preserves normalized provider token metadata, TP4 forwards it into llm_call_log (including parse failures), and the regression verifies the nonzero TP4 usage reaches metadata.llm_usage. Verification: Ruff clean; 1262 tests passed.
Motivation
rawpayload).Description
LLMCallRecordandllm_call_record()defaults:input_tokens,output_tokens, andtotal_tokens.with_structured_output(..., include_raw=True)and unwrap{"raw","parsed","parsing_error"}responses, extracting usage fromraw.usage_metadata(supportsinput_tokens/output_tokensandprompt_tokens/completion_tokensvariants) and preserving parsing-error behavior.run_batches) and async (arun_batches) paths (including non-structured raw responses), and propagateanalyzer.llm_usageinto node-levelllm_call_logentries (success and failure).llm_call_logtoken counters into report JSON metadata undermetadata.llm_usagewhile keeping existing metadata intact.include_raw=Trueso local/CLI providers exposerawusage without changing parser expectations.include_raw=Trueparsing, and metadata aggregation; closes feat: expose LLM token usage in JSON report output #242.Testing
ruff format .andruff check .with no issues.pytest -q: all tests passed (1261 passed, 12 skipped, 34 deselected, 6 xfailed).mypy srcwas executed and reported pre-existing typing issues in unrelated modules; no new type regressions introduced by these changes (mypy warnings are repository pre-existing and not caused by this PR).