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:42 evey Hermes plugins Thinking Block Stripping

From Leeroopedia
Knowledge Sources
Domains LLM_Delegation, Output_Processing, Debugging
Last Updated 2026-10-29 00:00 GMT

Overview

Strip <think>/<thinking>/<reasoning> tags from LLM responses before parsing, including unclosed blocks truncated by max_tokens.

Description

Some "thinking" models (e.g., DeepSeek-R1, QwQ) embed their chain-of-thought reasoning inline in the reply text using XML-like tags: <think>...</think>, <thinking>...</thinking>, or <reasoning>...</reasoning>. These reasoning blocks bloat the response, confuse downstream parsing (e.g., regex score extraction), and waste context window space. The pattern uses a case-insensitive regex (?is)<(think|thinking|reasoning)>.*?(?:</\1>|$) to remove these blocks — including unclosed blocks where the model ran out of tokens before closing the tag (the |$ alternative matches end-of-string).

This pattern appears identically in both evey-validate and evey-memory-consolidate, both of which parse LLM output for structured data (scores, facts).

Usage

Use this heuristic whenever you parse LLM output for structured data (scores, JSON, facts) and the model may be a "thinking" model. Apply the regex strip before any regex extraction or JSON parsing.

The Insight (Rule of Thumb)

  • Action: Apply re.compile(r"(?is)<(think|thinking|reasoning)>.*?(?:</\1>|$)") to LLM response text before parsing.
  • Value: The (?is) flags make it case-insensitive and dotall (newlines match .); the |$ alternative catches truncated unclosed blocks.
  • Trade-off: If a legitimate response contains a <think> tag as actual content (unlikely), it would be stripped. The trade-off is acceptable for structured-output use cases.
  • Compatibility: Works with any LLM provider; the regex is applied to the raw text string before any downstream processing.

Reasoning

Without stripping, a thinking model's response like <think>Let me analyze this...</think>SCORE: 7 | ISSUES: none would fail a re.search(r"score:\s*(\d+)", text) because the regex would match the reasoning text first or the extra content would confuse the parser. By stripping first, the parser sees only SCORE: 7 | ISSUES: none.

The max_tokens=512 used in score/validation calls is deliberately generous: it leaves room for a thinking model's reasoning before the actual answer. The strip then removes the reasoning, leaving just the score line.

Related Pages

Page Connections

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