-
Notifications
You must be signed in to change notification settings - Fork 65
feat: Add token usage tracking to OpenAI requests #466
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
Merged
rgonalo
merged 4 commits into
Telefonica:master
from
lmcalvo:feat/add-token-usage-tracking
Apr 7, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| """ | ||
| Copyright 2026 Telefónica Innovación Digital, S.L. | ||
| This file is part of Toolium. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| """ | ||
|
|
||
| import os | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from toolium.test.utils.ai_utils.common import ( | ||
| configure_default_openai_model, # noqa: F401, fixture needed to set the OpenAI model for all tests in this module | ||
| ) | ||
| from toolium.utils.ai_utils.openai import openai_request | ||
|
|
||
|
|
||
| def _build_mock_completion(content='test response', prompt_tokens=10, completion_tokens=5, total_tokens=15): | ||
| """Build a mock OpenAI completion object with usage data""" | ||
| usage_dump = { | ||
| 'prompt_tokens': prompt_tokens, | ||
| 'completion_tokens': completion_tokens, | ||
| 'total_tokens': total_tokens, | ||
| 'completion_tokens_details': { | ||
| 'accepted_prediction_tokens': 0, | ||
| 'audio_tokens': 0, | ||
| 'reasoning_tokens': 0, | ||
| 'rejected_prediction_tokens': 0, | ||
| }, | ||
| 'prompt_tokens_details': { | ||
| 'audio_tokens': 0, | ||
| 'cached_tokens': 0, | ||
| }, | ||
| } | ||
|
|
||
| mock_usage = mock.MagicMock() | ||
| mock_usage.prompt_tokens = prompt_tokens | ||
| mock_usage.completion_tokens = completion_tokens | ||
| mock_usage.total_tokens = total_tokens | ||
| mock_usage.model_dump.return_value = usage_dump | ||
|
|
||
| mock_message = mock.MagicMock() | ||
| mock_message.content = content | ||
|
|
||
| mock_choice = mock.MagicMock() | ||
| mock_choice.message = mock_message | ||
|
|
||
| mock_completion = mock.MagicMock() | ||
| mock_completion.choices = [mock_choice] | ||
| mock_completion.usage = mock_usage | ||
| return mock_completion | ||
|
|
||
|
|
||
| @mock.patch('toolium.utils.ai_utils.openai.OpenAI') | ||
| def test_openai_request_returns_token_usage(mock_openai_class): | ||
| mock_client = mock.MagicMock() | ||
| mock_openai_class.return_value = mock_client | ||
| mock_client.chat.completions.create.return_value = _build_mock_completion( | ||
| content='hello', prompt_tokens=20, completion_tokens=10, total_tokens=30 | ||
| ) | ||
|
|
||
| response, token_usage = openai_request('system', 'user') | ||
|
|
||
| assert response == 'hello' | ||
| assert token_usage['prompt_tokens'] == 20 | ||
| assert token_usage['completion_tokens'] == 10 | ||
| assert token_usage['total_tokens'] == 30 | ||
| assert 'completion_tokens_details' in token_usage | ||
| assert 'prompt_tokens_details' in token_usage | ||
|
|
||
|
|
||
| @mock.patch('toolium.utils.ai_utils.openai.OpenAI') | ||
| def test_openai_request_returns_empty_token_usage_when_no_usage(mock_openai_class): | ||
| mock_client = mock.MagicMock() | ||
| mock_openai_class.return_value = mock_client | ||
| mock_completion = _build_mock_completion() | ||
| mock_completion.usage = None | ||
| mock_client.chat.completions.create.return_value = mock_completion | ||
|
|
||
| response, token_usage = openai_request('system', 'user') | ||
|
|
||
| assert response == 'test response' | ||
| assert token_usage == {} | ||
|
|
||
|
|
||
| @mock.patch('toolium.utils.ai_utils.openai.OpenAI') | ||
| def test_openai_request_with_response_format_returns_token_usage(mock_openai_class): | ||
| mock_client = mock.MagicMock() | ||
| mock_openai_class.return_value = mock_client | ||
|
|
||
| mock_parsed = mock.MagicMock() | ||
| mock_message = mock.MagicMock() | ||
| mock_message.parsed = mock_parsed | ||
| mock_choice = mock.MagicMock() | ||
| mock_choice.message = mock_message | ||
| mock_completion = _build_mock_completion(prompt_tokens=50, completion_tokens=25, total_tokens=75) | ||
| mock_completion.choices = [mock_choice] | ||
| mock_client.beta.chat.completions.parse.return_value = mock_completion | ||
|
|
||
| response, token_usage = openai_request('system', 'user', response_format=mock.MagicMock()) | ||
|
|
||
| assert response is mock_parsed | ||
| assert token_usage['prompt_tokens'] == 50 | ||
| assert token_usage['completion_tokens'] == 25 | ||
| assert token_usage['total_tokens'] == 75 | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not os.getenv('AZURE_OPENAI_API_KEY'), reason='AZURE_OPENAI_API_KEY environment variable not set') | ||
| def test_openai_request_returns_token_usage_with_azure(): | ||
| response, token_usage = openai_request('You are a helpful assistant.', 'Say hello.', azure=True) | ||
|
|
||
| assert isinstance(response, str) | ||
| assert len(response) > 0 | ||
| assert token_usage['prompt_tokens'] > 0 | ||
| assert token_usage['completion_tokens'] > 0 | ||
| assert token_usage['total_tokens'] > 0 | ||
| assert 'completion_tokens_details' in token_usage | ||
| assert 'prompt_tokens_details' in token_usage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.