Two-step script execution workflow for debugging when shell_agent and execute_code_sandbox consistently fail
Use this pattern when:
shell_agent fails repeatedly with unclear error messagesexecute_code_sandbox consistently errors or times outInstead of delegating execution to an agent or running inline code, use this two-step approach:
write_filerun_shell with python script.pyThis provides:
Use write_file to create a self-contained Python script:
write_file with:
path: "path/to/script_name.py"
content: |
#!/usr/bin/env python3
# Your complete script here
# Include imports, logic, and error handling
Best Practices:
Use run_shell to execute the script:
run_shell with:
command: "python path/to/script_name.py"
Best Practices:
shell_agent with:
task: "Load Excel file, calculate correlations, save results"
Result: Agent struggles with path handling, unclear errors
# Step 1: Write script
write_file with:
path: "correlation_analysis.py"
content: |
import pandas as pd
import sys
try:
# Load data
df = pd.read_excel('data.xlsx', sheet_name='Returns')
print(f"Loaded {len(df)} rows")
# Calculate correlation
corr = df.corr()
print(f"Correlation matrix shape: {corr.shape}")
# Save results
with pd.ExcelWriter('output.xlsx') as writer:
df.to_excel(writer, sheet_name='Returns')
corr.to_excel(writer, sheet_name='Correlation')
print("SUCCESS: output.xlsx created")
except Exception as e:
print(f"ERROR: {type(e).__name__}: {e}", file=sys.stderr)
sys.exit(1)
# Step 2: Execute script
run_shell with:
command: "python correlation_analysis.py"
run_shell with ls -la path/ to verify files existIf this pattern also fails:
run_shell with which python or python --versionrun_shell with ls -la script.pyrun_shell with /usr/bin/python script.py