SPB Git

spb/poche Public

Agent personnel 100 % on-device — SwiftUI + Apple Foundation Models + EventKit + SwiftData. Aucune API, aucun serveur.

Swift 100%
1.4 KB · 49 lines swift
Raw Blame History
1//2//  ContextBudget.swift3//  Poche4//5//  Author:  Simon-Pierre Boucher6//  Contact: contact@spboucher.ai7//89import Foundation1011/// Token accounting for one session (CLAUDE.md §5 — the central concern).12///13/// The window holds everything at once: instructions, tool schemas, the14/// whole transcript, and the response to come. At 70% we condense; at least15/// 30% stays reserved for the response — a prompt that "fits" with no room16/// to answer still fails.17struct ContextBudget: Sendable {18    /// iOS 26 window. 8192 on newer OS/devices; keep the pessimistic value19    /// until the SDK exposes the real one.20    var windowSize = 409621    var responseReserveRatio = 0.3022    var condenseThresholdRatio = 0.702324    var responseReserve: Int {25        Int(Double(windowSize) * responseReserveRatio)26    }2728    var condenseThreshold: Int {29        Int(Double(windowSize) * condenseThresholdRatio)30    }3132    /// Largest prompt (fixed cost + transcript + new message) we allow.33    var sendableLimit: Int {34        windowSize - responseReserve35    }3637    func needsCondensation(estimatedTokens: Int) -> Bool {38        estimatedTokens >= condenseThreshold39    }4041    func canSend(estimatedTokens: Int) -> Bool {42        estimatedTokens < sendableLimit43    }4445    func usageRatio(estimatedTokens: Int) -> Double {46        min(1, Double(estimatedTokens) / Double(windowSize))47    }48}49