Overview
The @cloudflare/codemode package has undergone a complete rewrite to provide a modular, runtime-agnostic SDK for Code Mode—a feature that enables LLMs to write and execute code that orchestrates tools instead of calling them sequentially. This approach yields significant token savings, reduces context window pressure, and improves overall model performance.
Breaking Changes
The rewrite introduces several breaking changes developers must be aware of:
- Removed
experimental_codemode()andCodeModeProxy— The package no longer owns LLM calls or model selection - New import path —
createCodeTool()is now exported from@cloudflare/codemode/ai(previously integrated differently)
New Architecture
The SDK introduces a minimal Executor interface with a simple execute(code, fns) contract, allowing implementation for any code sandboxing primitive or runtime. This design enables developers to bring their own execution environments.
The DynamicWorkerExecutor is the prebuilt implementation for Cloudflare Workers, offering:
- Network isolation —
fetch()andconnect()are blocked by default when usingglobalOutbound: null - Console capture —
console.log/warn/erroroutputs are captured and returned inExecuteResult.logs - Configurable execution timeout — Default 30 seconds, adjustable via options
Getting Started
The new createCodeTool() returns a standard AI SDK Tool for use in AI agents. Basic integration requires:
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 });
Configure your wrangler.toml or wrangler.jsonc with a worker loader binding:
[[worker_loaders]]
binding = "LOADER"
Action item: Update to @cloudflare/codemode@latest and refactor code that uses the removed experimental_codemode() function. See the Code Mode documentation for full API reference and migration examples.