Add native tokenCount method to LanguageModelSession#12
Merged
Conversation
- Expose SystemLanguageModel.tokenCount(for:) via Nitro bridge (iOS 26.4+) - Add tokenCountError case to AppleAIErrors - Reject with UNSUPPORTED_PLATFORM on iOS < 26.4
- Expose SystemLanguageModel.contextSize (iOS 26.4+) via Nitro spec - Replace hardcoded 4096 context size with native value - Update example app to use session.tokenCount() with estimation fallback - Add `estimated` flag to TokenMetrics
- Add API reference for tokenCount() and wasContextReset - Update contextSize docs to reflect native bridging on iOS 26.4+ - Clarify contextSize returns undefined on iOS 26.0–26.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bridges Apple's
SystemLanguageModeltoken and context APIs (iOS 26.4+), replaces the previously hardcoded 4,096-token estimate, and surfaces automatic context-reset signaling.Native APIs
LanguageModelSession.tokenCount(prompt)— returns the token count for a given string usingSystemLanguageModel.tokenCount(for:). Rejects withUNSUPPORTED_PLATFORMon iOS < 26.4.LanguageModelSessionFactory.contextSize— exposesSystemLanguageModel.contextSizeas a read-only property.getFoundationModelsContextSize()/FoundationModelsAvailability.contextSizenow return the native value on iOS 26.4+ andundefinedon 26.0–26.3 (previously always4096).LanguageModelSession.wasContextReset— readonly getter indicating whether the session's context was automatically summarized/reset after hitting the model's limit.TOKEN_COUNT_ERRORerror case and regenerates the Nitro bridge (C++/Swift) for the new method and property.Example app
example/utils/tokenMetrics.tsnow callssession.tokenCount()when available and falls back to character-based estimation on older iOS, flagged via a newestimated: booleanfield onTokenMetrics.getTokenMetricsand await the result, and displaywasContextResetto the user when a summarization occurs.Docs
tokenCount()andwasContextReset.getFoundationModelsContextSize()/availability.contextSizenotes updated to describe native bridging on iOS 26.4+ andundefinedon older versions.Testing
tokenCountresolves with a valid count on iOS 26.4+ device/simulator.tokenCountrejects withUNSUPPORTED_PLATFORMon iOS 26.0–26.3.getFoundationModelsContextSize()returns a number on 26.4+ andundefinedon earlier versions.estimatedflag flips correctly.wasContextResetand confirm the UI surfaces it.