← Back to Articles
Single Agent Patterns • Part 5

Single Agent Patterns: Code as Action

Intent

Use executable code rather than a fixed tool vocabulary as the agent's action space. Because code composes—loops, conditionals, variables—a single action can express what would otherwise take many discrete tool calls.

Introduction

Traditional agent architectures rely on a predefined JSON tool schema vocabulary (e.g., search_web(), fetch_url(), calc()). When a task requires iterating through 50 data records, filtering items, and aggregating numerical statistics, a standard ReAct loop must make 50 individual LLM reasoning turns—one for each tool invocation. This results in extreme latency, massive token consumption, and risk of context drift. The Code as Action pattern replaces rigid tool calls by empowering the agent to write and execute full code scripts (such as Python or JavaScript) directly in a runtime interpreter, turning code itself into the action space.

Collapsing Round-Trips via Expressive Composition

Code is inherently composable. It natively supports control flow primitives—for loops, if/else branches, local variables, and module imports. By generating code, an agent can perform multi-step data manipulation inside a single execution turn:

Write Script Distilled Result Agent Core Generates Script Composite Action Code Interpreter Python / JS Sandbox Loops & Filtering Final Observation Clean Summary 1 Turn Completed

Mandatory Security Coupling with Sandboxing

Using code as an action space significantly increases the system's attack surface. Allowing an LLM to generate and execute unrestricted shell commands or Python scripts on a host system is an existential risk. Consequently, the Code as Action pattern is tightly coupled with Sandboxing and Permissioning. The code interpreter must execute inside isolated microVMs, with restricted disk mounts and network egress controls to prevent accidental or malicious system compromise.

Trade-offs

Adopting Code as Action yields immense performance and flexibility, but introduces specific execution risks:

Massive Turn Compression

Collapses complex loops, filtering, and multi-file processing from dozens of ReAct turns into a single code generation step.

Exact Calculation & Parsing

Offloads mathematical computations, data transformations, and regex parsing to a deterministic programming language runtime.

Enlarged Attack Surface

Requires strict microVM sandboxing, egress proxies, and resource timeout enforcement to prevent code execution vulnerability exploitation.

Silent Partial Failures

A script that partially succeeds (e.g. processes 10 of 20 files before crashing) can leave the environment in an inconsistent state.

Known Uses

Code as Action is widely used in state-of-the-art coding and reasoning agents:

References