← back

Code Mode: Let the Code do the Talking - Sunil Pai, Cloudflare

8.7K views · Apr 19, 2026 · 19:40 min · Watch on YouTube ↗
Takeaway

Drop JSON tool calls for code generation against a typed runtime — vastly fewer tokens, faster execution, and emergent state-machine inhabitation.

Summary

  • Sunil Pai (Cloudflare, PartyKit creator) advocates 'Code Mode': instead of JSON tool-call back-and-forth, have the model generate JavaScript that runs against a typed environment in one execution.
  • Worked example: Cloudflare's 2,600 API endpoints would consume 1.2M tokens as MCP tools; colleague Matt Carey collapsed it to two tools (search + execute) that take code strings — 99.9% token reduction to ~1,000 tokens.
  • DDoS-mitigation demo: 'find offending IPs and block them' becomes one code-generation pass vs. 8 round-trips through MCP.
  • Deeper insight via Kenton Varda's tic-tac-toe demo: the model didn't generate a separate tic-tac-toe app; it inspected the canvas stroke array and 'inhabited the state machine' to play directly — emergent behavior with no game code.
  • Frames code-mode as a new software architecture where LLMs let every user write 'custom-built interfaces' (like an engineer would with a script) instead of using lowest-common-denominator apps.
code-modetool-usecloudflare
Original description
Sunil Pai from Cloudflare discusses "Code Mode," an approach to interacting with AI agents where the model generates executable code (such as JavaScript) instead of relying on traditional JSON-based tool calling. This shift allows for more efficient, stateful, and complex system interactions.

Speaker info:
Sunil Pai created Partykit, the open source tool for real-time multi-player apps. For his day job, he builds AI Agents at Cloudflare.
- https://sunilpai.dev/
- blog.cloudflare.com/author/sunil/
- linkedin.com/in/sunil-pai-a47732253/

Timestamps
0:00 Introduction and speaker background
1:16 What is "Code Mode"?
1:31 Limitations of traditional tool calling at scale
2:03 The shift to generating executable code
3:01 Scaling API usage at Cloudflare
4:05 Why code generation is more efficient
5:28 Live demonstration of the Mythical server
7:20 A new way of interacting with systems
9:09 Example: The Kenton canvas and tic-tac-toe anecdote
11:46 New software architecture: The "Harness"
13:28 Observability and security in sandboxed environments
14:15 Long-running workflows and generative UI
16:41 Future outlook: Building for the next generation of users
17:50 The resurgence of capability-based security
18:33 Conclusion and final thoughts