Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add service tier to TokenUsage recording - #28709

Merged
pawbana merged 2 commits into
mainfrom
pawel/aigov-632-add-service_tier-information-to-interception-metadata
Aug 27, 2026
Merged

feat: add service tier to TokenUsage recording#28709
pawbana merged 2 commits into
mainfrom
pawel/aigov-632-add-service_tier-information-to-interception-metadata

Conversation

@pawbana

@pawbana pawbana commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Adds service_tier information to token usage recordings made by OpenAI and Anthropic interceptors.

@linear-code

linear-code Bot commented Aug 27, 2026

Copy link
Copy Markdown

AIGOV-632

@pawbana pawbana changed the title Pawel/aigov 632 add service tier information to interception metadata fix: add service tier to TokenUsage recording Aug 27, 2026
@pawbana pawbana changed the title fix: add service tier to TokenUsage recording feat: add service tier to TokenUsage recording Aug 27, 2026
@pawbana
pawbana marked this pull request as draft August 27, 2026 11:22
@pawbana
pawbana force-pushed the pawel/aigov-632-add-service_tier-information-to-interception-metadata branch 2 times, most recently from 10f23a2 to b441839 Compare August 27, 2026 11:51
@pawbana
pawbana force-pushed the pawel/aigov-632-add-service_tier-information-to-interception-metadata branch from b441839 to 0c14405 Compare August 27, 2026 12:00
@pawbana
pawbana marked this pull request as ready for review August 27, 2026 12:07
@pawbana
pawbana requested review from mtojek and ssncferreira August 27, 2026 12:07

@mtojek mtojek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

plumbing and testing 👍 left minor question about the structure.

serviceTier := string(response.ServiceTier)
var metadata recorder.Metadata
if serviceTier != "" {
metadata = recorder.Metadata{recorder.MetadataKeyServiceTier: serviceTier}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just confirmation: we want an empty structure rather rather nil-property?

@pawbana pawbana Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it is the same in case of recorder.Metadata which is a map[string]any since empty map == nil.
Struct with Metadata not set should be equal to one with Metadata set to empty map / nil, tested here: https://github.com/coder/coder/pull/28709/changes#diff-132fbd5ab5a0ddb045cc7066cb363bd86cbf6ea92151b10d7995be40dfdb1647R451-R470

@ssncferreira ssncferreira left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

InterceptionID: i.ID().String(),
MsgID: message.ID,
Output: delta.Usage.OutputTokens,
i.recordTokenUsage(streamCtx, message.ID, anthropic.Usage{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a behaviour change: before we were only storing Output, but now we store all the fields from recordTokenUsage. Are all those fields reported by message_delta events?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I believe other then ExtraTokenTypes would default to 0 before so there is no behaviour change for those. For ExtraTokenTypes there is as there was empty map before and now there is map with entries that has 0 value.
Will change so ExtraTokenTypes are only added when non 0 which is also a behaviour change but I think it is an improvement, no need to store 0 values in this map.

i.mcpProxy = mcpProxy
}

func (i *interceptionBase) recordTokenUsage(ctx context.Context, msgID string, usage anthropic.Usage) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Could we add a small unit test for Anthropic usage without service_tier? That would verify we do not create a misleading empty tier, matching the coverage in the OpenAI paths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, added.

@pawbana
pawbana enabled auto-merge (squash) August 27, 2026 15:43
@pawbana
pawbana merged commit b6505fe into main Aug 27, 2026
28 checks passed
@pawbana
pawbana deleted the pawel/aigov-632-add-service_tier-information-to-interception-metadata branch August 27, 2026 15:57
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants