Chain of Code: Reasoning with a Language Model-Augmented Code Emulator
Chengshu Li, Jacky Liang, Andy Zeng, Xinyun Chen, Karol Hausman, Dorsa Sadigh, Sergey Levine, Li Fei-Fei, Fei Xia, Brian Ichter
Abstract
Code provides a general syntactic structure to build complex programs and perform precise computations when paired with a code interpreter -we hypothesize that language models (LMs) can leverage code-writing to improve Chain of Thought reasoning not only for logic and arithmetic tasks (Chen et al., 2022; Nye et al., 2021; Austin et al., 2021) , but also for semantic ones (and in particular, those that are a mix of both). For example, consider prompting an LM to write code that counts the number of times it detects sarcasm in an essay: the LM may struggle to write an implementation for "detect_sarcasm(string)" that can be executed by the interpreter (handling the edge cases would be insurmountable). However, LMs may still produce a valid solution if they not only write code, but also selectively "emulate" the interpreter by generating the expected output of "detect_sarcasm(string)". In this work, we propose Chain of Code (CoC), a simple yet surprisingly effective extension that improves LM codedriven reasoning. The key idea is to encourage LMs to format semantic sub-tasks in a program as flexible pseudocode that the interpreter can explicitly catch undefined behaviors and hand off to simulate with an LM (as an "LMulator"). Experiments demonstrate that Chain of Code outperforms Chain of Thought and other baselines across a variety of benchmarks; on BIG-Bench Hard, Chain of Code achieves 84%, a gain of 12% over Chain of Thought. In a nutshell, CoC broadens the scope of reasoning questions that LMs can answer by "thinking in code".
Ask about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Your agent calls
Luneget_paper_fulltext
Free to start. No credit card required.
Terminal
Install the CLIlune papers fulltext 44c30488-a35b-4d0d-84db-ad6c5fc56dbaCited by top-tier papers38
- Instance-adaptive Zero-shot Chain-of-Thought PromptingXiaosong Yuan, Chen Shen, Shaotian Yan, Xiaofeng Zhang et al.NeurIPS 2024 · 46 citations
- OceanGPT: A Large Language Model for Ocean Science TasksZhen Bi, Ningyu Zhang, Yida Xue, Yixin Ou et al.ACL 2024 · 35 citations
- ARM: Adaptive Reasoning ModelSiye Wu, Jian Xie, Yikai Zhang, Aili Chen et al.NeurIPS 2025 · 31 citations
- Divide-and-Conquer Meets Consensus: Unleashing the Power of Functions in Code GenerationJingchang Chen, Hongxuan Tang, Zheng Chu, Qianglong Chen et al.NeurIPS 2024 · 29 citations
- LogoMotion: Visually-Grounded Code Synthesis for Creating and Editing AnimationVivian Liu, Rubaiat Habib Kazi, Li-Yi Wei, Matthew Fisher et al.CHI 2025 · 23 citations
Builds on17
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah et al.NeurIPS 2020 · 64,255 citations
- Chain-of-Thought Prompting Elicits Reasoning in Large Language ModelsJason Wei, Xuezhi Wang, Dale Schuurmans, Maarten Bosma et al.NeurIPS 2022 · 22,562 citations
- Large Language Models are Zero-Shot ReasonersTakeshi Kojima, Shixiang Shane Gu, Machel Reid, Yutaka Matsuo et al.NeurIPS 2022 · 8,168 citations
- Tree of Thoughts: Deliberate Problem Solving with Large Language ModelsShunyu Yao, Dian Yu, Jeffrey Zhao, Izhak Shafran et al.NeurIPS 2023 · 5,068 citations
- Solving Quantitative Reasoning Problems with Language ModelsAitor Lewkowycz, Anders Andreassen, David Dohan, Ethan Dyer et al.NeurIPS 2022 · 2,039 citations
Related papers
- Learning to Reason via Program Generation, Emulation, and SearchNathaniel Weir, Muhammad Khalifa, Linlu Qiu, Orion Weller et al.NeurIPS 2024 · 16 citations
- PAL: Program-aided Language ModelsLuyu Gao, Aman Madaan, Shuyan Zhou, Uri Alon et al.ICML 2023 · 700 citations
- Code Prompting Elicits Conditional Reasoning Abilities in Text+Code LLMsHaritz Puerto, Martin Tutek, Somak Aditya, Xiaodan Zhu et al.EMNLP 2024 · 4 citations
- Revisiting Chain-of-Thought in Code Generation: Do Language Models Need to Learn Reasoning before Coding?Renbiao Liu, Anqi Li, Chaoding Yang, Hui Sun et al.ICML 2025
- Language Models as Compilers: Simulating Pseudocode Execution Improves Algorithmic Reasoning in Language ModelsHyungjoo Chae, Yeonghyeon Kim, Seungone Kim, Kai Tzu-iunn Ong et al.EMNLP 2024
