Skip to content

[Platform] Standardise token usage #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

junaidbinfarooq
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
Docs? yes
Issues #208
License MIT

Changes proposed:

The PR aims to standardize the token usage extraction and its availability in the AI bundle. Essentially, the following is done:

  • Makes token usage explicit
  • Adds support to automatically register token usage extractors so that the token usage information is available in the result when a model is invoked
  • Makes the token usage auto-registration configurable

@carsonbot carsonbot added Feature New feature Platform Issues & PRs about the AI Platform component Status: Needs Review labels Aug 13, 2025
$result = $agent->call($messages, [
'max_tokens' => 500, // specific options just for this call
]);

$metadata = $result->getMetadata();
if (null === $tokenUsage = $result->getTokenUsage()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if you mean null === $tokenUsage, when token_usage is marked false in the config, as far as I can see.

final class TokenUsage implements \JsonSerializable
{
public function __construct(
public ?int $promptTokens = null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public ?int $promptTokens = null,
public ?int $prompt = null,

Maybe remove all the Token suffix here? They feel superfluous

Copy link
Contributor Author

@junaidbinfarooq junaidbinfarooq Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem.
I would not have kept the suffix if the object were simply named something like Tokens, but I thought TokenUsage is a better name. Or perhaps the DTO should be named as Tokens.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be discussed with @chr-hertel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did remove the suffixes BTW.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, not sure now that this was the right decision 😅

@junaidbinfarooq junaidbinfarooq force-pushed the feat/standardize-token-usage branch 2 times, most recently from 7a34c26 to d771023 Compare August 13, 2025 21:33
@junaidbinfarooq junaidbinfarooq force-pushed the feat/standardize-token-usage branch from d771023 to 75e98c8 Compare August 13, 2025 21:43
- Makes token usage explicit
- Adds support to automatically register token usage extractors so that the token usage information is available in the result when a model is invoked
- Makes the token usage auto-registration configurable
Copy link
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but we need to slim this down a bit - in general smaller PRs, that build on top of each other are favorable to me. so let's break it down like you did:

Makes token usage explicit
I like having the value object, we should definitely have that 👍
But I'm not in favor of having the getTokenUsage() directly on the result since the result is a high lever abstraction and token usage is quite specific to remote inference of GenAI models - not all models have that. let's keep it in the metadata please.

Adds support to automatically register token usage extractors so that the token usage information is available in the result when a model is invoked
That's a bit misleading since token usage was already sitting on an extension point => OutputProcessor and your design decision to use that extension point to add another extension point doesn't really resonate with me. why is the new extension point needed? it feels like we're just adding them because we can.

Makes the token usage auto-registration configurable
For me this is too early, i don't think that a use-case, do you? how often do we see the case for someone to bring their own token usage extractor.


all in all, let's start here please with the TokenUsage value object in the metadata class - that's an easy step we see the same picture 👍

edit: another PR could be to fix that issue #208 by registering the corresponding output processor based on the config setting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature Platform Issues & PRs about the AI Platform component Status: Needs Work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants