@@ -78,10 +78,6 @@ export async function* streamAgent(
7878
7979 // Main agent loop, continues until llm sends a response other than toolCall or has no tool calls.
8080 while (true) {
81- let estimate = estimateTokens(JSON.stringify(llmCtx));
82- // 80k is the agreed uppon threshold to the DUMB ZONE
83- if (estimate > 80000) compactContext(llmCtx.messages);
84-
8581 const s = streamSimple(agentCtx.options.model, llmCtx, {
8682 reasoning: agentCtx.options.effort,
8783 signal: agentCtx.signal,
@@ -115,9 +111,6 @@ export async function* streamAgent(
115111 yield { type: "message_update", partial };
116112 }
117113
118- estimate = estimateTokens(JSON.stringify(llmCtx));
119- // 80k is the agreed uppon threshold to the DUMB ZONE
120- if (estimate > 80000) compactContext(llmCtx.messages);
121114 break;
122115 }
123116
@@ -134,6 +127,12 @@ export async function* streamAgent(
134127 return;
135128 }
136129 }
130+
131+ // Experiment: use this inside the inner loop for "running" compaction
132+ // after we are in the dumb zone.
133+ const estimate = estimateTokens(JSON.stringify(llmCtx));
134+ // 80k is the agreed uppon threshold to the DUMB ZONE
135+ if (estimate > 80000) compactContext(llmCtx.messages);
137136 }
138137
139138 const message = await s.result();