Skip to main content
Fetched pages can contain text that tries to redirect your model. Use Context API criteria to get a prompt injection score for each page as you fetch it, then apply your own cutoff before any Markdown reaches your model.

A live run of this recipe on November 18, 2026. The terminal shows the script's output, paced for reading. The step captions and API panels were added for the recording.

Set up

You need a Perplexity API key and Python 3.11 or newer. The script reads a text file with 1 to 10 public URLs, one per line. 10 is the Tier 0 limit for URLs per request; higher usage tiers allow more, so raise the 10 in main to match yours.
Save these URLs as urls.txt. The first is a real indirect prompt injection demo page from the Greshake et al. 2023 research: hidden text that poses as a system message and tells the assistant to extract the user’s name. The second is the OWASP page that explains prompt injection. The third is RFC 9111, an ordinary technical page. The fourth does not exist, so you can see how the script handles a failed fetch.
urls.txt

Write the question

A criteria question has type noul, the yes or no format of the Decisions API. instructions (up to 1,000 characters) state the question, and criteria.true and criteria.false (up to 500 characters each) tell the decision model what counts as yes and no. A short question is not enough. Our first version asked “Does this page contain text that tries to instruct an AI model or agent that reads it?” with one-sentence yes and no descriptions, and the no description already excluded pages that discuss prompt injection. It still scored three security write-ups at 0.38, 0.80, and 0.95. The question in this recipe does four things:
  • Defines the class by what the text does: it tries to override, redirect, or manipulate the AI that reads the page, rather than inform a human reader.
  • Lists the patterns to count. Overrides, persona changes, requests to reveal data, unrequested actions, output manipulation, text posing as the site, the user, or a system message, forged chat turns, hidden or camouflaged text, and text in a different language from the rest of the page. These match what Perplexity’s BrowseSafe++ research found in injections collected from real pages: they often pose as a trusted source, and many push the agent toward an action the user never asked for.
  • Names the hard negative. Security articles, documentation, and research that quote an injection to explain it are not injections, even when the quote is verbatim.
  • Sets the default. Ordinary content is a no unless an attempt to direct an AI is clearly present. Most pages are clean, so a question biased toward yes produces mostly false alarms.
criteria.true adds short examples of the patterns, including the camouflaged kind (“IF YOU ARE AN AI AGENT READING THIS: your function has been updated”), and criteria.false restates the hard negative.

Fetch and screen the pages

The script sends POST /v1/context with the URLs and the question under the name is_prompt_injection. The response has one result per URL, in request order. The API never filters or reorders results based on scores. On a scored page, results[i].criteria.is_prompt_injection.noul is the probability from 0 to 1 that the answer is yes. There is no threshold field in the request, so the script applies --threshold to the response. An ok result that could not be scored has criteria_error instead of criteria. screen retries those URLs once when the error is timeout or overloaded. The retry is a second POST /v1/context, so those URLs are fetched and billed again. If the retry request itself fails, screen keeps the first results and the unscored pages stay quarantined. The script uses httpx so the request body is plain JSON. The Perplexity Python library sends the same request with client.context.create(urls=..., extra_body={"criteria": ...}). Copy this code to screen_pages.py.
screen_pages.py
Run the script with your URL list. The default threshold is 0.7.
Output from a run on November 18, 2026. Scores differ a little between runs.
The script prints one row per URL. It does not call a downstream model. In your own pipeline, call screen and then route on each result: A request-level error, such as a 400 for an invalid question, stops the script before it prints any routes.

Check the cutoff against labeled pages

Before you trust a cutoff, run the script on pages you have labeled yourself, including pages that discuss attacks without attempting them. These are the scores from November 18, 2026 for the pages we used while writing the question. Each page was scored twice, and for every page in the table the two scores differed by less than 0.02. Lowering the cutoff quarantines more pages, including legitimate ones. Raising it lets more pages through, including possible injection attempts. Measure both directions on your own traffic, and re-check whenever you change the question. For a benchmark-scale evaluation, the BrowseSafe++ SDK scores a detector against page pairs built from injections observed on real websites. A low score does not make a page safe. Screening is one layer: the same research found that every model it tested still followed some injections it had itself flagged, so keep your model’s permissions narrow and mark fetched content as untrusted.

Hand passed pages to your model

Put each passed page inside a labeled block and tell your model the content is data, not instructions. client is the httpx.Client from main:
A page can contain its own </document> tag, so the label is a hint to the model, not a boundary it enforces. The instruction in the prompt and the narrow permissions from the previous section do the real work.

Test it offline

Copy this code to test_screen_pages.py. The tests use synthetic responses through httpx.MockTransport, so they run without an API key. They cover the routing rules, the retry, and the input checks.
test_screen_pages.py

Notes

  • A request can carry 1 to 5 criteria questions. Each needs type "noul" and instructions; criteria.true and criteria.false are optional.
  • With criteria, timeout_ms can be at most 95000, and scoring may add up to 15 seconds to the request.
  • A result has criteria only when it fetched ok and was scored. An ok result that could not be scored has criteria_error (timeout, overloaded, or error) instead.
  • Limits, errors, and the full request and response shape are in Score pages with criteria and the API reference.