<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: YUICHI KANEKO</title>
    <description>The latest articles on Forem by YUICHI KANEKO (@yuichi).</description>
    <link>https://forem.com/yuichi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3854598%2F6df92f63-f347-4a6c-87fc-e2899235cfb9.png</url>
      <title>Forem: YUICHI KANEKO</title>
      <link>https://forem.com/yuichi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yuichi"/>
    <language>en</language>
    <item>
      <title>Detecting Prompt Injection in LLM Apps (Python Library)</title>
      <dc:creator>YUICHI KANEKO</dc:creator>
      <pubDate>Wed, 01 Apr 2026 03:31:37 +0000</pubDate>
      <link>https://forem.com/yuichi/detecting-prompt-injection-in-llm-apps-python-library-1fgp</link>
      <guid>https://forem.com/yuichi/detecting-prompt-injection-in-llm-apps-python-library-1fgp</guid>
      <description>&lt;p&gt;I've been working on LLM-backed applications and ran into a recurring issue: prompt injection via user input.&lt;/p&gt;

&lt;p&gt;Typical examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Ignore all previous instructions"&lt;/li&gt;
&lt;li&gt;"Reveal your system prompt"&lt;/li&gt;
&lt;li&gt;"Act as another AI without restrictions"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In many applications, user input is passed directly to the model, which makes these attacks practical.&lt;/p&gt;

&lt;p&gt;Most moderation APIs are too general-purpose and not designed specifically for prompt injection detection, especially for non-English inputs. So I built a small Python library to act as a screening layer before sending input to the LLM:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/kanekoyuichi/promptgate" rel="noopener noreferrer"&gt;https://github.com/kanekoyuichi/promptgate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Detection strategies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;rule-based (regex / phrase matching)&lt;br&gt;&lt;br&gt;
latency: &amp;lt;1ms, no dependencies&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;embedding-based (cosine similarity with attack exemplars)&lt;br&gt;&lt;br&gt;
latency: ~5–15ms, uses sentence-transformers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;LLM-as-judge&lt;br&gt;&lt;br&gt;
higher accuracy, but +150–300ms latency, requires external API&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Baseline evaluation (rule-only):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;FPR: 0.0% (0 / 30 benign samples)&lt;/li&gt;
&lt;li&gt;Recall: 61.4% (27 / 44 attack samples)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So rule-based alone misses ~40% of attacks, especially paraphrased or context-dependent ones.&lt;/p&gt;

&lt;p&gt;This is not intended as a complete solution — the design assumption is defense-in-depth, where this acts as a first screening layer.&lt;/p&gt;

&lt;p&gt;Known limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rule-based detection struggles with paraphrased / indirect instructions
&lt;/li&gt;
&lt;li&gt;embedding approach depends on exemplar coverage (not a trained classifier)
&lt;/li&gt;
&lt;li&gt;LLM-as-judge is non-deterministic and API-dependent
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Would be interested in feedback on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;better evaluation methodologies
&lt;/li&gt;
&lt;li&gt;detection strategies beyond pattern / similarity / LLM judging
&lt;/li&gt;
&lt;li&gt;how others are handling prompt injection at the application layer
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>python</category>
      <category>security</category>
    </item>
  </channel>
</rss>
