By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Letting the agent run code turns 'the model does arithmetic in its head' into 'the model writes a calculation and checks the result'. It is also the most dangerous tool in the box, so it needs the strictest wrapper.
| Use case | Why code beats prose | Risk |
|---|---|---|
| Data analysis | Exact numbers over a real dataframe | Filesystem and network access |
| Chart generation | Deterministic output you can re-render | Arbitrary imports |
| Glue between tools | Loops and retries without spending tokens | Infinite loops |
| Validation | Parse and check structured output | Resource exhaustion |
You will not build a sandbox in this course, but you must know what it has to guarantee before you offer execution as a tool:
while True must die, not run until the invoice arrives.[0] * 10**9 should fail cleanly.def run_python(source: str, timeout_s: int = 10) -> dict:
"""Execute in an isolated runner. Never in this process."""
result = sandbox.run(source, timeout=timeout_s, network=False, memory_mb=256)
return {
"exit_code": result.exit_code,
"stdout": result.stdout[:4000], # cap it
"stderr": result.stderr[:4000],
}Key takeaways
Code execution makes maths, charts and validation exact instead of approximate.
The wrapper needs a time limit, memory cap, no network and no credentials.
Cap output size before it reaches the context window.
Treat stdout and tracebacks as untrusted text.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.