The @cloudflare/codemode package has been rewritten into a modular, runtime-agnostic SDK.
Code Mode enables LLMs to write and execute TypeScript that orchestrates your tools, instead of calling them one at a time. This can (and does) yield significant token savings, reduces context window pressure and improves overall model performance on a task.
The new Executor interface is runtime agnostic and comes with a prebuilt DynamicWorkerExecutor to run generated code in a Dynamic Worker Loader.
Breaking changes
- Removed
experimental_codemode()andCodeModeProxy— the package no longer owns an LLM call or model choice - New import path:
createCodeTool()is now exported from@cloudflare/codemode/ai
New features
createCodeTool()— Returns a standard AI SDKToolto use in your AI agents.Executorinterface — Minimalexecute(code, fns)contract. Implement for any code sandboxing primitive or runtime.
DynamicWorkerExecutor
Runs code in a Dynamic Worker. It comes with the following features:
- Network isolation —
fetch()andconnect()blocked by default (globalOutbound: null) when usingDynamicWorkerExecutor - Console capture —
console.log/warn/errorcaptured and returned inExecuteResult.logs - Execution timeout — Configurable via
timeoutoption (default 30s)
Usage
JavaScript
import { createCodeTool } from "@cloudflare/codemode/ai";import { DynamicWorkerExecutor } from "@cloudflare/codemode";import { streamText } from "ai";const executor = new DynamicWorkerExecutor({ loader: env.LOADER });const codemode = createCodeTool({ tools: myTools, executor });const result = streamText({model,tools: { codemode },messages,});TypeScript
import { createCodeTool } from "@cloudflare/codemode/ai";import { DynamicWorkerExecutor } from "@cloudflare/codemode";import { streamText } from "ai";const executor = new DynamicWorkerExecutor({ loader: env.LOADER });const codemode = createCodeTool({ tools: myTools, executor });const result = streamText({model,tools: { codemode },messages,});
Wrangler configuration
wrangler.jsonc
{"worker_loaders": [{ "binding": "LOADER" }],}wrangler.toml
[[worker_loaders]]binding = "LOADER"
See the Code Mode documentation for full API reference and examples.
Upgrade
npm i @cloudflare/codemode@latestSource: Cloudflare
Latest Posts
- (Updated) Toggle to new Outlook [MC949965]
![(Updated) Toggle to new Outlook [MC949965] 2 pexels pixabay 208821](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==)
- (Updated) Microsoft Teams: Find SharePoint agents in Teams chats and Teams Store [MC1193415]
![(Updated) Microsoft Teams: Find SharePoint agents in Teams chats and Teams Store [MC1193415] 3 pexels felixmittermeier 957040](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==)
- (Updated) Microsoft Copilot Notebooks: Overview page with Copilot-powered summaries and insights [MC1182698]
![(Updated) Microsoft Copilot Notebooks: Overview page with Copilot-powered summaries and insights [MC1182698] 4 pexels agk42 2599244.bak](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==)
- (Updated) Events in Meet app – Introducing a redesigned, unified Events experience in Microsoft Teams [MC1227087]
![(Updated) Events in Meet app – Introducing a redesigned, unified Events experience in Microsoft Teams [MC1227087] 5 pexels fauxels 3184419](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==)

![(Updated) Toggle to new Outlook [MC949965] 2 pexels pixabay 208821](https://mwpro.co.uk/wp-content/uploads/2024/08/pexels-pixabay-208821-150x150.webp)
![(Updated) Microsoft Teams: Find SharePoint agents in Teams chats and Teams Store [MC1193415] 3 pexels felixmittermeier 957040](https://mwpro.co.uk/wp-content/uploads/2024/08/pexels-felixmittermeier-957040-150x150.webp)
![(Updated) Microsoft Copilot Notebooks: Overview page with Copilot-powered summaries and insights [MC1182698] 4 pexels agk42 2599244.bak](https://mwpro.co.uk/wp-content/uploads/2024/08/pexels-agk42-2599244.bak_-150x150.webp)
![(Updated) Events in Meet app – Introducing a redesigned, unified Events experience in Microsoft Teams [MC1227087] 5 pexels fauxels 3184419](https://mwpro.co.uk/wp-content/uploads/2025/06/pexels-fauxels-3184419-150x150.webp)
