<?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>DEV Community: Naman-sharma00100</title>
    <description>The latest articles on DEV Community by Naman-sharma00100 (@namansharma00100).</description>
    <link>https://dev.to/namansharma00100</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%2F737200%2F7d3b4b54-d425-4fa4-b223-971e1cdddb98.png</url>
      <title>DEV Community: Naman-sharma00100</title>
      <link>https://dev.to/namansharma00100</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/namansharma00100"/>
    <language>en</language>
    <item>
      <title>Implementing sudo for LLMs: A Middleware Approach to AI Security</title>
      <dc:creator>Naman-sharma00100</dc:creator>
      <pubDate>Sat, 31 Jan 2026 22:35:27 +0000</pubDate>
      <link>https://dev.to/namansharma00100/implementing-sudo-for-llms-a-middleware-approach-to-ai-security-1pam</link>
      <guid>https://dev.to/namansharma00100/implementing-sudo-for-llms-a-middleware-approach-to-ai-security-1pam</guid>
      <description>&lt;h2&gt;
  
  
  The "Write Access" Anxiety
&lt;/h2&gt;

&lt;p&gt;We are all rushing to build "Agents"—AI that can use tools, not just chat. But the moment I gave my LangChain agent a &lt;code&gt;stripe_api_key&lt;/code&gt;, I felt a knot in my stomach.&lt;/p&gt;

&lt;p&gt;We are essentially giving a probabilistic model (an LLM) deterministic access to our bank accounts and cloud infrastructure. If the LLM hallucinates a loop, or gets prompt-injected, my database is gone.&lt;/p&gt;

&lt;p&gt;I realized that &lt;strong&gt;"asking the LLM nicely" (System Prompts) is not a security strategy.&lt;/strong&gt; You wouldn't secure a Linux server by asking users to "please be nice." You use permissions. You use &lt;code&gt;sudo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, I spent the last few weeks building a &lt;strong&gt;Governance Layer&lt;/strong&gt; for AI Agents. Here is a deep dive into the architecture and the challenges of building "Human-in-the-Loop" middleware.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Architecture: The "Man-in-the-Middle"
&lt;/h2&gt;

&lt;p&gt;The core problem is that once an Agent starts a tool call, the developer usually loses control. The execution is synchronous and opaque.&lt;/p&gt;

&lt;p&gt;I needed a proxy. A middleware that sits between the &lt;strong&gt;Agent&lt;/strong&gt; and the &lt;strong&gt;Critical Resource&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I call it &lt;strong&gt;SudoMode&lt;/strong&gt;. It works on a simple "Intercept and Verify" loop:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Intercept:&lt;/strong&gt; The SDK wraps the dangerous function (e.g., &lt;code&gt;stripe.charge&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Evaluate:&lt;/strong&gt; A local Policy Engine checks &lt;code&gt;policies.yaml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Pause:&lt;/strong&gt; If the action is "High Risk," the Python thread &lt;em&gt;sleeps&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Poll:&lt;/strong&gt; The SDK polls the Governance Server every 2 seconds.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Resume:&lt;/strong&gt; Once a human approves the request via the Dashboard, the server returns the authorization token, and the script resumes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Code: Policy as Configuration
&lt;/h2&gt;

&lt;p&gt;I didn't want to bake rules into the Python code. I wanted them declarative, similar to Kubernetes manifests or OPA (Open Policy Agent).&lt;/p&gt;

&lt;p&gt;Here is what a policy looks like. It’s simple YAML that defines the "Blast Radius" of an agent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rules:
  # Rule 1: Hard Block (The Firewall)
  - id: "protect-production-db"
    resource: "postgres"
    action: "drop_table"
    response: "DENY"

  # Rule 2: Conditional Approval (The Sudo Command)
  - id: "spending-limit"
    resource: "stripe"
    action: "charge"
    condition: "args.amount &amp;gt; 50"
    response: "REQUIRE_APPROVAL"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The "Wait" Mechanism (The Engineering Challenge)
&lt;/h2&gt;

&lt;p&gt;The hardest engineering challenge was handling the Async/Sync mismatch.&lt;/p&gt;

&lt;p&gt;Most Agent frameworks (CrewAI, LangChain) expect a tool to return a value immediately. They don't handle "Wait for human" well natively. If you throw an error, the agent crashes or tries to "fix" the error by hallucinating.&lt;/p&gt;

&lt;p&gt;To solve this, I implemented a &lt;strong&gt;Long Polling&lt;/strong&gt; loop in the client SDK. Instead of raising an exception, the client enters a while True loop that mimics a slow network request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def execute(self, resource, action, args):
    # 1. Initial Check
    decision = self.check(resource, action, args)

    # 2. The Waiting Game
    if decision['status'] == 'REQUIRE_APPROVAL':
        # Log to stdout so the developer sees the pause
        logger.info(f"Paused. Waiting for Admin (ID: {decision['request_id']})...")

        while True:
            time.sleep(2)

            # POLL THE SERVER
            status = self._get_request_status(decision['request_id'])

            if status == "APPROVED":
                return True 
            elif status == "REJECTED":
                raise PermissionError("Request Denied by Admin.")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To the Agent, it just looks like the API is taking a while to respond. To the Human, it looks like a "Pending Request" on a dashboard.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Open Source?
&lt;/h2&gt;

&lt;p&gt;I built this because I needed it, but I realized every developer building Agents is reinventing this wheel. We are all hacking together weird input() loops to check our agents.&lt;/p&gt;

&lt;p&gt;I open-sourced &lt;strong&gt;SudoMode&lt;/strong&gt; to be a standard, drop-in safety net. It’s not perfect—it’s an MVP—but it effectively separates Intelligence (the LLM) from Control (the Policy).&lt;/p&gt;

&lt;p&gt;I’m currently looking for feedback on the architecture, specifically on how to handle distributed state if the agent crashes while waiting.&lt;/p&gt;

&lt;p&gt;If you are building Agents and losing sleep over security, check out the repo. I’d love to see if this architecture fits your use case.&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/numcys/sudomode" rel="noopener noreferrer"&gt;https://github.com/numcys/sudomode&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>security</category>
      <category>architecture</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
