Skip to content

Code Mode

Code Mode is a tool-use pattern where a model writes code instead of requesting each operation separately. The model receives one code-execution tool. Its code becomes a compact plan that calls tools, processes results, and returns the information needed for a response.

Code Mode exposes configured tools as typed methods. Models use those methods to express multi-step work with familiar programming constructs. Depending on the integration, tool definitions can be provided up front or discovered when needed.

Code as a plan

With direct tool use, the model selects one tool, receives its result, and then decides what to do next. Each operation can require another round trip through the model.

Code Mode moves that intermediate logic into executable code. A single plan can:

  • Compose several dependent tool calls.
  • Loop over collections of results.
  • Filter and transform returned data.
  • Branch based on earlier results.
  • Shape the final returned value.

This approach keeps control flow and data handling together. It is useful when the model must coordinate several operations before producing an answer.

Progressive tool discovery

Large tool catalogs can consume significant model context if every definition is loaded up front. Connector-based Code Mode runtimes support progressive discovery through codemode.search() and codemode.describe().

Search finds relevant connectors, methods, and saved snippets. Describe returns detailed type information for a selected target. These results enter the running code, so the model can pull the definitions it needs instead of receiving the entire catalog with every request.

Choose between Code Mode and direct tool calls

With direct tool calls, each result returns to the model before it chooses the next operation. Intermediate data consumes context, even when the model only needs a small part of it for the final answer.

Code Mode keeps that work inside one sandbox execution. Generated code can pass results between tools, filter intermediate data, and return only the final value the model needs. This makes multi-tool tasks more efficient and easier to generalize or repeat.

Use direct tool calls for simple tasks with a small, fixed tool set. Use Code Mode when a task needs composition, dependent calls, progressive discovery, reusable logic, or control flow:

PatternBest suited for
Direct tool callsSimple tasks using a small, known tool set
Code ModeComposed or dependent calls, large tool catalogs, loops, branching, filtering, result shaping, or reusable logic

Choose an integration

Code Mode provides surfaces for agent runtimes, AI frameworks, browsers, and Model Context Protocol (MCP) systems.

Understand the pattern