You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/use-cases/code-mode.mdx
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,18 @@ icon: "code"
8
8
Full working example with SecureExecExecutor and tool dispatch.
9
9
</Card>
10
10
11
-
Instead of calling tools one at a time (requiring round-trips through the LLM), Code Mode gives the agent a single "execute code" tool. The LLM writes JavaScript that chains multiple tool calls in one shot, and Secure Exec runs it in an isolated V8 sandbox.
11
+
Instead of calling MCP tools one at a time, Code Mode lets the LLM write JavaScript that orchestrates everything in one go — run safely in a V8 sandbox by Secure Exec.
12
12
13
-
This pattern, introduced by [Cloudflare's Code Mode](https://blog.cloudflare.com/code-mode/), works because LLMs are better at writing code than calling tools. They have millions of lines of real-world code in their training data but limited exposure to synthetic tool-calling formats.
13
+
<Tip>
14
+
[MCP Toolkit](https://mcp-toolkit.nuxt.dev/advanced/code-mode) provides a premade Code Mode library powered by Secure Exec: `experimental_codeMode: true`. We recommend trying it first. The rest of this page covers how to implement Code Mode yourself.
15
+
</Tip>
16
+
17
+
## Why Code Mode
18
+
19
+
-**[81% less token overhead](https://x.com/hugorcd/status/2034616192225407273)**: With 50 tools, replacing per-call tool descriptions with a single code-execution tool cuts tool description tokens by 81%
20
+
-**Fewer round-trips**: Chain multiple tool calls, conditionals, and data transformations in a single execution
21
+
-**Real control flow**: Loops, branching, `Promise.all` — not a chain of isolated tool calls
22
+
-**Drop-in replacement**: Your existing tools don't change at all. Code Mode wraps them transparently.
14
23
15
24
## How it works
16
25
@@ -72,18 +81,9 @@ Three tool calls, one sandbox execution, zero extra LLM round-trips.
72
81
73
82
See the [full working example](https://github.com/rivet-dev/secure-exec/tree/main/examples/code-mode) for the complete implementation including the `SecureExecExecutor`.
74
83
75
-
## Why Code Mode
76
-
77
-
-**Fewer round-trips**: Chain multiple tool calls, conditionals, and data transformations in a single execution
78
-
-**Lower token usage**: Intermediate results stay in the sandbox instead of passing back through the LLM context
79
-
-**Better tool handling**: LLMs manage more tools and greater complexity when presented as TypeScript APIs vs. raw tool definitions
80
-
81
-
## Status
82
-
83
-
Code Mode support in Secure Exec is early. Today you need to copy the [`SecureExecExecutor` adapter](https://github.com/rivet-dev/secure-exec/tree/main/examples/code-mode) from the example into your project. We're planning official first-party support.
0 commit comments