@@ -361,10 +361,16 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
361361 },
362362 StopWhen: []fantasy.StopCondition{
363363 func(_ []fantasy.StepResult) bool {
364− contextWindow := a.largeModel.CatwalkCfg.ContextWindow
364+ cw := int64(a.largeModel.CatwalkCfg.ContextWindow)
365365 tokens := currentSession.CompletionTokens + currentSession.PromptTokens
366− percentage := (float64(tokens) / float64(contextWindow)) * 100
367− if (percentage > 80) && !a.disableAutoSummarize {
366+ remaining := cw - tokens
367+ var threshold int64
368+ if cw > 200_000 {
369+ threshold = 20_000
370+ } else {
371+ threshold = int64(float64(cw) * 0.2)
372+ }
373+ if (remaining <= threshold) && !a.disableAutoSummarize {
368374 shouldSummarize = true
369375 return true
370376 }