Skip to main content
Text from users, web pages and documents can carry instructions meant to take over your assistant. This example checks a piece of text and says whether it tries to. Use it on every incoming message, and on any text your agent pulls in from the web or a document, before any tool or retrieval runs.

How it works

The context is the text to check. One binary decision, attack, asks whether the text tells the assistant to replace its stated task, reveal hidden content or break a rule. The outcome descriptions say that such an instruction counts even when it is hidden among other text. The policy has abstain_below at 0.7: if neither yes nor no reaches a probability of 0.7, the action is abstain.

Try it

Open this recipe in the playground to run it without an account. The text there asks for a French translation of a customer review. The review tells the assistant to ignore its instructions and reply with the admin password. To run it from your code, set SQWISH_API_KEY as in the quickstart, then send this request:

Act on the answer

On yes, don’t pass the text to your model or its tools. Block it, or send it for review. A no only means no attack was found. It doesn’t mean the text is safe to hand to a tool. Keep your agent’s other checks, such as checking each tool call before it runs.

Adapt it to your data

  • Catch more attacks. Compare probabilities.yes with a threshold of your own, and lower it until it catches enough of your known attacks. Then check how many ordinary messages it flags.
  • Keep the wording. The question and descriptions were tuned for accuracy. Change them only if you measure the result.
  • Many texts. Send each piece of text in a request of its own, so an attack in one can’t change the answer for another.

What to watch

  • Attacks marked no. Keep a small set of known attacks, and run it again after every change to your decision or model.
  • False alarms. When attacks are rare, more of the messages it flags will be false alarms.
  • In testing, ordinary messages were called correctly more often than attacks.

How well it works

Measured with kettei-core on 960 examples from a public dataset, it chose the right answer 92.8% of the time. Always picking the most common answer scores 50.3%. Your own data will differ, so measure it on examples of yours before you rely on it (improve a decision).
  • Dataset: PromptShield (Apache-2.0).
  • Sample: 960 rows, 960 measured decisions.
  • Decisions measured: attack.
  • Wording: tuned.
Always picking the most common answer has an accuracy of 0.5031, and guessing by how common each answer is has a log loss of 0.6931.These are past results on one dataset, not a promise of accuracy on yours.
Half the test messages were attacks, far more than in most real traffic.

Next steps