@@ -38,6 +38,7 @@ import { QueuedMessage } from "./Messages/QueuedMessage";
3838import { CompactionWarning } from "./CompactionWarning" ;
3939import { shouldAutoCompact } from "@/browser/utils/compaction/autoCompactionCheck" ;
4040import { useProviderOptions } from "@/browser/hooks/useProviderOptions" ;
41+ import { useSendMessageOptions } from "@/browser/hooks/useSendMessageOptions" ;
4142
4243interface AIViewProps {
4344 workspaceId : string ;
@@ -328,8 +329,14 @@ const AIViewInner: React.FC<AIViewProps> = ({
328329 // Get active stream message ID for token counting
329330 const activeStreamMessageId = aggregator . getActiveStreamMessageId ( ) ;
330331
331- const autoCompactionCheck = currentModel
332- ? shouldAutoCompact ( workspaceUsage , currentModel , use1M )
332+ // Use pending send model for auto-compaction check, not the last stream's model.
333+ // This ensures the threshold is based on the model the user will actually send with,
334+ // preventing context-length errors when switching from a large-context to smaller model.
335+ const pendingSendOptions = useSendMessageOptions ( workspaceId ) ;
336+ const pendingModel = pendingSendOptions . model ;
337+
338+ const autoCompactionCheck = pendingModel
339+ ? shouldAutoCompact ( workspaceUsage , pendingModel , use1M )
333340 : { shouldShowWarning : false , usagePercentage : 0 , thresholdPercentage : 70 } ;
334341
335342 // Show warning when: shouldShowWarning flag is true AND not currently compacting
0 commit comments