Good. Now **Fix 2, 3, 4:** Update the JS in thread_view.php to:
- Use `thread_for_ai` endpoint instead of `thread`
- Fix token estimation to use `emailCount * 500` instead of `* 2000`
- Truncate total content before sending
- Handle fetch errors gracefully


1429:            total += contextThreads.length * 2000;


  1410→                var model = modelSelect ? modelSelect.value : '';
  1411→                var limit = MODEL_LIMITS[model] || 128000;
  1412→                if (limit < minLimit) minLimit = limit;
  1413→            });
  1414→            return minLimit === Infinity ? 128000 : minLimit;
  1415→        }
  1416→
  1417→        function estimateCurrentTokens() {
  1418→            var content = buildEmailContent();
  1419→            var question = (document.getElementById('ai-question').value || '').trim();
  1420→            var context = (document.getElementById('ai-context').value || '').trim();
  1421→            var agent = AI_AGENTS[selectedAgent];
  1422→            var total = content.length;
  1423→            if (agent) total += agent.systemPrompt.length;
  1424→            if (question) total += question.length;
  1425→            if (context) total += context.length;
  1426→            // Add conversation history estimate
  1427→            total += buildConversationContext().length;
  1428→            // Add context threads estimate (rough: each thread ~2000 chars)
  1429→            total += contextThreads.length * 2000;
  1430→            return Math.ceil(total / 3.5);
  1431→        }
  1432→
  1433→        function updateTokenEstimate() {
  1434→            var el = document.getElementById('token-estimate');
  1435→            if (!el) return;
  1436→            var tokens = estimateCurrentTokens();
  1437→            var limit = getSmallestModelLimit();
  1438→            var pct = (tokens / limit) * 100;
  1439→            var formatted = tokens.toLocaleString();

                var model = modelSelect ? modelSelect.value : '';
                var limit = MODEL_LIMITS[model] || 128000;
                if (limit < minLimit) minLimit = limit;
            });
            return minLimit === Infinity ? 128000 : minLimit;
        }

        function estimateCurrentTokens() {
            var content = buildEmailContent();
            var question = (document.getElementById('ai-question').value || '').trim();
            var context = (document.getElementById('ai-context').value || '').trim();
            var agent = AI_AGENTS[selectedAgent];
            var total = content.length;
            if (agent) total += agent.systemPrompt.length;
            if (question) total += question.length;
            if (context) total += context.length;
            // Add conversation history estimate
            total += buildConversationContext().length;
            // Add context threads estimate (rough: each thread ~2000 chars)
            total += contextThreads.length * 2000;
            return Math.ceil(total / 3.5);
        }

        function updateTokenEstimate() {
            var el = document.getElementById('token-estimate');
            if (!el) return;
            var tokens = estimateCurrentTokens();
            var limit = getSmallestModelLimit();
            var pct = (tokens / limit) * 100;
            var formatted = tokens.toLocaleString();


Fix the token estimation — use `emailCount * 500` per stripped email (much more realistic):


Edit

            // Add conversation history estimate
            total += buildConversationContext().length;
            // Add context threads estimate (rough: each thread ~2000 chars)
            total += contextThreads.length * 2000;
            return Math.ceil(total / 3.5);

            // Add conversation history estimate
            total += buildConversationContext().length;
            // Add context threads + checkbox threads estimate
            // Use emailCount * 500 chars per stripped email (realistic average)
            var checkboxThreads = getSelectedThreads();
            contextThreads.forEach(function(ct) { total += ct.emailCount * 500; });
            total += checkboxThreads.length * 5000; // rough estimate for checkbox threads
            return Math.ceil(total / 3.5);