ai icon indicating copy to clipboard operation
ai copied to clipboard

[Feature Request]: Code snippet execution instead of individual tool-calling

Open nileshtrivedi opened this issue 2 months ago • 2 comments

Description

This pattern is known as CodeAct. Cloudflare recently published a blog post and added this pattern, calling it "Code Mode", not to be confused with ai-sdk's codemods.

The core idea is that tool-calling is unnatural to LLMs. There is a lot more training data for writing code, instead of tool-calls using special tokens. With V8 isolates, it is possible to evaluate LLM-generated code safely. So, we can generate a Typescript API from the tools MCP discovers, and ask LLM to write a code snippet instead of a single tool call. And then we execute this snippet safely in an isolate.

As the paper shows, there are many advantages of this approach. But Typescript and Isolates make it pragmatic.

It would be great to have this ability in ai-sdk natively, irrespective of how tools are discovered.

In essence, this feature request is about re-implementing codeact implementation from Cloudflare's Agent SDK in ai-sdk. AFAIK, Cloudflare's implementation only supports sandboxes in their infra. But there's no reason why code snippet execution cannot be done locally (in NodeJS). workerd runs locally but is an entire runtime and might be unnecessary.

AI SDK Version

ai: 5.0

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

nileshtrivedi avatar Oct 04 '25 21:10 nileshtrivedi

For people coming here, sandboxing is required for this to be production-ready and not be a hell of a security headache. I'm implementing this with Vercel Sandboxes rn. It's the most practical if you run everything in Vercel.

carloslfu avatar Nov 10 '25 02:11 carloslfu

Anthropic recently published a blog post about this approach.

They also open-sourced their sandboxing runtime which Claude Code on the Web uses and published a post about it.

nileshtrivedi avatar Nov 14 '25 00:11 nileshtrivedi

@carloslfu codeact + Vercel Sandboxes is something we want to look into once we ship v6 stable. If you have any findings / insights, please share!

gr2m avatar Nov 22 '25 19:11 gr2m

New open-source and more portable tooling for code mode is now emerging:

  • https://portofcontext.com/
  • https://github.com/aberemia24/code-executor-MCP

nileshtrivedi avatar Dec 04 '25 16:12 nileshtrivedi