Skip to content

Add native tokenCount method to LanguageModelSession#12

Merged
corasan merged 3 commits into
mainfrom
t3code/add-native-token-count
Apr 20, 2026
Merged

Add native tokenCount method to LanguageModelSession#12
corasan merged 3 commits into
mainfrom
t3code/add-native-token-count

Conversation

@corasan
Copy link
Copy Markdown
Owner

@corasan corasan commented Apr 19, 2026

Summary

Bridges Apple's SystemLanguageModel token 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 using SystemLanguageModel.tokenCount(for:). Rejects with UNSUPPORTED_PLATFORM on iOS < 26.4.
  • LanguageModelSessionFactory.contextSize — exposes SystemLanguageModel.contextSize as a read-only property. getFoundationModelsContextSize() / FoundationModelsAvailability.contextSize now return the native value on iOS 26.4+ and undefined on 26.0–26.3 (previously always 4096).
  • LanguageModelSession.wasContextReset — readonly getter indicating whether the session's context was automatically summarized/reset after hitting the model's limit.
  • Adds TOKEN_COUNT_ERROR error case and regenerates the Nitro bridge (C++/Swift) for the new method and property.

Example app

  • example/utils/tokenMetrics.ts now calls session.tokenCount() when available and falls back to character-based estimation on older iOS, flagged via a new estimated: boolean field on TokenMetrics.
  • Hook and streaming demos pass the live session into getTokenMetrics and await the result, and display wasContextReset to the user when a summarization occurs.

Docs

  • API reference gains entries for tokenCount() and wasContextReset.
  • getFoundationModelsContextSize() / availability.contextSize notes updated to describe native bridging on iOS 26.4+ and undefined on older versions.

Testing

  • Verify tokenCount resolves with a valid count on iOS 26.4+ device/simulator.
  • Verify tokenCount rejects with UNSUPPORTED_PLATFORM on iOS 26.0–26.3.
  • Verify getFoundationModelsContextSize() returns a number on 26.4+ and undefined on earlier versions.
  • Exercise the example app on both OS ranges and confirm the estimated flag flips correctly.
  • Drive a long conversation to trigger wasContextReset and confirm the UI surfaces it.

corasan added 3 commits April 20, 2026 00:35
- 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
@corasan corasan merged commit 3bae128 into main Apr 20, 2026
3 checks passed
@corasan corasan deleted the t3code/add-native-token-count branch April 20, 2026 15:48
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.

1 participant