Skip to content

simple_agent crashes when a later model turn returns usage=None, and also double-counts first-turn usage #938

Closed

Description

@danecor

responses_api_agents/simple_agent/app.py assumes every model_response has non-null usage, but responses_api_models/vllm_model/app.py can legally return usage=None. In multi-turn runs, if an earlier turn has usage and a later turn does not, simple_agent crashes with AttributeError on model_response.usage.input_tokens.

Separately, its current aggregation logic aliases the first turn's usage object and immediately adds it to itself, double-counting the first turn.

In responses_api_agents/simple_agent/app.py:

if not usage:
    usage = model_response.usage

if usage:
    usage.input_tokens += model_response.usage.input_tokens
    usage.output_tokens += model_response.usage.output_tokens
    usage.total_tokens += model_response.usage.total_tokens

    # TODO support more advanced token details
    usage.input_tokens_details.cached_tokens = 0
    usage.output_tokens_details.reasoning_tokens = 0

In responses_api_models/vllm_model/app.py:

usage = None
if chat_completion_response.usage:
    usage = NeMoGymResponseUsage(
        input_tokens=chat_completion_response.usage.prompt_tokens,
        input_tokens_details=NeMoGymResponseInputTokensDetails(cached_tokens=0),
        output_tokens=chat_completion_response.usage.completion_tokens,
        output_tokens_details=NeMoGymResponseOutputTokensDetails(reasoning_tokens=0),
        total_tokens=chat_completion_response.usage.prompt_tokens
        + chat_completion_response.usage.completion_tokens,
    )

Observed failure:

File "/opt/nemo-gym/responses_api_agents/simple_agent/app.py", line 111, in responses
    usage.input_tokens += model_response.usage.input_tokens
AttributeError: 'NoneType' object has no attribute 'input_tokens'

I think the sequence of events here occurs during tool-use conversations, and goes something like:

  1. First model turn is normal.
  • The model sees the user prompt plus tool schema.
  • It returns a function_call.
  • That first response includes usage != None.
  1. simple_agent executes the tool and appends the tool result to conversation history.
  • The tool/MCP response is added as a function_call_output.
  • On the next turn, that becomes a role="tool" message sent back to the model.
  1. The second model call now has a larger prompt, including:
  • original user prompt
  • prior assistant tool call
  • tool output text
    If the tool output is large, or if there are multiple tool turns, the prompt grows quickly.
  1. vLLM rejects that next request for context/max-token budget reasons. The wrapper explicitly catches 400s mentioning "context length" or "max_tokens".
  2. That fallback chat completion has no usage, then responses() converts it into a NeMoGymResponse with usage=None.
  3. simple_agent crashes because it already has accumulated usage from turn 1.
  • usage is truthy from the first turn.
  • model_response.usage is now None.
  • It does model_response.usage.input_tokens and crashes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions