Perplexity Code Mode: Power-User Guide to Agent API Programs, Setup and Limits

Last updated: December 2, 2026

Perplexity Code Mode is a new Agent API option, added in December 2026, that lets the model write a JavaScript program to call your tools. Perplexity runs the program, so one step can make many tool calls in parallel and send the model only the data it needs, which can sharply cut input tokens.

Contents

What Perplexity Code Mode is

Perplexity announced Code Mode in the December 2026 section of its API changelog (API changelog). The model writes a JavaScript program that calls built-in tools, MCP tools and custom functions, and Perplexity runs that program on its side.

The documentation lists three things a single program can do that would otherwise take many direct tool calls (Code Mode docs):

  • Call a tool once for each item in a list, in parallel.
  • Use the result of one tool call as the input of the next.
  • Filter 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. You never write or run the program; the model may write a different one each time.

If you are new to the Agent API itself, start with our Perplexity Agent API guide to effort levels, tool approvals and pricing.

Why it matters: one recorded comparison

Perplexity’s cookbook includes a release-watchlist script that checks ten PyPI packages twice: once with Code Mode and once with direct tool calls, using the same model, tools and output schema (Perplexity cookbook). Both runs returned the same ten rows. Here is the recorded run:

Metric Code Mode Direct tool calls
Programs 1 0
search_web calls 10 6
fetch_url calls 1 8
Input tokens 4,543 516,651
Output tokens 992 1,845
Total cost (USD) 0.03704 1.0111
Wall time (seconds) 10.3 29.9

The difference comes from what reaches the model. The program sent only the selected evidence, while the direct run sent fetched page text. Perplexity notes that results and metrics change on each run, so treat this as one example, not a benchmark.

How to turn on Code Mode

Code Mode is turned on per request. These steps follow the Code Mode documentation (Code Mode docs).

  1. Use the Agent API endpoint. Send a POST request to https://api.perplexity.ai/v1/agent with your API key as a Bearer token.
  2. Add the Code Mode marker. Put {"type": "programmatic_tool_calling"} in the tools array. Only one such object is allowed, and it accepts only type.
  3. Allow programs to call tools. On each tool programs may use, set allowed_callers, for example {"type": "web_search", "allowed_callers": ["programmatic"]}.
  4. Give the loop room. The model needs two or more steps to read one program’s output and write another. If you set model without an effort level, also set max_steps; the documentation example uses 5.
  5. Send plain HTTP for now. Python library 0.50.0 and @perplexity-ai/perplexity_ai 0.45.0 have no Code Mode types, so the official examples use httpx, fetch or cURL.
  6. Read the output items. Loop over output and handle program (the code), program_output (its status and result) and message (the answer).

The documentation example uses openai/gpt-5.6-sol, which the changelog prices at $5.00 per million input tokens and $30.00 output for inputs up to 272k tokens (API changelog). Its program ran four tools.search_web calls through Promise.all.

allowed_callers and program functions

The allowed_callers array decides who may call each tool:

  • ["programmatic"]: only programs call the tool.
  • ["direct", "programmatic"]: the model chooses for each call.
  • ["direct"] or omitted: direct calls only; programs cannot use it.

Inside a program, function names can differ from the tool type. For example, web_search exposes search_web, search_academic, search_secgov and llm, among others. sandbox exposes functions such as bash and grep.

Built-in tools still accept their usual options. For instance, snapshot_as_of filters from our Perplexity Time Machine guide to past-date search apply to web_search and fetch_url.

Custom functions and MCP tools

Custom functions accept allowed_callers, and an optional output_schema describes the return shape to programs. When a program calls one, it pauses. The response contains the program item and one function_call item per call, each with a caller object pointing to the program.

Run each function, then return a function_call_output item with the same call_id and caller, via previous_response_id and the same tools array.

MCP tools also accept allowed_callers and appear in programs as server_label__tool_name. If an MCP call needs approval, the program pauses with an mcp_approval_request item. A current limitation: when an MCP tool is programmatic-only, set defer_loading to true, or the approving request returns 400.

Reading program output, streaming and usage

Each program adds a program item (with code and an opaque fingerprint) and a program_output item sharing the same call_id. Match them by call_id, not position.

The result field is a JSON string in the code_mode/v1 format, holding the values sent with text() and, if the program failed, an error. A failed program does not fail the response: its status is incomplete, and output sent before the error is kept.

When streaming, take the program item from the response.output_item.done event, because the added event has an empty fingerprint.

In usage.tool_calls_details, calls made by programs are counted per tool, so four searches show search_web.invocation as 4. Web search costs $2.50 per 1,000 invocations, or $1.00 with search_type: "fast", plus model tokens (Web Search docs).

Code Mode also works with background, store and structured outputs. For content from known URLs, see our Perplexity Context API guide.

Prompt patterns and cost tips

  • Tell the model how to write the program. The documentation suggests directions in instructions or input, such as one search per item in parallel and only a short JSON array sent back.
  • Be specific about evidence. The cookbook prompt requires exact URL matches, one batched fetch_url call, and null instead of placeholders for unverified rows.
  • Handle errors in code. Failed tool calls raise ToolError, which programs can catch with try and catch.

For a coding-agent take on the same idea, see our Claude Code Code Mode guide.

Limitations and common errors

  • No imports or network access. Programs cannot import modules, and process and fetch are unavailable; external data must come through tools.
  • Reserved names. Custom functions cannot be named exec or exec_wait.
  • Fingerprints must be unchanged. Sending a program item without its original fingerprint returns 400.

Frequently asked questions

What is Perplexity Code Mode?

Code Mode is an Agent API feature that lets the model write a JavaScript program that calls your tools. Perplexity runs the program and returns its output to the model, so one program can make many tool calls in parallel and pass along only the data the model needs.

How do I turn on Code Mode in the Perplexity Agent API?

Add {“type”: “programmatic_tool_calling”} to the tools array of a POST /v1/agent request, then add “programmatic” to the allowed_callers array of each tool that programs may call. Sending a code_mode field instead returns a 400 error.

Does Code Mode cost extra?

Perplexity’s documentation does not list a separate charge for programs. Tool calls made inside a program are counted in usage, and web_search is billed at $2.50 per 1,000 invocations, or $1.00 with fast search, plus model tokens.

Which tools can a Code Mode program call?

Programs can call built-in tools such as web_search, fetch_url, finance_search, sandbox, people_search and image_search, plus remote MCP tools and your own custom functions, as long as each tool lists programmatic in allowed_callers.

Do the Perplexity SDKs support Code Mode?

Not yet with types. The documentation says Python library 0.50.0 and @perplexity-ai/perplexity_ai 0.45.0 have no types for Code Mode, so its examples send HTTP requests directly.

Sources

Related answers

Leave a Comment