Tools
Code Mode
Code mode is an experimental, opt-in OpenClaw agent-runtime feature. When
enabled, the model no longer sees every enabled tool schema. Instead, it sees
exec, wait, and any direct-only tool whose structured result cannot cross
the JSON-only guest bridge. The model writes a small JavaScript or TypeScript
program that searches, describes, and calls the hidden tool catalog.
This page documents OpenClaw Code Mode, not Codex Code Mode. The two features
share a name and the same control-tool names (exec, wait), but they are
separate implementations:
- Codex Code Mode runs inside the Codex coding harness. Its
exectool is a freeform-grammar tool: the model writes raw JavaScript source (optionally prefixed by a// @exec: {...}pragma line for execution options), executed in Codex's in-process V8 Code Mode runtime. - OpenClaw Code Mode runs in the generic OpenClaw agent runtime and is
enabled through global, agent, or model activation settings. Its
exectool takes a JSON{ code, language }payload, executed in a QuickJS-WASI worker.
Both are JavaScript execution surfaces, not shell-command surfaces. Treat them
as independent, differently-implemented features that happen to expose
identically-named exec/wait tools.
In OpenClaw Code Mode, command is a JavaScript or TypeScript alias for
code, not a shell command. For shell or file operations, call the appropriate
async tool global from guest JavaScript. Recognizable shell
commands are rejected before guest execution with actionable
invalid_input guidance.
Source validation, TypeScript compilation, and guest execution run in a bounded pool of worker threads that scales with available CPU cores. Workers stay warm between calls. Each cell gets an isolated QuickJS VM. Fast host exchanges retain that VM within the same call rather than snapshotting every await. Tool permissions, approvals, and session ownership remain with the Gateway. Queued work shares the execution deadline, and cancellation stops an active worker before the call settles.
This page is an index. Code Mode is documented on eight pages, one per reader job. Open the page that matches your task.
| Page | Read it when |
|---|---|
| Code Mode quickstart | You want to turn Code Mode on, override one model, and recover from tool errors. |
| Code Mode configuration | You need the configuration fields, the preferred-model list, and activation order. |
What it does
- The model-visible tool list becomes
exec,wait, plus any direct-only tool such ascomputeror the native-visionview_imageloader whose image result cannot survive the guest bridge. execevaluates model-generated JavaScript or TypeScript in an isolated QuickJS-WASI worker thread.- Every catalog-eligible enabled non-MCP tool (OpenClaw core, plugin, client) is
hidden as a standalone model tool and exposed inside the guest program as an
async global function. MCP stays under the
MCPnamespace. - The
execdescription carries a bounded quick index of final callable names, compact input hints, and compact declared output hints when a trusted tool provides an output schema. It omits descriptions, full schemas, MCP entries, and overflow entries. Callablecatalog.search(...)results are the fallback. Input hints retain integer and numeric bounds as comments, such asoffset?: number /* integer, >= 1 */. Other validation details remain in the full schema available throughdescribe(). These hints do not change tool validation or output contracts. - Guest code calls globals directly or searches the hidden catalog for callable
handles. A handle exposes bounded metadata and
describe(), but never the exact internal catalog id. Calls use the same execution path as normal agent turns (policy, approvals, hooks, telemetry all still apply). - MCP tools are grouped under the
MCPnamespace. In Code Mode this is the only supported way to call them. waitresumes a suspended Code Mode run when nested tool calls are still pending.
Call wait only when the outer Code Mode result has status: "waiting", using
its top-level runId. A completed cell can return a background shell operation
with its own sessionId inside value. Use the enabled process-control tool
inside a new exec to poll that operation. Its sessionId is not a Code Mode
run ID.
Code mode changes the model-facing orchestration surface only. It does not replace tools, plugin tools, MCP tools, auth, approval policy, channel behavior, or model selection.
Why use it
- Smaller prompt surface: providers get two control tools, a bounded native-tool index, and only the few required direct tools instead of dozens or hundreds of full tool schemas.
- Better orchestration: the model can use loops, joins, small transforms, conditional logic, and parallel nested tool calls inside one code cell.
- Fewer model round trips: a declared output contract lets the model call and
transform a tool result in one
exec. Unknown outputs remain raw-first. - Provider neutral: works for OpenClaw, plugin, MCP, and client tools without depending on provider-native code execution.
- Fails closed: if Code Mode is enabled but the QuickJS-WASI runtime is unavailable, the run fails instead of silently falling back to broad direct tool exposure.
Most useful for agents with a large enabled tool catalog, or workflows where the model needs to search, combine, and call several tools before answering.
Keep direct tool exposure for a small catalog or a model that does not reliably write short programs. Use Tool Search when you want a compact catalog but prefer structured search/describe/call controls instead of the QuickJS-WASI guest.
Technical tour
These pages cover the runtime contract and implementation details, for maintainers, plugin authors debugging tool exposure, and operators validating high-risk deployments.
| Page | Read it when |
|---|---|
| Code Mode tool surface | You need the exec and wait contracts, the hidden catalog, and collisions. |
| Code Mode guest API | You are writing guest code and need its globals, handles, and MCP namespaces. |
| Code Mode output | You need declared output contracts or the guest output API. |
| Code Mode internals | You need scope, terms, nested execution, snapshots, or the security boundary. |
| Code Mode troubleshooting | You need error codes, telemetry fields, or the debug environment variables. |
| Code Mode maintainer notes | You are changing Code Mode source, validating it, or writing E2E coverage. |
Where each section moved
Every section heading from the previous single-page version keeps its anchor
here, so an existing link such as /tools/code-mode#guest-runtime-api still
resolves. Each entry points at the page that now holds the content.
- Quickstart
- Enable code mode
- Override one model
- What the model does
- Recover from tool errors
- Verify the active surface
- Use Swarm for agent fan-out
- Configuration
- Automatic per-model activation
- The compat catalog flag
- Shipped preferred models
- Models shipped by more than one provider
- Choosing when to enable
- Activation
- Model-visible tools
- The exec tool
- Source in session history
- The wait tool
- Tool catalog
- Tool Search interaction
- Tool names and collisions
- Guest runtime API
- Reading paginated file data
- Declared output contracts
- Output API
- Runtime status
- Scope
- Terms
- Nested tool execution
- Run and snapshot lifecycle
- QuickJS-WASI runtime
- TypeScript
- Security boundary
- Error codes
- Telemetry
- Debugging
- Implementation layout
- Validation checklist
- E2E test plan
Related
- Swarm for fan-out agent orchestration from Code Mode scripts
- Tool Search
- Agent runtimes
- Exec tool
- Code execution