Skip to content

Total token metrics at the end of the run does not reflect all the tokens used #2310

Description

@lironsam

Problem Description

I'm running an agent using OpenAI's GPT-4o model along with a Google Search tool. During execution, my agent may perform multiple searches, which results in multiple calls to the language model.

While debugging, I can see the input/output tokens for each step. However, at the end of the process, when I try to display the total input and output tokens used, it does not include all previous tokens that the agent has consumed across multiple steps.

Current Implementation

I'm retrieving metrics using:

response = self.agent.run(message=message, show_full_reasoning=self.debug)
print("METRICS: ", response.metrics)

However, the response.metrics only reflects the most recent step and does not accumulate token usage over the entire process.

Proposed Solution

It would be helpful to have a session-wide history of token usage that keeps track of all intermediate calls made by the agent.

A few possible solutions:

  • Implement a cumulative metrics tracker at the agent/session level that aggregates tokens across all calls.
  • Extend SessionMetrics to persist historical token data so that the total cost can be estimated accurately.
  • Provide an API method like agent.get_total_metrics() that returns the sum of all tokens consumed during an agent's lifecycle.

This would allow users to easily compute the total fees incurred when running multi-step agents.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions