Skip to content

feat(chat): add completion_tokens_details to CompletionUsage#411

Merged
nezhyborets merged 1 commit intomainfrom
claude/issue-258-completion-tokens-details
Apr 29, 2026
Merged

feat(chat): add completion_tokens_details to CompletionUsage#411
nezhyborets merged 1 commit intomainfrom
claude/issue-258-completion-tokens-details

Conversation

@Krivoblotsky
Copy link
Copy Markdown
Contributor

Summary

Closes #258.

OpenAI's chat completion usage payload includes a completion_tokens_details object that surfaces three pieces of information that are otherwise unreachable today:

  • reasoning_tokens — emitted by o-series reasoning models.
  • audio_tokens — emitted when the model produces audio output.
  • accepted_prediction_tokens / rejected_prediction_tokens — emitted when Predicted Outputs are used.

This PR mirrors the existing PromptTokensDetails pattern with a new nested CompletionTokensDetails struct on ChatResult.CompletionUsage. Fields are Int? because each only appears under specific request conditions, and we don't want a missing field to fail decoding.

The new CompletionUsage.init parameter has a default of nil, so existing call sites in tests and downstream code continue to compile unchanged.

Test plan

  • New decoder test (testCompletionUsageWithCompletionTokensDetails) round-trips a representative reasoning-model usage payload.
  • New decoder test (testCompletionUsageDecodesWithoutCompletionTokensDetails) confirms backward-compat decode of payloads that don't include the new field.
  • swift test — all 177 tests pass locally.

🤖 Generated with Claude Code

OpenAI's chat completion `usage` payload includes a
`completion_tokens_details` object for reasoning models, audio
outputs, and Predicted Outputs. The library was silently dropping
this on decode, making fields like reasoning_tokens unreachable.

Mirror the existing `PromptTokensDetails` shape with a new
`CompletionTokensDetails` struct exposing accepted_prediction_tokens,
audio_tokens, reasoning_tokens, and rejected_prediction_tokens. Fields
are `Int?` because each only appears under specific conditions
(reasoning_tokens for o-series, audio_tokens for audio output, the
prediction fields for Predicted Outputs).

The new init parameter has a default value, so existing call sites in
tests continue to compile unchanged.

Closes #258

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for OpenAI Chat Completions usage.completion_tokens_details by extending the ChatResult.CompletionUsage model to decode (optionally) reasoning/audio/predicted-output token breakdowns.

Changes:

  • Add completionTokensDetails to ChatResult.CompletionUsage with a new nested CompletionTokensDetails model (all fields optional) and corresponding coding key.
  • Add decoder tests covering payloads both with and without completion_tokens_details.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
Tests/OpenAITests/OpenAITestsDecoder.swift Adds decoder coverage for completion_tokens_details presence/absence.
Sources/OpenAI/Public/Models/ChatResult.swift Extends CompletionUsage with completionTokensDetails and a new nested CompletionTokensDetails Codable struct.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Tests/OpenAITests/OpenAITestsDecoder.swift
@nezhyborets nezhyborets merged commit 97e1956 into main Apr 29, 2026
7 checks passed
@nezhyborets nezhyborets deleted the claude/issue-258-completion-tokens-details branch April 29, 2026 16:58
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.

Add support for completion_tokens_details

3 participants