A Swift wrapper that remembers the conversation
Thin API client for OpenAI's ChatGPT with built-in streaming, history management, and token-aware truncation.

What it does
ChatGPTSwift is a Swift Package Manager library that wraps OpenAI’s ChatGPT API for Apple platforms and Linux. It handles the two basic interaction modes: streaming responses chunk-by-chunk via AsyncThrowingStream, or standard request-response for complete replies. The library also tracks conversation history and automatically truncates older messages when the token count nears 4096, using a companion GPTEncoder library for local token calculation.
The interesting bit
The history management is where this stops being “just another API wrapper.” The client maintains context across turns, calculates tokens locally to stay under limits, and lets you inspect, delete, or entirely replace the conversation history. The author notes future support for GPT-4’s larger 8K context window, which suggests the truncation logic is intentionally modular.
Key highlights
- Supports streaming and standard request-response APIs
- Built-in conversation history with automatic token-count truncation
- Configurable model, system prompt, and temperature per request
- Cross-platform: iOS/tvOS 15+, macOS 12+, watchOS 8+, Linux
- Companion libraries for token encoding (
GPTEncoder) and GUI tokenization (GPTTokenizerUI)
Caveats
- Token threshold is currently hardcoded at 4096; GPT-4’s 8K support is listed as future work
- History replacement requires manual token discipline — the library won’t protect you from blowing the limit with a custom list
Verdict
Worth a look if you’re building a Swift-native ChatGPT client and don’t want to hand-roll streaming, history state, and token math. Skip it if you need advanced features like function calling, fine-tuning, or non-OpenAI model support — this is intentionally thin.
Frequently asked
- What is alfianlosari/ChatGPTSwift?
- Thin API client for OpenAI's ChatGPT with built-in streaming, history management, and token-aware truncation.
- Is ChatGPTSwift open source?
- Yes — alfianlosari/ChatGPTSwift is open source, released under the MIT license.
- What language is ChatGPTSwift written in?
- alfianlosari/ChatGPTSwift is primarily written in Swift.
- How popular is ChatGPTSwift?
- alfianlosari/ChatGPTSwift has 698 stars on GitHub.
- Where can I find ChatGPTSwift?
- alfianlosari/ChatGPTSwift is on GitHub at https://github.com/alfianlosari/ChatGPTSwift.