Layered Guardrail Pipeline for Production LLM Agents
Checking what an agent receives, which actions it can take, and what it returns.
- LLM Agents
- Guardrails
- Prompt Injection
- FastAPI
- Tool Permissions
- Audit Logging
Safety checks across the agent workflow
An agent can fail at several points: an input may try to override its instructions, a tool call may exceed its permissions, or an answer may expose information or invent a source. Rishiraj Tanwar's Layered Guardrails project places checks at these different stages and measures what happens when individual layers are removed.
The implementation combines a FastAPI gateway, configurable policies, independent input guards, tool permissions, output checks, and an audit trail. A deliberately vulnerable mock model and simulated tools make the demonstration repeatable without sending emails or deleting files.
How the layers work together
- Gateway checks: require an API key and enforce a token-bucket rate limit.
- Input guards: check injection patterns and restricted topics, and redact recognized personal-information patterns. Independent checks can run in parallel.
- Tool controls: apply an allowlist, argument validation, and role-based permissions. High-risk mock actions pause with a pending-approval response.
- Output guards: check for a planted secret, unsupported URLs or numbers, unsuitable output length, and recognized personal information. Repeated failures produce a fallback response.
- Audit logging: records request IDs, input fingerprints, layer decisions, and elapsed time.
A controlled test of each layer
The evaluation contains 58 author-written prompts: 39 attack cases and 19 benign requests. It checks four outcomes: a planted secret reaching the response, an unauthorized mock tool executing, a fabricated citation appearing, and personal information remaining in the answer.
With input, tool, and output controls disabled, all 39 attack cases succeeded against the mock agent. With all three layers enabled, two succeeded, an attack-success rate of approximately 5.1%. The full configuration also blocked two of the 19 benign prompts, a false-positive rate of approximately 10.5%.
Removing the input checks left the same two successful attacks while eliminating the two benign rejections. Removing the tool controls increased successful attacks to seven; removing output checks increased them to 17. These differences help identify what each layer contributes in this particular test.
API demonstration
The repository's demo screenshot shows the local API responding to several cases:
- A direct instruction-override request is blocked by the input layer.
- A request for the system prompt that passes the input checks is caught by output checks and ends in a fallback.
- An administrator's mock file-deletion request pauses for approval.
- An invalid API key receives an unauthorized response.
The source includes local setup instructions and 20 test cases covering guards, tool permissions, approval behavior, API authentication, rate limiting, and logging.
What the results can tell us
This is a small, controlled demonstration with a mock model, simulated tools, and prompts written with the guards in mind. Its rates describe those cases. Real models, adaptive attacks, and broader workloads need separate evaluation.
The detectors use regular expressions and simple heuristics. The remaining attack cases involve obfuscated personal information; the benign rejections involve ordinary discussion of filters or injection phrases. Groundedness checking is limited to URLs and numbers, and secret-leak checking uses an exact planted string.
A separate scheduling experiment adds an artificial 50-millisecond delay to each input guard. It illustrates the potential benefit of parallel checks; deployment latency needs measurement with the actual models and infrastructure.
Explore the project
The implementation uses Python, FastAPI, Pydantic, PyYAML, and asynchronous guard execution. Future extensions include trained injection detection, stronger personal-information detection, more realistic models, and adaptive attack evaluation.
Browse the source code and documentation, open the demo screenshot, or inspect the ablation results.