Feedback on Foundation Models context management wrapper

I’ve been experimenting with Foundation Models and built a small Swift package that wraps LanguageModelSession with simple context management.

The current approach checks the transcript token count using tokenCount(for:), compacts the transcript when it reaches a threshold, and retries once if exceededContextWindowSize is thrown.

I’d appreciate feedback on whether this is a sensible use of Foundation Models APIs, especially around rebuilding a session from a compacted Transcript.

GitHub: https://github.com/ricky-stone/FoundationContext

The way you're doing compaction is generally correct, and recreating the session with the new transcript is correct if you're targeting iOS 26.

In iOS 27, session's transcript property is now mutable, and transcript has a history accessor for updating everything except the instructions, so you can just use that instead of recreating the session.

We've also introduced the notion of DynamicProfiles as a way to clip into the session lifecycle without having to wrap it, and open sourced some context management utilities similar to your own! You can use them as-is, or use them as inspiration to create your own context management modifiers to vend to others.

Feedback on Foundation Models context management wrapper
 
 
Q