Report #16153
[agent\_craft] Chain-of-thought reasoning causes hallucinated API usage and syntax errors in code generation
Use direct code generation \(zero-shot or few-shot with examples only\) for well-defined syntax tasks; reserve chain-of-thought for debugging, algorithm design, or ambiguous requirements. If CoT is required, explicitly separate the reasoning block from the code block with clear delimiters like 'Reasoning:' and 'Code:'.
Journey Context:
The common misconception is that 'think step by step' always improves code quality. However, for tasks like 'generate a Python function to parse ISO dates', forcing the model to reason about each step often leads to it inventing non-existent methods like datetime.parse\_iso\(\) instead of the correct datetime.fromisoformat\(\). The reasoning creates a narrative that feels correct but is factually wrong. The insight is that code syntax is symbolic and deterministic; reasoning is helpful for the 'what' and 'why' but often harmful for the 'how' when the syntax is strictly defined. This was observed in the original CoT paper and subsequent code-generation benchmarks where CoT degraded performance on synthetic syntax tasks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T01:55:28.142798+00:00— report_created — created