# Swift Context Management: A Package to efficiently handle LLM token limits and history

**URL:** <https://forums.swift.org/t/swift-context-management-a-package-to-efficiently-handle-llm-token-limits-and-history/84326>\
**Category:** Community Showcase\
**Tags:** llm\
**Created:** [January 23, 2026, 2:47pm UTC](https://forums.swift.org/t/swift-context-management-a-package-to-efficiently-handle-llm-token-limits-and-history/84326 "2026-01-23T14:47:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![silolabs](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/silolabs/32/44239_2.png) [@silolabs](https://forums.swift.org/u/silolabs)\
**Post date:** [January 23, 2026, 2:47pm UTC](https://forums.swift.org/t/swift-context-management-a-package-to-efficiently-handle-llm-token-limits-and-history/84326/1 "2026-01-23T14:47:06Z")

</div>

Hi everyone! 👋

We wanted to share a new package we have been working on called **Swift Context Management**.

 ![hero](https://global.discourse-cdn.com/swift/original/3X/9/b/9badb6b62ecbaae4249ab97ff265918963cbb8a5.jpeg)

As we build more conversational interfaces in Swift, managing the context window (and staying within token limits) becomes a major challenge. We built this package to help automate the process of pruning, summarizing, and restructuring conversation history so your users don't hit "context length exceeded" errors.

**What it does:** It provides configurable `ContextReductionPolicy` strategies to keep your context efficient:

- **Progressive Reduction:** Automatically retries requests with more aggressive reduction if limits are hit.

- **Rolling Summaries:** Compresses older history into a running summary while keeping recent turns verbatim.

- **Sliding Windows:** Simple N-turn retention.

- **Structured State:** Extracts key facts/decisions into a dedicated state object.

**Usage:** It integrates easily with `FoundationModels`. Here is a quick example of a session that automatically manages its own history:

Swift

```swift
import FoundationModels
import SwiftContextManagement

let session = LanguageModelSession()
let contextualSession = ContextualSession(
    session: session,
    policy: .rollingSummary() // or .slidingWindow(limit: 10), ...
)

// The session automatically manages context and token limits
let response = try await contextualSession.respond(to: "Hello, world!")

```

I have a lot more policies planned (like semantic recall and topic memory), and I’d love to hear your feedback or ideas for other reduction strategies!

**Check it out on GitHub:** [[swift-context-management]](https://github.com/Silo-Labs/swift-context-management)
