Skip to main content

Overview

With Code Mode, the model writes a JavaScript program that calls your tools. Perplexity runs the program and sends its output to the model. You do not write or run the program. One program can do work that needs many direct tool calls:
  • It can call a tool one time for each item in a list, in parallel.
  • It can use the result of one tool call as the input of the next tool call.
  • It can filter the tool results and send only the necessary data to the model.
Without Code Mode, the model makes each tool call directly and reads each result.

Turn On Code Mode

Make these two changes to the tools array of a POST /v1/agent request:
  1. To turn on Code Mode, add the object {"type": "programmatic_tool_calling"}.
  2. To let programs call a tool, put programmatic in the allowed_callers array of that tool.
The model needs two or more steps to read the output of one program and then write a new program. When you use model without an effort level, set max_steps. See Customize the loop. Version 0.50.0 of the Perplexity Python library and version 0.45.0 of @perplexity-ai/perplexity_ai do not have types for Code Mode. The examples on this page send HTTP requests directly.
The model wrote this program for the request. The program makes four searches in parallel and sends the results to the model:
The output array of the response has a program item, a program_output item, and the message item. The long values in this example are shortened:
The model can write a different program each time you send the same request.

Set the Callers of a Tool

The allowed_callers array on a tool sets how the model can call that tool. These rules apply:
  • A request can have only one programmatic_tool_calling object. The object accepts only type.
  • If a tool has programmatic in allowed_callers, the request must have the programmatic_tool_calling object.
  • An mcp tool accepts allowed_callers. A program cannot call an MCP tool that needs approval. That call fails with ToolError: code_mode cannot execute a tool call requiring client approval.
  • Custom functions accept allowed_callers, including ["programmatic"] and ["direct", "programmatic"]. Add an optional output_schema JSON Schema on the function tool to describe the return shape to JavaScript programs.
  • When a request has the programmatic_tool_calling object, a custom function cannot have the name exec.
  • tool_choice cannot select a tool that only programs can call.
When a request has the programmatic_tool_calling object, a direct web_search call is a function_call item with the name search_web in output. Its status is completed, and the response has no search_results item for that call. Perplexity runs this call. It is not a call to one of your custom functions.

Custom Functions in Code Mode

When a program calls a client-executed custom function (await tools.<name>(args)), Perplexity pauses the JavaScript runtime and returns the program item along with one or more function_call items. Each programmatic function_call item includes a caller object pointing back to the paused program:
Execute the function calls on your side, then send the matching function_call_output items back (including call_id, name, output, and caller) with previous_response_id — or by replaying the full conversation with the unchanged program item and its fingerprint. Perplexity resumes the paused JavaScript runtime and emits the final program_output and message items.

Programs

The model writes each program in JavaScript. A program has these properties:
  • It calls a tool with await tools.<name>(args). Each call returns a promise, so Promise.all runs calls in parallel.
  • It sends a value to the model with text(value). The model does not get the value of the last expression.
  • It gets the name and description of each available function from ALL_TOOLS.
  • It gets a ToolError when a tool call fails. A program can use try and catch to continue.
  • It cannot import modules. process and fetch are not available. A program uses tools to get external data.
  • It does not keep variables for the next program.
To change how the model writes programs, give directions in instructions or input. For example, tell the model to make one search for each item in parallel and to send only a short JSON array to the model.

Function Names

The function names in a program can be different from the tool type. One tool can give the program more than one function. The llm function uses a model to extract structured data from the items that the program gives it. Each MCP tool call that a program makes adds an mcp_call item to output.

Response Items

Each program adds two items to output. They have the same call_id. Use call_id to connect the two items, not their positions in output. A program item has the code that the model wrote: A program_output item has the result of the program: The parsed result is an object with these fields: A program that fails does not make the response fail. In this example, a tool call fails and the program does not catch the error. The response status is completed, and the status of the program_output item is incomplete. The parsed result keeps the output that the program sent before the error:
Some functions also add the usual item of their tool to output. For example, a finance_quotes call adds a finance_results item, and a bash call adds a sandbox_results item.

Streaming

When you set stream to true, programs use the standard output item events:
  1. A response.output_item.added event has the program item when the program starts.
  2. A response.output_item.done event has the program_output item when the program stops.
  3. A response.output_item.done event has the program item.
The program item in the response.output_item.added event has an empty fingerprint. Use the program item from the response.output_item.done event when you send the item in input. The stream has no delta events for program code or program output.

Usage

usage.tool_calls_details has entries for the tool calls that programs make. For example, when one program makes four search_web calls and each call completes, usage.tool_calls_details.search_web.invocation is 4. A program does not have an entry of its own.

Continue a Conversation

Code Mode works with previous_response_id. To manage the conversation yourself, send the program and program_output items back in input with the other output items. Do not change fingerprint. A program item with no fingerprint or a changed fingerprint returns 400. Code Mode also works with stream, background, store, and structured outputs.

Errors

The API returns 400 with type invalid_request for these requests. tools[N] gives the position of the tool in the tools array, and the first position is 0.

Next Steps

PyPI Release Watchlist

Run a recipe that compares one Code Mode request with direct tool calls.

Tools Overview

See the tools that an Agent API request can use.

Customize the Loop

Set the number of steps that a run can take.