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.
Turn On Code Mode
Make these two changes to thetools array of a POST /v1/agent request:
- To turn on Code Mode, add the object
{"type": "programmatic_tool_calling"}. - To let programs call a tool, put
programmaticin theallowed_callersarray of that tool.
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.
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:
Set the Callers of a Tool
Theallowed_callers array on a tool sets how the model can call that tool.
These rules apply:
- A request can have only one
programmatic_tool_callingobject. The object accepts onlytype. - If a tool has
programmaticinallowed_callers, the request must have theprogrammatic_tool_callingobject. - An
mcptool acceptsallowed_callers. A program cannot call an MCP tool that needs approval. That call fails withToolError: code_mode cannot execute a tool call requiring client approval. - Custom functions accept
allowed_callers, including["programmatic"]and["direct", "programmatic"]. Add an optionaloutput_schemaJSON Schema on the function tool to describe the return shape to JavaScript programs. - When a request has the
programmatic_tool_callingobject, a custom function cannot have the nameexec. tool_choicecannot 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:
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, soPromise.allruns 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
ToolErrorwhen a tool call fails. A program can usetryandcatchto continue. - It cannot import modules.
processandfetchare not available. A program uses tools to get external data. - It does not keep variables for the next program.
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 tooltype. 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 tooutput. 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:
output. For example, a finance_quotes call adds a finance_results item, and a bash call adds a sandbox_results item.
Streaming
When you setstream to true, programs use the standard output item events:
- A
response.output_item.addedevent has theprogramitem when the program starts. - A
response.output_item.doneevent has theprogram_outputitem when the program stops. - A
response.output_item.doneevent has theprogramitem.
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 withprevious_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 returns400 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.