This is a simple, working example of an AI agent using Cloudflare's 'Code Mode', based on the CodeACT pattern.
It is derived from Cloudflare's example at https://github.com/cloudflare/agents/tree/main/examples/codemode
It demonstrates how you can define a custom tool to be provided to the agent (see src/tools.ts)
- Install dependencies (
npm install) - Create a
.envfile with your OpenAI API key (see.env.example) - Run
npm startto start the development server - Visit
http://localhost:5173to see the demo - Try asking about the weather, e.g. "How's the weather today?"
Here's a sequence diagram showing how Code Mode works.

You may also like to read this blog post about Code Mode.