Skip to content

fix(ext): normalize Semantic Kernel token usage - #8327

Open
Thota Shashank (thotashashank302) wants to merge 1 commit into
microsoft:mainfrom
thotashashank302:fix/sk-anthropic-token-usage
Open

Thota Shashank (thotashashank302) wants to merge 1 commit into
microsoft:mainfrom
thotashashank302:fix/sk-anthropic-token-usage

Conversation

@thotashashank302

Copy link
Copy Markdown

Why are these changes needed?

SKChatCompletionAdapter reports zero token usage for Anthropic responses because it only reads OpenAI's prompt_tokens and completion_tokens attributes. For example, Claude usage with input_tokens=147 and output_tokens=1024 becomes RequestUsage(0, 0), affecting the returned usage, cumulative counters, and logged usage.

Normalize usage in both create() and create_stream(). Accept SDK usage objects and mappings: Semantic Kernel's Anthropic connector returns an object for ordinary responses and a dictionary for streaming responses. Prefer OpenAI field names when present, including explicit zero values, and fall back to Anthropic names. Missing usage retains the existing zero-count behavior.

Add 16 offline regression cases covering both request paths, Anthropic and OpenAI usage objects/dictionaries, field precedence, missing/null usage, accumulated totals across two calls, and usage events.

Validation on macOS with Python 3.12.14, using uv, lockfile-pinned Semantic Kernel/Anthropic dependencies and check tools:

  • New regression cases on the original implementation: 6 failed, 10 passed.
  • Complete Semantic Kernel adapter test module after the fix: 26 passed.
  • Ruff lint and formatting, mypy, and Pyright on both changed files: passed.
  • autogen-ext wheel build and git diff --check: passed.

The full workspace/optional-provider test matrix and documentation build were not run locally. Tests use mocked responses; no live model APIs were called. Implementation and local validation were assisted by OpenAI Codex.

Related issue number

Fixes #5464

Checks

  • Reviewed documentation impact; this fixes existing token accounting without changing public APIs.
  • Added tests corresponding to the changes.
  • All GitHub checks have passed (pending CI).

This branch has not been deployed

No deployments
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.

Token usage field mismatch between Anthropic Claude and OpenAI naming conventions in SKChatCompletionAdapter

1 participant