AgentStep.llm_usage is never assigned, so the console can never show per-step token usage

Author: sxh313Created Sep 28, 2026Updated Sep 28, 2026

What I see

The per-step table that SimpleCLIConsole prints always shows the running "Total Tokens" row, and never the per-step "Token Usage" row — even though the model reports input/output token counts for every single step. So when a run costs 40k tokens there is no way to see which step spent them; only the cumulative figure is visible.

Why it happens

The row is written, but the field it reads is never filled in:

  • trae_agent/utils/cli/simple_console.py:96-100 renders the row from agent_step.llm_usage.
  • trae_agent/agent/agent_basics.py:56 declares llm_usage: LLMUsage | None = None on AgentStep.
  • The only code that handles usage at all is BaseAgent._update_llm_usage (trae_agent/agent/base_agent.py:291-299), which folds llm_response.usage into execution.total_tokens and never touches the step. _run_llm_step (base_agent.py:217) stores step.llm_response = llm_response but not the usage.

A repo-wide search for llm_usage returns exactly three files — the declaration above, the read in simple_console.py, and the name of _update_llm_usage. Nothing in the repository ever assigns to AgentStep.llm_usage, so the if agent_step.llm_usage: guard at simple_console.py:96 is always false and the branch is dead.

Reproduction (measured on main = e839e55, no network)

Driving a real TraeAgent whose _llm_client.chat is replaced by a stub that returns LLMResponse(usage=LLMUsage(input_tokens=1234, output_tokens=567)) on step 1 and LLMUsage(input_tokens=2000, output_tokens=800) on step 2, then calling _run_llm_step + _finalize_step for both steps:

--- step 1 ---
  llm_response.usage : LLMUsage(input_tokens=1234, output_tokens=567, ...)
  step.llm_usage     : None
  execution.total_tokens: LLMUsage(input_tokens=1234, output_tokens=567, ...)
--- step 2 ---
  llm_response.usage : LLMUsage(input_tokens=2000, output_tokens=800, ...)
  step.llm_usage     : None
  execution.total_tokens: LLMUsage(input_tokens=3234, output_tokens=1367, ...)
--- rendered console output for the two steps ---
step tables printed        : 2
per-step usage shown (2000): False
running total shown   (3234): True

The step's own usage arrives from the model, is stored on the response, and is dropped on the floor one line later.

Direction I'd take

Assign it where the response lands, so the console's existing read works:

python
step.llm_response = llm_response
step.llm_usage = llm_response.usage

I would fix the producer rather than point the console at agent_step.llm_response.usage, because AgentStep.llm_usage is the field the step record exposes — leaving it unassigned keeps it a permanently-dead public attribute. (LLMUsage only defines __add__, which returns a new object, so sharing the usage instance between the step and execution.total_tokens is not mutated by later steps.)

Related open PRs: #412 (long-term memory) and #413 (architectural overhaul) both edit inside _run_llm_step for unrelated reasons and neither assigns llm_usage.

I'll take this.