| name | cort-code-reasoning |
| title | CoRT: Code-integrated Reasoning within Thinking |
| version | 0.0.2 |
| engine | skillxiv-v0.0.2-claude-opus-4.6 |
| license | MIT |
| url | https://arxiv.org/abs/2506.09820 |
| keywords | ["reasoning","code execution","extended thinking","grounded computation","reasoning models"] |
| description | Enhance reasoning models by integrating executable code within thinking traces, enabling grounded computation verification and reducing hallucination in mathematical and logical reasoning. |
CoRT: Code-integrated Reasoning within Thinking
Core Concept
CoRT augments reasoning models' thinking process by embedding executable code directly into reasoning traces. Rather than pure symbolic reasoning prone to arithmetic errors and logical fallacies, models generate reasoning steps with executable code blocks that are immediately validated. This grounds abstract reasoning in concrete computation, reducing hallucination and improving accuracy on mathematical, logical, and programming tasks. The approach enables self-correction when code execution results contradict reasoning assumptions.
Architecture Overview
- Code-Augmented Thinking: Reasoning traces include executable code sections alongside natural language explanations
- Immediate Validation: Code blocks execute during generation, providing real-time feedback to guide subsequent reasoning
- Grounded Computation: Mathematical operations, symbolic manipulations, and logical checks verified through code
- Self-Correction Mechanism: Models adjust reasoning when code results contradict expectations
- Token Efficiency: Shorter total token count vs pure reasoning through code compression
- Multi-Domain Support: Applicable to math, logic puzzles, programming, and science problems
Implementation
Step 1: Code-Integrated Thinking Module
import torch
import torch.nn as nn
from typing import Dict, List, Tuple
import subprocess
import tempfile
import os
class CodeIntegratedThinkingModule(nn.Module):
"""
Augments reasoning traces with executable code blocks.
Interleaves natural language reasoning with Python code for verification.
"""
def __init__(self, base_model, execution_timeout=5):
super().__init__()
self.base_model = base_model
self.execution_timeout = execution_timeout
self.execution_history = []
def generate_with_code_reasoning(self, question: str, max_thinking_tokens: int = 8000):
"""
Generate reasoning trace with embedded code blocks.
Format: <think>
Natural language reasoning...
```python
# Code block
code here
```
More reasoning...
</think>
"""
thinking_prompt = f"""
Solve this problem with integrated reasoning and code verification.
Use this format:
<think>
Explain your approach.
```python
# Code to verify computations
result = ...
print(f"Result: {{result}}")
```
Interpret the code output and continue reasoning...
</think>
Problem: {question}
"""
thinking_trace = self._generate_thinking(
thinking_prompt,
max_tokens=max_thinking_tokens
)
code_blocks = ._extract_code_blocks(thinking_trace)
execution_results = []
code code_blocks:
result = ._execute_code_block(code)
execution_results.append({
: code,
: result[],
: result[],
: result[]
})
.execution_history.append(result)
{
: thinking_trace,
: code_blocks,
: execution_results
}
() -> :
() -> []:
re
pattern =
matches = re.findall(pattern, thinking_trace, re.DOTALL)
matches
() -> :
:
tempfile.NamedTemporaryFile(mode=, suffix=, delete=) f:
f.write(code)
temp_path = f.name
:
result = subprocess.run(
[, temp_path],
capture_output=,
text=,
timeout=.execution_timeout
)
{
: result.stdout,
: result.stderr,
: result.returncode,
: result.returncode ==
}
:
os.path.exists(temp_path):
os.remove(temp_path)
subprocess.TimeoutExpired:
{
: ,
: ,
:
}
Exception e:
{
: ,
: (e),
:
}
Step 2: Self-Correction via Code Feedback
class SelfCorrectionMechanism:
"""
Detects contradictions between reasoning and code execution results.
Triggers re-reasoning when assumptions prove incorrect.
"""
def __init__(self, model, max_correction_rounds=3):
self.model = model
self.max_correction_rounds = max_correction_rounds
def identify_contradictions(self, reasoning_trace: str,
execution_results: List[Dict]) -> List[Dict]:
"""
Identify where reasoning contradicts code execution results.
Returns list of contradiction locations.
"""
contradictions = []
for i, exec_result in enumerate(execution_results):
if not exec_result['success']:
contradictions.append({
'block_index': i,
'type': 'execution_error',
'error': exec_result['error'],
'severity': 'high'
})
continue
output = exec_result['output']
if self._contradicts_reasoning(reasoning_trace, output):
contradictions.append({
'block_index': i,
: ,
: output,
:
})
contradictions
() -> :
contradictions:
{: }
correction_prompt =
contradiction contradictions[:]:
contradiction[] == :
correction_prompt +=
:
correction_prompt +=
correction_prompt +=
corrected_trace = .model.generate(correction_prompt, max_tokens=)
{
: ,
: initial_reasoning,
: corrected_trace,
: (contradictions)
}
() -> :
() -> :
current_reasoning =
execution_results = []
correction_count =
round_num (max_rounds):
current_reasoning :
result = ._generate_initial(question)
:
result = ._generate_correction(question, current_reasoning)
current_reasoning = result[]
execution_results = result[]
contradictions = .identify_contradictions(
current_reasoning,
execution_results
)
contradictions:
correction_count +=
{
: current_reasoning,
: execution_results,
: correction_count,
: (contradictions) ==
}
() -> :
{: , : []}
() -> :
{: , : []}
Step 3: Code Validation and Type Checking
import ast
import typing
class CodeValidator:
"""
Validates code blocks before execution for common errors.
Catches logical issues and improves error messages.
"""
def __init__(self):
self.allowed_builtins = {
'print', 'len', 'range', 'sum', 'min', 'max',
'int', 'float', 'str', 'list', 'dict', 'set',
'abs', 'round', 'sorted', 'enumerate', 'zip'
}
self.forbidden_imports = {'os', 'sys', 'subprocess', '__main__'}
def validate_code_block(self, code: str) -> Dict:
"""
Comprehensive validation before execution.
Returns validation status and identified issues.
"""
issues = []
try:
tree = ast.parse(code)
except SyntaxError as e:
issues.append({
'type': 'syntax_error',
'message': str(e),
'severity':
})
{
: ,
: issues,
:
}
node ast.walk(tree):
(node, ast.Import):
alias node.names:
alias.name .forbidden_imports:
issues.append({
: ,
: alias.name,
:
})
(node, ast.ImportFrom):
node.module .forbidden_imports:
issues.append({
: ,
: node.module,
:
})
node ast.walk(tree):
(node, ast.While):
(node.test, ast.Constant) node.test.value :
issues.append({
: ,
: ,
:
})
defined_vars = ()
used_vars = ()
node ast.walk(tree):
(node, ast.Assign):
target node.targets:
(target, ast.Name):
defined_vars.add(target.)
(node, ast.Name) (node.ctx, ast.Load):
used_vars.add(node.)
undefined = used_vars - defined_vars - (.allowed_builtins)
var undefined:
issues.append({
: ,
: var,
:
})
safe = ([i i issues i[] [, ]]) ==
{
: ,
: issues,
: safe,
: defined_vars,
: used_vars
}
Step 4: Reasoning with Code Verification
class CodeVerifiedReasoning:
"""
High-level orchestration of code-integrated reasoning.
Manages generation, validation, execution, and correction.
"""
def __init__(self, model):
self.model = model
self.thinking_module = CodeIntegratedThinkingModule(model)
self.correction_mechanism = SelfCorrectionMechanism(model)
self.validator = CodeValidator()
def reason(self, question: str, max_attempts: int = 3) -> Dict:
"""
Complete reasoning pipeline with code verification.
"""
attempt = 0
current_result = None
while attempt < max_attempts:
current_result = self.thinking_module.generate_with_code_reasoning(question)
validation_status = {}
for i, code in enumerate(current_result['code_blocks']):
validation = self.validator.validate_code_block(code)
validation_status[i] = validation
if not validation['safe_to_execute']:
attempt += 1
break
else:
contradictions = .correction_mechanism.identify_contradictions(
current_result[],
current_result[]
)
contradictions:
{
: ,
: current_result,
: attempt +
}
attempt +=
{
: ,
: current_result,
: max_attempts
}
Practical Guidance
Code-Reasoning Integration:
- Embed code after every major computational claim (not after every sentence)
- Use print statements to output intermediate results for verification
- Keep code blocks focused (5-15 lines maximum)
- Comment code to connect with natural reasoning
Code Execution Safety:
- Whitelist allowed functions (math, list operations, string manipulation)
- Forbid file I/O, network access, and dangerous imports
- Set tight timeouts (5 seconds maximum per block)
- Validate AST before execution to catch common errors
Correction Strategy:
- First correction: Fix syntax errors and undefined variables
- Second correction: Address logical contradictions
- Third correction: Reconsider approach if still failing
- Stop after 3 attempts to avoid infinite loops
Performance Improvements:
- Math problems: +15-25% accuracy improvement
- Logic puzzles: +10-20% improvement
- Programming tasks: +20-30% improvement
- Reduces hallucination in numerical reasoning
When to Use CoRT:
- Mathematical reasoning (MATH, AIME, Calculus)
- Logic puzzles and symbolic manipulation
- Code generation and verification tasks
- Scientific problem-solving with numerical components
Reference
- Abstract Syntax Tree (AST): Enables safe code analysis without execution
- Subprocess isolation: Executes code in separate process for safety
- Type inference: Can detect variable scope issues statically
- Execution feedback: Code results ground reasoning in reality