@@ -361,10 +361,16 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
362 StopWhen: []fantasy.StopCondition{
362 StopWhen: []fantasy.StopCondition{
363 func(_ []fantasy.StepResult) bool {
363 func(_ []fantasy.StepResult) bool {
364− contextWindow := a.largeModel.CatwalkCfg.ContextWindow
364+ cw := int64(a.largeModel.CatwalkCfg.ContextWindow)
365 tokens := currentSession.CompletionTokens + currentSession.PromptTokens
365 tokens := currentSession.CompletionTokens + currentSession.PromptTokens
366− percentage := (float64(tokens) / float64(contextWindow)) * 100
366+ remaining := cw - tokens
371+ threshold = int64(float64(cw) * 0.2)
367− if (percentage > 80) && !a.disableAutoSummarize {
373+ if (remaining <= threshold) && !a.disableAutoSummarize {
368 shouldSummarize = true
374 shouldSummarize = true
369 return true
375 return true