Skip to content

Conversation

@eneufeld
Copy link
Contributor

@eneufeld eneufeld commented Apr 2, 2025

What it does

This adds logic to anthropic, openai and google providers to report token usage.
Furthermore it adds a new tab to the AiConfiguration view which shows the token usage per model.
Currently the usage is not persisted over application restart.

How to test

Open the AiConfigurationView and trigger some llm request.

Follow-ups

Breaking changes

  • This PR introduces breaking changes and requires careful review. If yes, the breaking changes section in the changelog has been updated.

Attribution

Review checklist

Reminder for reviewers

@eneufeld eneufeld requested review from JonasHelming and planger April 2, 2025 19:56
@github-project-automation github-project-automation bot moved this to Waiting on reviewers in PR Backlog Apr 2, 2025
@eneufeld eneufeld mentioned this pull request Apr 2, 2025
61 tasks
Copy link
Contributor

@planger planger left a comment

Choose a reason for hiding this comment

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

Very nice! Tested with OpenAI and Anthropic models.

Tracking Request IDs

I think it might be worth including the request id in addition to the model id, when tracking the token usage. This would allow us to show the cost of specific requests later on.

Token Usage View

The Token Usage view uses the active border color, even if it isn't active or has focus. I suggest to use a normal border color instead. As the table is not really getting focus, it should probably never use the focus colors.

Should we use date-fns to show the date relative to now (e.g. 1 week ago) and the raw date only on hover? This would feel more natural than showing the raw date. See for instance the recent chat session.

* Gets the current token usage data for all models
*/
async getTokenUsageData(): Promise<ModelTokenUsageData[]> {

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

* @param model The identifier of the model that was used
* @returns A promise that resolves when the token usage has been recorded
*/
recordTokenUsage(inputTokens: number, outputTokens: number, model: string): Promise<void>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: To me, it seems more logical to put the model id as a first parameter and the second parameter is an object with input and output tokens. This way we can more easily extend what we track besides input and output tokens (thinking in the direction of image and audio).

this.client = client;
}

// Store token usage records
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
// Store token usage records

static readonly ID = 'ai-token-usage-configuration-container-widget';
static readonly LABEL = nls.localize('theia/ai/tokenUsage/label', 'Token Usage');

// Data will be fetched from the service
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
// Data will be fetched from the service

this.title.label = AITokenUsageConfigurationWidget.LABEL;
this.title.closable = false;

// Load initial data
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
// Load initial data

// Load initial data
this.refreshData();

// Subscribe to token usage updates
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
// Subscribe to token usage updates


.token-usage-configuration-title {
margin: 0 0 16px 0;
border-bottom: 1px solid var(--theia-panelTitle-activeBorder);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we shouldn't use the active border here. This color is somewhat reserved for components that have focus and are active. Here we use this color all the time.


.token-usage-table-container {
overflow-x: auto;
border: 1px solid var(--theia-panelTitle-activeBorder);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we shouldn't use the active border here. This color is somewhat reserved for components that have focus and are active. Here we use this color all the time.

text-align: left;
padding: 10px;
background-color: var(--theia-editor-background);
border-bottom: 1px solid var(--theia-panelTitle-activeBorder);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we shouldn't use the active border here. This color is somewhat reserved for components that have focus and are active. Here we use this color all the time.

@planger planger self-requested a review April 4, 2025 09:27
Copy link
Contributor

@planger planger left a comment

Choose a reason for hiding this comment

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

Very nice! Thanks for the revision. Looks good to me!

@github-project-automation github-project-automation bot moved this from Waiting on reviewers to Needs merge in PR Backlog Apr 4, 2025
@eneufeld eneufeld marked this pull request as ready for review April 4, 2025 10:10
@eneufeld eneufeld merged commit 8d34507 into master Apr 4, 2025
10 of 11 checks passed
@github-project-automation github-project-automation bot moved this from Needs merge to Done in PR Backlog Apr 4, 2025
@github-actions github-actions bot added this to the 1.61.0 milestone Apr 4, 2025
@JonasHelming JonasHelming mentioned this pull request Apr 5, 2025
37 tasks
@JonasHelming JonasHelming mentioned this pull request May 5, 2025
16 tasks
@ndoschek ndoschek deleted the feat/token-usage branch December 5, 2025 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants