Skip to main content

Overview

Code Mode lets an AI assistant orchestrate tools by writing code. Large language models are good at writing code, often better than they are at chaining many individual tool calls. Code Mode puts that ability to work: instead of calling tools one at a time and reasoning about each result in the conversation, the assistant writes a short program that calls the tools it needs, and Code Mode runs that program in an isolated environment. Your application remains in control of which tools the program can reach and what they may do.

Tool Orchestration Through Code

Let the assistant write the code that coordinates your tools.

The assistant expresses a multi-step task as a program. Code Mode executes it in a sandbox where each tool is an ordinary function, while your application keeps control over access and execution.

Why Code Mode Exists​

With direct tool calling, every step of a task is a separate exchange with the model: call a tool, send the full result back, let the model decide the next call, and repeat. Loops, filtering, and combining results all happen one message at a time, and every intermediate result passes through the conversation.

Code is a better medium for that work. A model that can write a loop, branch on a value, or merge two lists can describe the whole plan at once. Code Mode gives the model a place to run that code safely: the program executes in an isolated environment, sees only the tools your application registered, and returns only the final result.

What You Get​

Orchestrate in code

The assistant uses loops, conditions, and data transformations to coordinate several dependent tool calls in one program.

Reduce model round trips

One program replaces a series of call-and-decide exchanges, so the model is not consulted between every tool call.

Use fewer tokens

Intermediate results stay inside the program, and the assistant looks up tool details only when it needs them instead of receiving the full catalog upfront.

Stay in control

Generated code runs isolated from your application. You choose which tools it can call and can require approval before sensitive actions.


Runs Where Your Application Runs​

Code Mode runs in the browser and in Node.js. The sandbox is a WebAssembly JavaScript engine that runs inside your application's own process. You don't need to provision, call, or pay for a separate code-execution service.

  • Secure: generated code, tool arguments, and tool results never leave your environment for a third-party sandbox.
  • Fast: a program starts locally, without a network round trip to a remote execution service.
  • Cheap: there is no hosted sandbox to operate and no per-execution fee. The only added cost is the local compute the program uses.

How It Works​

  • Step 1 Your application registers the tools the assistant may use.

  • Step 2 The assistant looks up the tools and input schemas it needs for the task.

  • Step 3 The assistant writes a program that calls those tools and combines their results.

  • Step 4 Code Mode runs the program in an isolated environment. Each tool call goes to your application, which carries out approved actions, and the program returns its result to the assistant.


When to Use It​

Code Mode is a good fit when:

  • a task needs several tool calls, especially when later calls depend on earlier results
  • the assistant needs to filter, sort, or combine tool results before answering
  • the tool catalog is large or changes over time
  • you want model-generated logic to run isolated from your application, with control over sensitive actions

For a task that needs one or two known tools, direct tool calls may be enough. Code Mode pays off when the work is naturally expressed as a program.

Next Steps​