Codemode lets LLMs write and execute code that orchestrates your tools, instead of calling them one at a time. Inspired by CodeAct, it works because LLMs are better at writing code than making individual tool calls — they have seen millions of lines of real-world TypeScript but only contrived tool-calling examples. TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/agents/llms.txt
Use this file to discover all available pages before exploring further.
@cloudflare/codemode package converts your tools into typed TypeScript APIs, gives the LLM a single “write code” tool, and executes the generated code in a secure, isolated Worker sandbox.
When to use Codemode
Codemode is most useful when the LLM needs to:- Chain multiple tool calls with logic between them (conditionals, loops, error handling)
- Compose results from different tools before returning
- Work with MCP servers that expose many fine-grained operations
- Perform multi-step workflows that would require many round-trips with standard tool calling
For simple, single tool calls, standard AI SDK tool calling is simpler and sufficient.
Installation
Quick Start
Create the codemode tool
createCodeTool takes your tools and an executor, and returns a single AI SDK tool:What the LLM writes
When the LLM decides to use codemode, it writes an async arrow function like:Configuration
Wrangler bindings
Add aworker_loaders binding to your wrangler.jsonc. This is the only binding required:
wrangler.jsonc
Vite configuration
If you usezod-to-ts (which codemode depends on), add a __filename define to your Vite config:
vite.config.ts
How it works
Type generation
createCodeTool generates TypeScript type definitions from your tools and builds a description the LLM can readTool dispatch
Inside the sandbox, a
Proxy intercepts codemode.* calls and routes them back to the host via Workers RPC (ToolDispatcher extends RpcTarget)Network isolation
Externalfetch() and connect() are blocked by default — enforced at the Workers runtime level via globalOutbound: null. Sandboxed code can only interact with the host through codemode.* tool calls.
To allow controlled outbound access, pass a Fetcher:
Using with an Agent
The typical pattern is to create the executor and codemode tool inside an Agent’s message handler:With MCP tools
MCP tools work the same way — merge them into the tool set:Tool names with hyphens or dots (common in MCP) are automatically sanitized to valid JavaScript identifiers (e.g.,
my-server.list-items becomes my_server_list_items).API Reference
createCodeTool(options)
Returns an AI SDK compatibleTool.
| Option | Type | Default | Description |
|---|---|---|---|
tools | ToolSet | ToolDescriptors | required | Your tools (AI SDK tool() or raw descriptors) |
executor | Executor | required | Where to run the generated code |
description | string | auto-generated | Custom tool description. Use {{types}} for type defs |
DynamicWorkerExecutor
Executes code in an isolated Cloudflare Worker viaWorkerLoader.
| Option | Type | Default | Description |
|---|---|---|---|
loader | WorkerLoader | required | Worker Loader binding from env.LOADER |
timeout | number | 30000 | Execution timeout in ms |
globalOutbound | Fetcher | null | null | Network access control. null = blocked, Fetcher = routed |
generateTypes(tools)
Generates TypeScript type definitions from your tools. Used internally bycreateCodeTool but exported for custom use (e.g., displaying types in a frontend).
sanitizeToolName(name)
Converts tool names into valid JavaScript identifiers.The Executor interface
TheExecutor interface is deliberately minimal — implement it to run code in any sandbox:
DynamicWorkerExecutor is the built-in Cloudflare Workers implementation. You can build your own for Node VM, QuickJS, containers, or any other sandbox.
Security Considerations
- Code runs in isolated Worker sandboxes — each execution gets its own Worker instance
- External network access (
fetch,connect) is blocked by default at the runtime level - Tool calls are dispatched via Workers RPC, not network requests
- Execution has a configurable timeout (default 30 seconds)
- Console output is captured separately and does not leak to the host
Current Limitations
- Requires Cloudflare Workers environment for
DynamicWorkerExecutor - Limited to JavaScript execution
- The
zod-to-tsdependency bundles the TypeScript compiler, which increases Worker size - LLM code quality depends on prompt engineering and model capability
Example
See the codemode example for a full working example — a project management assistant that uses codemode to orchestrate tasks, sprints, and comments via SQLite.Related Documentation
- Chat Agents — Full
AIChatAgentreference - MCP Integration — Using Model Context Protocol servers with agents