From 2e65e65afd9933ca1aff997188fb4cac2b456a3f Mon Sep 17 00:00:00 2001 From: chihlasm Date: Sat, 21 Feb 2026 04:01:58 -0500 Subject: [PATCH] fix: increase branch_detail max_tokens to 8192 and add response logging Truncated output at 4096 tokens produces invalid JSON mid-generation. Also logs stop_reason and output_tokens per attempt to diagnose failures. Co-Authored-By: Claude Sonnet 4.6 --- backend/app/core/ai_tree_generator_service.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/app/core/ai_tree_generator_service.py b/backend/app/core/ai_tree_generator_service.py index 34af11d2..c08f47a3 100644 --- a/backend/app/core/ai_tree_generator_service.py +++ b/backend/app/core/ai_tree_generator_service.py @@ -202,14 +202,23 @@ async def generate_branch_detail( for attempt in range(2): response = await client.messages.create( model=settings.AI_MODEL, - max_tokens=4096, + max_tokens=8192, system=BRANCH_DETAIL_SYSTEM_PROMPT, messages=messages, ) - raw_text = _strip_markdown_fences(response.content[0].text) total_input += response.usage.input_tokens total_output += response.usage.output_tokens + logger.debug( + "branch_detail attempt=%d stop_reason=%s content_blocks=%d output_tokens=%d", + attempt, + response.stop_reason, + len(response.content), + response.usage.output_tokens, + ) + raw_text = _strip_markdown_fences(response.content[0].text) if response.content else "" + if not raw_text: + logger.warning("branch_detail attempt=%d returned empty text, stop_reason=%s", attempt, response.stop_reason) try: branch_tree = json.loads(raw_text)