Skip to content

FEAT: Capture token usage from ChatCompletion response in OpenAIChatTarget#1476

Merged
rlundeen2 merged 3 commits intoAzure:mainfrom
slister1001:fix/capture-token-usage
Mar 17, 2026
Merged

FEAT: Capture token usage from ChatCompletion response in OpenAIChatTarget#1476
rlundeen2 merged 3 commits intoAzure:mainfrom
slister1001:fix/capture-token-usage

Conversation

@slister1001
Copy link
Member

Store response.usage (prompt_tokens, completion_tokens, total_tokens, cached_tokens, model_name) in the first response piece's labels under the 'token_usage' key. This enables downstream consumers like azure-ai-evaluation to track per-row token usage when using OpenAIChatTarget as a prompt target.

Description

Tests and Documentation

…arget

Store response.usage (prompt_tokens, completion_tokens, total_tokens,
cached_tokens, model_name) in the first response piece's labels under
the 'token_usage' key. This enables downstream consumers like
azure-ai-evaluation to track per-row token usage when using
OpenAIChatTarget as a prompt target.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rlundeen2 rlundeen2 self-assigned this Mar 16, 2026
- Move token_usage storage from labels to prompt_metadata (flat keys
  with token_usage_ prefix) for type safety with dict[str, Union[str, int]]
- Add unit tests: token usage capture, no-usage case, missing attr defaults
- Add integration test: validates real API responses contain token usage

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rlundeen2 rlundeen2 merged commit 555f877 into Azure:main Mar 17, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants