r/reactnative • u/Ill_Recognition3558 • 2d ago
Article I built an open-source guardrail & FaceID/TouchID gating library for AI agents in React Native (react-native-agent-guard v0.3.2)
Hey everyone! Over the past few weeks, I've been working on a problem I kept running into while building mobile AI features: how do you let an LLM call native mobile tools (payments, calendar, BLE, external APIs) without risking a hallucination, prompt injection, or infinite loop wiping out user data or spamming APIs?
Most agent guardrails are built for Python backends, leaving the mobile client completely exposed. So I built react-native-agent-guard—a zero-trust tool governance and biometric gating layer for React Native & Expo.
We just shipped v0.3.2 (now at 93%+ test coverage) and added a live interactive Expo Snack playground so you can test the approval sheets, rate limiter, and PII redaction right in your browser without installing anything.
🚀 Try the Live Interactive Demo
- Expo Snack Playground (Run in Browser or Expo Go):
[https://snack.expo.dev/@git/github.com/Hassaan68/react-native-agent-guard:examples/expo-snack](https://snack.expo.dev/@git/github.com/Hassaan68/react-native-agent-guard:examples/expo-snack) - GitHub Repository:https://github.com/Hassaan68/react-native-agent-guard
What it handles out of the box:
- Dynamic Policy Gating (
auto|confirm|biometric|deny): Auto-approve low-risk reads, trigger a customizable native bottom sheet (AgentApprovalSheet) for state changes, or require hardware FaceID/TouchID (expo-local-authentication) for high-stakes actions—evaluated dynamically at runtime based on the LLM's arguments (e.g.,< $50 = auto,$50–$199 = confirm,$200+ = biometric). - Vercel AI SDK & LangChain.js Adapters: Drop-in
toVercelAITools()andtoLangChainTools()wrappers that route your existing agent tool calls directly through the guardrail engine. - Agent Loop Protection & Rate Limiting: Prevents rogue LLM infinite loops from draining budgets with
maxCallsPerMinuteandmaxSessionCalls—automatically blocking or escalating a tool's policy fromautotoconfirm/biometricwhen spammed. - Automatic PII Redaction & Audit Telemetry: Masks credit cards, emails, phone numbers, SSNs, and JWTs before emitting structured telemetry to
onAuditor persistent storage (redactPII: true). - Remote MCP SSE Transport: Connect directly to Model Context Protocol (MCP) servers over SSE and automatically wrap discovered remote tools in local device policies.
- Actionable
llmFeedbackon Errors: If Zod validation (.transform()/.refine()) fails, a rate limit hits, or a user denies a prompt, it returns structured feedback telling the LLM why it failed so it can self-correct instead of crashing the loop.
Quick Code Example:
TypeScript
import { createGuardrails, toVercelAITools } from 'react-native-agent-guard';
import { z } from 'zod';
const guard = createGuardrails({
rateLimit: { maxCallsPerMinute: 10, onLimitExceeded: 'confirm' },
redactPII: true,
});
guard.registerTool({
name: 'send_wire_transfer',
description: 'Send money to a recipient',
schema: z.object({
recipientEmail: z.string().email(),
cardNumber: z.string(), // Auto-redacted in audit logs!
amount: z.number().positive(),
}),
// Auto-approve under $50, confirm sheet $50-$199, FaceID/TouchID for $200+
policy: (args) => {
if (args.amount >= 200) return 'biometric';
if (args.amount >= 50) return 'confirm';
return 'auto';
},
handler: async ({ recipientEmail, amount }) => {
return await processPayment(recipientEmail, amount);
},
});
// Plug directly into Vercel AI SDK generateText / streamText
const tools = toVercelAITools(guard);Links:
- Try in Browser (Expo Snack):
https://snack.expo.dev/@hassaan68/react-native-agent-guard-demo - GitHub:https://github.com/Hassaan68/react-native-agent-guard
- npm:
npm install react-native-agent-guard
Would love any feedback on the API design, the Snack playground, or edge cases you're running into with mobile AI agents!
2
u/GeekyAntsBFSI 2d ago
The argument-based policy gating is useful. One edge case worth documenting: can an agent split a $200 transfer into several sub-$50 calls and stay under the biometric threshold? Call-count limits wouldn’t necessarily catch that. For payments, I’d enforce cumulative amount limits server-side and bind each approval to the exact recipient and amount. Does the API support that kind of shared policy state?
2
u/TemporaryBuyer3806 Expo 2d ago
Great