Pass AI token usage data to the events

Created on 14 August 2025, 19 days ago

Problem/Motivation

In the issue Abstract token usage Active , we implemented getters and setters for the token usage:

  public function getTotalTokenUsage(): ?int;
  public function getInputTokenUsage(): ?int;
  public function getOutputTokenUsage(): ?int;
  public function getCachedTokenUsage(): ?int;

But added them only to the class ChatOutput and that's it.

And after receiving this information in the plugin ProviderProxy - it just disappears!

So, we need to pass the token usage data from the provider to the events, at least, to the PostGenerateResponseEvent and PostStreamingResponseEvent.

Proposed resolution

The easiest way is to just pass each value as a parameter to the PostGenerateResponseEvent, something like this:

    // Invoke the post generate response event.
    $post_generate_event = new PostGenerateResponseEvent(
      $event_id,
      $this->plugin->getPluginId(),
      $operation_type,
      $this->plugin->configuration,
      $arguments[0],
      $arguments[1],
      $response,
      $this->plugin->getTags(),
      $this->plugin->getDebugData(),
      $pre_generate_event->getAllMetadata(),

      // Pass the token usage data.
      $this->plugin->getTotalTokenUsage(),
      $this->plugin->getInputTokenUsage(),
      $this->plugin->getOutputTokenUsage(),
      $this->plugin->getReasoningTokenUsage(),
      $this->plugin->getCachedTokenUsage(),
    );

But the number of the constructor parameters is already huge! So adding more parameters, especially one more separate parameter for just an integer, is not so good, from my point of view.

Because all the token usage numbers usually come simultaneously, I think it'll be a good idea to introduce a DTO class handling all this, and pass it as an argument.

What do you think about this way of moving forward?

Remaining tasks

Feature request
Status

Active

Version

1.2

Component

AI Core module

Created by

🇦🇲Armenia murz Yerevan, Armenia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024