Back to Blog

Let the Agent Write the Script

What Perplexity’s Code Mode changes when you pay for every tool call

I build small AI products on my own from Maidenhead: a calorie counter that reads a photo, a workout planner, a sleep app, a UK listings site. The cost I watch is not the one-line answer. It is the loop. Search, fetch, parse, search again. Each step is another round trip, and each round trip pastes raw text back into the prompt.

On 3 December 2026 Perplexity published Code Mode for the Agent API. Their changelog lists the presets that turn it on as high@2026-12-01.0, xhigh@2026-12-01.0 and ultra@2026-12-01.0. The idea is one I should have been stricter about in my own code. Stop handing the model every tool result. Let it write a short script that calls the tools, throws most of the payload away, and returns a summary.

What actually shipped

In the ordinary tool loop the model asks for one call, waits, reads the whole result, then asks for the next. Code Mode, also called programmatic_tool_calling, replaces that chain with a JavaScript program. Perplexity runs it. You do not write it, and you do not run it. The program calls the tools. Only values passed to text() go back to the model. The docs say the model does not see the value of the last expression, so a script that forgets text() comes back empty.

This sits on Search as Code, which they shipped in June: an agent ran Python against a search library inside a Linux sandbox. Code Mode keeps the useful part of that — fan-out, branching, deduplication, and filtering payloads outside the context window — and drops the container for the common case. The script runs in an embedded QuickJS runtime, inside the agent loop. It calls tools through one tools.* interface: web search, fetching a URL, people search, finance search, MCP servers, and the sandbox file and shell tools.

The boundary around that script is the part I trust. A program cannot import modules. process and fetch are not available, so outside data has to come through a tool. A failed call raises ToolError, and the script can catch it. Variables do not carry into the next program. Parallel work is Promise.all.

The numbers, and what they do not prove

Perplexity measured this on WANDR, their wide-research benchmark. They scored the first 100 of 500 tasks with the public harness, using what they call soft recall: quality-adjusted completion, with partial credit for incomplete records. Cost is the average per task. I have not reproduced the runs.

They split the change in two. First, newer models, still on direct tool calls, against the 5 November presets. On xhigh, Sol-6.2 went from 76.5% at $7.30 a task to 78.7% at $5.57. On ultra, Opus 5.5 plus three Sol-6.2 runs went from 85.9% at $21.71 to 88.5% at $12.12.

Then they turned Code Mode on with those same models. Sol-6.2 plus Code Mode reached 80.5% at $3.85 a task, which they describe as 47% less than the 5 November xhigh. The ultra mix reached 89.4% at $10.30, 53% less than the 5 November ultra.

The line that matters on a small budget is the mid tier. They say earlier Code Mode tries on high and below scored worse and cost more, because the models made a mess of the scripts and spent extra steps recovering. With newer mid-tier models, high plus Code Mode reached 47.7% at $0.38 a task, 2.9 points above their baseline, with no hand-written research workflow. The model searched in parallel, deduplicated in JavaScript, wrote the raw pages to disk, and sent back a short summary.

A separate test held the spend fixed and changed only the scheduling. If a script is still running after 10 seconds, exec returns the partial output and the script continues in the background. The agent collects the rest with exec_wait. On paired WANDR runs that cut the median from 28.9 minutes to 25.3 (12.5%), the 95th percentile from 97.3 minutes to 57.5 (40.9%), and the longest run from 174.2 minutes to 75.6 (56.6%).

Read the score as a research workload. WANDR is many searches and many pages. A calorie photo or a listing screen is a different shape. The mechanism is what I can reuse. The percentage is not a forecast for my apps.

Where the money goes in a small product

When a feature gets expensive, the usual cause is that the model read JSON it did not need. A screening question on a listings site has a short useful answer: address, price, the fields someone asked for, and the source URL. I have not built that as an agent. The naive shape is still obvious. Fetch every page, stuff the HTML into the transcript, then pay the model to do what a few lines of code could have done first. Their finance write-up is the same move: batch the calls, save the raw filings, and emit a summary.

On the December high preset they also removed the cap of 40 tool calls per run. The cap of 40 steps stayed. A script can make many calls inside one step. Steps are model turns. Calls inside the script are not. That is why the loop gets cheaper, and why a bad script can still run up a bill. On high those calls are no longer capped, and they are no longer paced by a round trip.

What I would change, and what I would leave

I am not pulling the photo path out of SimplyAI, or the session planner out of Trainify. Those features are narrow. One image in, a structured estimate out. Deep Rest should not be fanning out across the web at all. A script in the middle does not help.

I would use it where the work wanders across many listings, documents, or search results, and I would tell the model to return only a short JSON array. Anything that spends money or writes a user record stays as my own function. When a program calls one of your functions, Perplexity pauses. The HTTP response is still completed. You get the program source and a function_call for each call, not a program_output yet. You run the function and post the result back, with the caller object that points at the paused program. A public search can live in the script. A database write should hit my server, where I can refuse it.

I would log that program. A script that throws does not fail the HTTP response. program_output comes back incomplete, with the error and any text emitted before the crash. The assistant message alone will not tell a bad query from a bad summary.

I would pin the previous preset until I had my own traffic beside it. high@2026-11-11.0 keeps direct tool calls and the earlier limits. xhigh@2026-11-18.0 does the same for that tier. The December high preset turns Code Mode on, and by default web search, fetch, and the sandbox run only from programs. Version 0.50.0 of their Python library, and version 0.45.0 of @perplexity-ai/perplexity_ai, do not have types for Code Mode. That preset also bills container time if a run starts the sandbox. Code Mode does not need the container for search and fetch, so on a listing feature I would leave the sandbox off.

The part the benchmark leaves to you

A script is still model output. It can call the wrong tool, drop a row, or summarise a page it misread. Perplexity say this used to lose money below the top tier, because models spent the savings repairing syntax. Newer models made that overhead small enough for high to come out ahead on their benchmark. The win still depends on the model writing a small program correctly.

I do not need that program to be clever. I need it to be boring: fan out, filter, deduplicate, hand back ten lines. Judgement happens afterwards, on the short text. That split is worth taking even if I never call this API. The question for a solo build is which features paste raw tool output into the prompt, and which of them would be cheaper with a script in between.

Sources