diff --git a/strix/llm/llm.py b/strix/llm/llm.py
index d166dfd4..fcd2ae21 100644
--- a/strix/llm/llm.py
+++ b/strix/llm/llm.py
@@ -26,6 +26,8 @@ from strix.utils.resource_paths import get_strix_resource_path
litellm.drop_params = True
litellm.modify_params = True
+_THINKING_BLOCK_RE = re.compile(r"]*>.*?", re.DOTALL)
+
class LLMRequestFailedError(Exception):
def __init__(self, message: str, details: str | None = None):
@@ -188,11 +190,8 @@ class LLM:
continue
delta = self._get_chunk_content(chunk)
if delta:
- accumulated += delta
- check_content = re.sub(
- r"]*>.*?", "", accumulated, flags=re.DOTALL
- )
- if "" in check_content or "" in check_content:
+ accumulated = _THINKING_BLOCK_RE.sub("", accumulated + delta)
+ if "" in accumulated or "" in accumulated:
end_tag = "" if "" in accumulated else ""
pos = accumulated.find(end_tag)
accumulated = accumulated[: pos + len(end_tag)]
@@ -207,8 +206,6 @@ class LLM:
accumulated = normalize_tool_format(accumulated)
accumulated = fix_incomplete_tool_call(_truncate_to_first_function(accumulated))
- accumulated = re.sub(r"]*>.*?", "", accumulated, flags=re.DOTALL)
-
yield LLMResponse(
content=accumulated,
tool_invocations=parse_tool_invocations(accumulated),