Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AI QL Tuui Think Tag Separation

From Leeroopedia
Knowledge Sources
Domains LLM, Streaming, Parsing
Last Updated 2026-10-24 20:38 GMT

Overview

Separate reasoning content enclosed in <think>...</think> tags from regular content during streaming, accumulating each into distinct fields for display.

Description

Some LLMs (e.g., Qwen, DeepSeek) emit reasoning traces wrapped in <think> / </think> tags within the streamed content. The think-tag separation pattern accumulates content as it arrives; when a closing </think> tag is found, it extracts the text between the open and close tags into reasoning_content and keeps the remainder as content. Only the first occurrence of the closing tag is processed, as models typically fail to function properly with multiple closing tags. The open tag may be absent (in which case everything before the close tag is treated as reasoning).

Usage

Apply this heuristic when parsing streamed LLM responses from models that emit reasoning content in think tags. The symptom is raw <think> markup appearing in the chat UI instead of being separated into a reasoning panel.

The Insight (Rule of Thumb)

  • Action: Accumulate raw content; on encountering </think>, split into reasoning_content (between tags) and content (after tags).
  • Value: Only the first </think> occurrence is processed. THINK_OPEN and THINK_CLOSE constants hold the tag strings.
  • Trade-off: Models that emit multiple think blocks will have only the first separated; subsequent think tags appear as raw content.
  • Edge case: If THINK_OPEN is absent, reasoning content starts from index 0.

Reasoning

Reasoning models interleave chain-of-thought with the final answer. Displaying raw tags in the UI is confusing to users. Separating into two fields allows the UI to render reasoning in a collapsible panel and the answer in the main content area. The single-occurrence constraint is documented in the code comment: "the model typically fails to function properly when multiple THINK_CLOSE instances appear."

Code Evidence

Think tag constants and separation logic from `src/renderer/composables/chatCompletions.ts:55-56,448-464`:

const THINK_OPEN = '<think>'
const THINK_CLOSE = '</think>'

const parseMixedContent = (chunk: string, target: AssistantMessage) => {
  // Only cases with a single occurrence of THINK_CLOSE in the response are considered,
  // as the model typically fails to function properly when multiple THINK_CLOSE instances appear.
  const data = (target.content ?? '') + chunk
  const closeIdx = data.indexOf(THINK_CLOSE)
  if (closeIdx === -1) {
    target.content += chunk
  } else {
    const openIdx = data.indexOf(THINK_OPEN)
    const divider = closeIdx + THINK_CLOSE.length
    const afterThink = data.substring(divider)
    const startIdx = openIdx < 0 ? 0 : openIdx + THINK_OPEN.length
    target.reasoning_content += data.substring(startIdx, closeIdx).trim()
    target.content = afterThink.trimStart()
  }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment