> ## Documentation Index
> Fetch the complete documentation index at: https://docs.restate.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Code mode and tool search

> Let the model combine tool calls in one JavaScript program, and keep large tool catalogs out of each model request.

Code mode and tool search keep the model's context small when an agent has many tools. Code mode keeps intermediate tool results out of the context, and tool search keeps the definitions of deferred tools out of each request until the model searches for them.

## Code mode

Code mode is on by default. When the agent has a tool that a program can call, the run adds the built-in `run_program` tool, which lets the model write one JavaScript program that calls several tools. Only the program's result is added to the model's context.
Programs run in a QuickJS sandbox without network or file system access. Their tool calls go through the same guardrails and approvals as direct calls, and count toward the agent's `maxToolCalls`.

To limit which tools a program can call, add a `programTool` from `@restatedev/harness/program`. It replaces the built-in `run_program`. Set `codeMode: false` to turn code mode off:

```typescript {"CODE_LOAD::harness/src/tools.ts#code_mode"}  theme={null}
const ticketAgent = agent({
  model,
  tools: {
    lookupTicket,
    issueRefund,
    // Programs can call only lookupTicket, at most 20 times per program
    program: programTool({tools: ["lookupTicket"], limits: {maxToolCalls: 20}}),
  },
});

const directAgent = agent({model, tools: {lookupTicket}, codeMode: false}); // no run_program
```

While code mode adds `run_program` to a run, no other tool can have that name: `run` throws a `TypeError` when it starts, and Restate retries the invocation.

## Tool search

Tool search is on by default. A tool with `deferred: true` isn't listed in the model request until the model finds it with the built-in `search_tools` tool.
Tools from an [MCP server](https://docs.restate.dev/harness/mcp.md) are deferred by default.

To list deferred tools from the start, set `toolSearch: false`:

```typescript {"CODE_LOAD::harness/src/tools.ts#tool_search"}  theme={null}
const catalogAgent = agent({
  model,
  tools: {lookupTicket, refunds: serviceTool({iface: refundIface, deferred: true})},
  toolSearch: false, // list the deferred refunds tool from the start
});
```

While tool search adds `search_tools` to a run, which it does when a tool is deferred, no other tool can have that name: `run` throws a `TypeError` when it starts, and Restate retries the invocation.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.