Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 7, 2026

TokenUsage lacked model information, making it impossible to calculate costs accurately when using multiple models with different pricing (e.g., GPT-4o vs embeddings).

Changes

  • TokenUsageInterface/TokenUsage: Added model property and getModel() method
  • TokenUsageAggregation: Returns model name if all usages share the same model, null otherwise
  • All bridge extractors: Extract and populate model from API responses (OpenAI, Anthropic, Gemini, VertexAI, DeepSeek, Perplexity, Mistral, Ollama, Generic)
  • Tests: Added coverage for model property and aggregation behavior
  • Examples: Updated print_token_usage() function in examples/bootstrap.php to display the model property in all token usage examples

Usage

$result = $platform->request('gpt-4o', 'Hello');
$tokenUsage = $result->getMetadata()->get('token_usage');

// Model now available for cost calculation
$model = $tokenUsage->getModel(); // "gpt-4o-2024-08-06"

// Calculate costs based on actual model used
$pricing = [
    'gpt-4o' => ['input' => 2.50, 'output' => 10.00],
    'text-embedding-3-small' => ['input' => 0.02, 'output' => 0],
];

foreach ($pricing as $prefix => $prices) {
    if (str_starts_with($model, $prefix)) {
        $cost = ($tokenUsage->getPromptTokens() / 1_000_000 * $prices['input'])
              + ($tokenUsage->getCompletionTokens() / 1_000_000 * $prices['output']);
    }
}

The model parameter is optional (nullable) for backward compatibility.

Q A
Bug fix? no
New feature? yes
Docs? no
Issues Fix #1583
License MIT
Original prompt

This section details on the original issue you should resolve

<issue_title>[Platform] Token usage - information about used model</issue_title>
<issue_description>Hi, I’m trying to calculate the total cost of a request. For example, a single request may include two or three calls to the OpenAI GPT-4o model and to the embeddings model.

However, when I look at the token usage in the response:

$tokenUsage = $response->getMetadata()->get('token_usage');

I get an array of TokenUsage objects that does not contain any information about which model was used. Since each model has different pricing, how can I correctly calculate the total cost?

Is there another way to retrieve this information?</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits February 7, 2026 19:22
Co-authored-by: OskarStark <995707+OskarStark@users.noreply.github.com>
Co-authored-by: OskarStark <995707+OskarStark@users.noreply.github.com>
Co-authored-by: OskarStark <995707+OskarStark@users.noreply.github.com>
Copilot AI changed the title [WIP] Add information about used models in token usage [Platform] Add model property to TokenUsage for cost calculation Feb 7, 2026
Copilot AI requested a review from OskarStark February 7, 2026 19:25
@carsonbot carsonbot changed the title [Platform] Add model property to TokenUsage for cost calculation Add model property to TokenUsage for cost calculation Feb 7, 2026
@OskarStark
Copy link
Contributor

@copilot can you update the examples

Co-authored-by: OskarStark <995707+OskarStark@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 7, 2026

@copilot can you update the examples

Updated the examples to display the model property in the token usage output. The print_token_usage function in examples/bootstrap.php now shows the model as the first row in the token usage table. (13ba1a7)

Copilot AI changed the title Add model property to TokenUsage for cost calculation Add model property to TokenUsage for cost calculation Feb 7, 2026
@OskarStark
Copy link
Contributor

Add changelog entries to components @copilot

Co-authored-by: OskarStark <995707+OskarStark@users.noreply.github.com>
@OskarStark
Copy link
Contributor

@copilot it's for 0.4 for all components

0.3
---

* Add `model` property to `TokenUsage` and `TokenUsageInterface` to track which AI model was used for token consumption
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

0.2
---

* Add model information to token usage extraction
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

0.2
---

* Add model information to token usage extraction
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

0.2
---

* Add model information to token usage extraction
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

0.5
---

* Add model information to token usage extraction
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

0.2
---

* Add model information to token usage extraction
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

0.2
---

* Add model information to token usage extraction
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

0.2
---

* Add model information to token usage extraction
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

0.3
---

* Add model information to token usage extraction
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for 0.4 section

@OskarStark
Copy link
Contributor

@copilot

$tokenUsage = $response->getMetadata()->get('token_usage');

returns a TokenUsageAggregation object, which contains a private array of TokenUsage objects.

Each TokenUsage object should have a model property.

There is also an issue with iterating over the TokenUsage objects, since the array is private.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Platform] Token usage - information about used model

3 participants