Build a Search as Code agent with the Perplexity API
Let an AI model write and run its own search code in the Perplexity Agent API sandbox, for research with many parts, in about 20 minutes.

What you need
Step 1: Create a key and add credits
4 minThe Agent API runs on prepaid credits, and every request needs a key from your API project.
- Go to console.perplexity.ai and sign in with your Perplexity account.
- Under Project, open Settings and fill in your project details if it asks.
- In Billing, click Add payment method, then buy credits.
- On the API Keys page, generate a key and copy it right away.
You should see a new key that starts with
pplx-, shown on screen once.Your key stops working after a few runs?
You've probably run out of credits, and Perplexity blocks keys at a zero balance. Buy more credits, or turn on Auto reload in Billing.
Perplexity shows the full key only once. Save it in a password manager, and keep it out of code, prompts and shared files.
Step 2: Install the SDK and set your key
3 minThe Python SDK sends your requests and reads the key from your environment, so the key never sits in your code.
- Open a terminal.
- Run the commands below, with your key in place of [YOUR API KEY].
- Check the install with
pip show perplexityai.
Run thismkdir sac-agent && cd sac-agent python3 -m venv .venv source .venv/bin/activate pip install perplexityai export PERPLEXITY_API_KEY="[YOUR API KEY]"
You should see the install finish with no errors, and a version number from
pip show perplexityai.pip can't find a matching version?
The package needs Python 3.10 or newer. Check with
python3 --version, install a newer Python, then make the.venvagain.The
exportline lasts only for this terminal window. In a new window, run it again before you run the script.Step 3: Describe the whole task
5 minThe model plans its searches from the full job, so it can split the work before it runs any code.
- Save the script below as
sac.pyin thesac-agentfolder. - Replace [YOUR TASK] with the whole job: the items, the facts you need and the format you want.
- Keep both tools and
max_stepsas they are.
Your scriptfrom perplexity import Perplexity client = Perplexity() # reads PERPLEXITY_API_KEY TASK = "[YOUR TASK]" response = client.responses.create( model="openai/gpt-5.6-sol", input=TASK, tools=[{"type": "sandbox"}, {"type": "web_search"}], max_steps=10, instructions=( "Write and run Python in the sandbox to do the searches. " "Plan every query first, run them in parallel, and drop duplicate URLs. " "Print only a short table of findings with a source URL for each. " "Then answer from that table." ), ) for item in response.output: if item.type == "sandbox_results": print("--- Code the model ran ---") print(item.code) print("--- Answer ---") print(response.output_text) print("Cost in USD:", response.usage.cost.total_cost)You should see
sac.pyin your folder, with your own task in theTASKline.A good task names every item and fact. For example: "Compare the free plans of 5 note-taking apps: storage, device limits and price, with a source for each."
- Save the script below as
Step 4: Run it and read the code
5 minThe response returns the exact code the model ran, so you can see how it chose to search.
- Run the command below and wait. A run with many searches takes a minute or more.
- Read the printed code. Look for many queries sent at once, filters and duplicate checks.
- Check the answer and the cost line at the end.
Run thispython sac.py
You should see one or more blocks of Python, then a short answer with sources, then the cost in US dollars.
You get a 401 error?
The key isn't set in this terminal window. Run the
exportline from step 2 again in the same window, then run the script.With a direct model,
max_stepsstarts at 1. The agent spends its first step loading the sandbox, so 1 step leaves no room to run code.Step 5: Refine the search plan
4 minYou change the plan in plain words, and the model writes new code on the next run.
- Note what the first run missed, such as old pages, a skipped item or repeated sources.
- Add a line like the one below to the end of
instructions, inside the quotes. - Run
python sac.pyagain and compare the new code with the first run.
Add to your instructionsOnly use pages published after [DATE]. Search each item on its own, and prefer each maker's official site.
You should see new code with a date filter or one query per item, and a fresher answer.
A big task times out?
Add
background=Trueto the request. Then callclient.responses.retrieve(response.id)every few seconds until the status iscompleted.To change speed and cost, swap the model for another one on Perplexity's Models page.
You get
A research agent that writes its own search code, runs it in a sandbox and shows you every line it ran
Try next
Did this guide work for you?
Thanks. It helps us fix the steps.


