Perplexity has added Code Mode to its Agent API. Instead of calling its tools one at a time and feeding every result back through the model, the agent writes one short script that calls them together and returns only what matters. On Perplexity’s own WANDR benchmark, the change lifted the xhigh effort level from 78.7% to 80.5% and cut the cost per task by 31%. I ran the same pattern on OpenAI and Claude with a bulky, deterministic task, and the model read between 16 and 50 times fewer input tokens when it wrote a script. The details are in Perplexity’s post, and the news came from the Perplexity Developers account on X.
Disclosure: I’m an OpenAI, Firecrawl and Devin ambassador, and GPT-6.2 Sol is one of the models in my test.
What Perplexity shipped
Code Mode is a tool called programmatic_tool_calling. With it on, the model writes JavaScript that calls the agent’s tools directly, runs the calls in parallel, deduplicates and filters the results in code, and hands back a compact answer. The script runs inside the agent loop in an embedded QuickJS WebAssembly runtime, so there’s no container to start. Perplexity’s earlier approach, Search as Code from June, ran Python against its own SDK inside a Linux sandbox.
Any tool the agent has enabled can be called from the script through one tools.* interface: web search, page fetch, people search, finance data, MCP servers and sandbox file and shell tools. Scripts that run past 10 seconds return partial output while they keep going, and the agent can carry on with other work and collect the result later with exec_wait. In paired WANDR runs at the same spend, that cut median duration from 28.9 to 25.3 minutes and the slowest run from 174.2 to 75.6 minutes.

| Effort level | Before Code Mode | With Code Mode |
|---|---|---|
high | Direct tool calls | 47.7% at $0.38 a task, 2.9 points up |
xhigh | 78.7% at $5.57 a task | 80.5% at $3.85 a task |
ultra | 88.5% at $12.12 a task | 89.4% at $10.30 a task |
The high result is the new one. Perplexity says earlier attempts at Code Mode on mid-tier models scored lower and cost more than direct calling, because those models made syntax mistakes and misread tool output. With newer mid-tier models it now pays off, and Perplexity expects most of its agent loops to run this way eventually.
Turning it on
The latest high, xhigh and ultra effort levels have it on by default. For a custom request, add the programmatic_tool_calling tool and put programmatic in allowed_callers on each tool a script may call. This is adapted from Perplexity’s docs:
{
"model": "openai/gpt-5.6-sol",
"input": "Find the current stable release of Python, Node.js, Go, and Rust. Search for each one. Then give me a four-row table.",
"tools": [
{"type": "programmatic_tool_calling"},
{"type": "web_search", "allowed_callers": ["programmatic"]}
],
"max_steps": 5
}

A few details from the docs matter before you flip it on. A program shows up in the response as a program item followed by a program_output item. The high level now includes the sandbox tool, and a run that starts a sandbox container is billed for container time, at $0.03 per started 20-minute block. To keep direct tool calls on xhigh, pin [email protected]. An MCP tool that only programs may call needs defer_loading set to true for now, or the request returns a 400. When a program calls one of your own functions, it pauses until you return the result.
What I ran
I ran the pattern on OpenAI’s and Anthropic’s APIs, not Perplexity’s. Perplexity’s Agent API takes the same field names as OpenAI’s Responses API, including programmatic_tool_calling, allowed_callers and the program and program_output items, so the request shape carries over. Anthropic calls its version programmatic tool calling too, and its scripts are Python.
The task is built so the answer can only come from the data. A mock order system has eight regions, each with 150 orders of about 41 KB of JSON, and the tools are list_regions, get_orders and get_fx_rate. Together that’s 17 tool calls. The prompt was the same for every run:
Our order system has several regions. For each region, work out the total USD revenue from orders whose status is 'paid' (qty x unit_price, converted to USD with the region currency's fx rate). Then tell me the top 3 regions by USD revenue with each total rounded to the nearest dollar. Use the tools; do not guess.
The correct answer is Austin at $59,786, London at $59,749 and Berlin at $55,595. London trails Austin by $37, so rough arithmetic gets the order wrong. I ran each setup five times, once with the three tools called directly and once with them marked callable only from a script. The models were GPT-6.2 Sol and Claude Sonnet 5.5. Five runs each makes these smoke tests.
The machine was a 4 vCPU Intel Xeon at 2.3 GHz with 15 GB of memory and a 252 GB disk, running Ubuntu 24.04.5 on kernel 6.18 with Python 3.13. The model calls dominate the time, and the box only ran the mock tools.

| Setup | API requests | Input tokens | Output tokens | Seconds | Correct |
|---|---|---|---|---|---|
| GPT-6.2 Sol, direct | 4 | 197,143 | 5,964 | 86.6 | 5 of 5 |
| GPT-6.2 Sol, script | 11 | 3,929 | 367 | 16.9 | 5 of 5 |
| Claude Sonnet 5.5, direct | 4 | 281,751 | 17,215 | 94.8 | 5 of 5 |
| Claude Sonnet 5.5, script | 18 | 17,837 | 848 | 24.5 | 5 of 5 |

Every run was correct, direct or scripted. The scripted runs were faster and used far fewer tokens because the 41 KB payloads never reached the model. Claude wrote about 17,000 output tokens working through the data directly, and about 850 when it wrote a script. My first Claude direct attempt ran out of its 16,000-token output cap before answering, so I raised the cap to 32,000 for all the Claude runs and report the five runs under it.
The scripted runs made more API requests, 11 and 18 against 4. That’s down to my setup. My three tools were client-owned functions, so each batch of calls paused the script until my harness returned the results. Perplexity’s built-in tools run inside its own loop, so a script that only calls them wouldn’t pause for a client at all. Here is what GPT-6.2 Sol wrote for the second and third programs:
const regions=["Auckland","Sydney","Singapore","Tokyo","London","Berlin","Toronto","Austin"];
const orders=await Promise.all(regions.map(region=>tools.get_orders({region})));
text(regions.map((region,i)=>{const sums={}; for(const o of orders[i]) if(o.status==="paid") sums[o.currency]=(sums[o.currency]??0)+o.qty*o.unit_price; return {region,sums};}));
const rows=[["Auckland","NZD",50492.38],["Sydney","AUD",47842.16],["Singapore","SGD",43827.82],["Tokyo","JPY",6979025],["London","GBP",47046.72],["Berlin","EUR",51004.41],["Toronto","CAD",42679.78],["Austin","USD",59785.99]];
const totals=[];
for(const [region,currency,total] of rows){const fx=await tools.get_fx_rate({currency}); totals.push({region,fx,usd:total*fx.to_usd});}
totals.sort((a,b)=>b.usd-a.usd);
text(totals);
The first of these fetched the orders and summed them by currency inside the runtime. The model read only the eight small subtotals, then wrote the next script for the exchange rates. Claude’s script did the same job in Python:
fx={}
for c in ['NZD','AUD','SGD','JPY','GBP','EUR','CAD','USD']:
fx[c]=json.loads(await get_fx_rate({"currency":c}))
print(fx)
res={}
for x in r:
o=data[x]; c=o[0]['currency']
res[x]=sum(d['qty']*d['unit_price'] for d in o if d['status']=='paid')*fx[c]['to_usd']
for k,v in sorted(res.items(),key=lambda t:-t[1]): print(k,round(v,2))
I didn’t test Perplexity’s Agent API or reproduce its WANDR numbers. Those are Perplexity’s own evidence, and my runs show that the pattern behind them works as described on two other providers.
Not a new idea
Several vendors now offer this under different names. I’ve explained code mode and Anthropic’s code execution with MCP before, and OpenCode has pulled its runtime out as Psychoscript.
| Provider | Where the script runs | Language | Notes |
|---|---|---|---|
| Perplexity | Embedded QuickJS WebAssembly runtime inside the agent loop | JavaScript | Built-in tools, MCP and custom functions |
| OpenAI | Fresh isolated V8 runtime, no network or filesystem | JavaScript | Works with zero data retention; functions, MCP, shell, code interpreter |
| Anthropic | Code execution container | Python | Stable; not eligible for zero data retention, data kept up to 30 days |
| Cloudflare | Isolated Worker | JavaScript | Its API server exposes two tools in about 1,000 tokens instead of 1.17 million |
The MCP project’s client best practices now describe the same technique, and note that a client has to supply the sandbox.
When direct calls still win
OpenAI’s guidance is the clearest on this. A script suits work where code can filter, join, rank or deduplicate many results into something smaller, and where the control flow is predictable. Direct calls suit a single lookup, a search where each result should change the next step, anything that writes or needs approval, and final citation checks. Perplexity’s own rollout agrees: until now it kept Code Mode to its top two effort levels.
My test sat firmly in the first group, with eight bulky payloads and a fixed calculation. A task of three small lookups would show little gain. If you try it, compare score, cost and latency on your own workload, as Perplexity suggests, and keep writes and approvals on direct calls.
Sources and further reading:
- Introducing Code Mode in the Agent API, Perplexity
- Code Mode, Perplexity docs
- Sandbox, Perplexity docs
- Rethinking search as code generation, Perplexity
- Programmatic Tool Calling, OpenAI
- Programmatic tool calling, Anthropic
- Code Mode: give agents an entire API in 1,000 tokens, Cloudflare
- Perplexity Developers on X




