Code Execution Agents

Write, Run, Observe, Fix

A code execution agent writes code, runs it in a sandboxed environment, observes the output or error, and uses that observation to iterate — until the code works correctly. This feedback loop makes it dramatically more capable than static code generation.

The Code-Execute-Observe Loop

1

Write — Agent generates code to accomplish the task.

2

Execute — Code runs in a sandboxed environment; stdout, stderr, and return value are captured.

3

Observe — Agent receives the output: correct result, error, or unexpected output.

4

Reason and revise — Agent analyzes the output, understands what went wrong, generates corrected code, and executes again. Repeats until correct or until the iteration limit is reached.

Sandboxing is Non-Negotiable

Code execution security

Without a sandbox, a code execution agent is a direct code injection vulnerability. Any prompt that tricks the agent into writing malicious code would execute it on the host system. Sandboxing — via containers, process isolation, or managed remote execution (E2B, Modal) — is required for any production code execution agent.

Handling Errors as Information

Syntax / Runtime Errors

Agent reads the error message or stack trace, identifies the problem line, reasons about the cause (wrong type, missing key, index error), and fixes it. Usually resolved in 1-2 iterations.

Wrong Output

Hardest case — no error, but incorrect result. Agent adds diagnostic output, re-runs to observe intermediate values, and traces the logic bug. Set an iteration limit to prevent infinite loops.

Need Help?

Ask the AI assistant about code execution agents, sandboxing approaches, or how to handle errors in the code-execute-observe loop.