Your complete roadmap to building secure AI agents. From understanding core security principles to implementing guardrails, structured outputs, and defense mechanisms against prompt injection.
By Alejandro Aboy
Published: 2026-05-28
URL: https://thepipeandtheline.substack.com/p/building-secure-ai-agents-from-prompt
Hi there! Alejandro here 😊
Subscribe if you like to read about technical data & AI learnings, deep dives!
Enjoy the reading and let me know in the comments what you think about it 👨🏻💻
📝 TL;DR
- Complete AI security roadmap: Links all my AI security articles with context and a logical learning path from foundational concepts to production-ready implementations.
- Hands-on learning: The ShamanAI GitHub repo provides working examples of guardrails, prompt injection defense, and structured outputs.
- Learning sequence: Structured outputs for reliability → Prompt injection prevention → Secure design principles → Guardrails with Agno
- Defense in depth: 4 security layers covering input validation, guardrails, output control, architecture & observability.
- Real production experience: Lessons from building AI agents that survive in production, not just demos that work on your laptop.
🗺️ Learning Path
Recommended sequence:
- Reliability First: Structured Outputs - Ensure your agent outputs are reliable and parseable before worrying about security
- Understand the Threat: Prompt Injection Prevention - Learn what you’re defending against with real examples
- Design Principles: Secure Design - Apply software engineering best practices to AI architecture
- Hands-On Implementation: AI Guardrails with Agno - Clone the ShamanAI repo and implement real guardrails
- Observability Implementation: don’t hesitate to implement something like Opik which is super fast and provides lots of context to get started.
\Image generated by ChatGPT.\
🔐 Core AI Security Series
Foundations: Output Reliability
Recommended: Secure LLMs for Data Engineers: Structured Outputs
Understand why structured outputs aren’t just about formatting but a critical security layer.
Learn to enforce JSON schemas with Pydantic, use response\_format for validation, and implement the Instructor library for automatic retries and error handling.
Structured outputs prevent downstream pipeline failures and ensure your agents don’t fabricate data to fit expected schemas.
Defense: Fighting Prompt Injection
Recommended: Secure LLMs for Data Engineers: How To Prevent Prompt Injection
Master the techniques to prevent users from hijacking your LLM with “ignore previous instructions” attacks.
Get introduced to text classifiers with Hugging Face models, build LLM-as-judge guardrails, sanitize inputs, and use few-shot prompting to teach your models what attacks look like.
From supermarket chatbots giving Python tutorials to company bots revealing internal instructions. See what happens when prompt injection isn’t taken seriously.
The Pipe & The Line is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.
Architecture: Secure Design Principles
Recommended: Secure LLMs for Data Engineers: Other Design principles for your AI Applications
Apply software engineering security principles to AI applications.
Implement least privilege database access, scope tools properly, inherit authentication context for multi-tenant apps, and build comprehensive logging and monitoring.
An agent can’t drop your production table if it doesn’t have DROP permissions. Security is just good engineering applied to AI.
Implementation: Production Guardrails
Recommended: What are AI Guardrails? Hands On Implementation With Agno
Go hands-on with the Agno framework to implement four essential guardrails: prompt injection detection, PII masking, content moderation with OpenAI, and structured output enforcement.
Learn how to build custom guardrails for domain-specific protection.
Work with the ShamanAI agent that answers philosophical questions in 5 words—see how guardrails prevent it from being serious, verbose, or leaking sensitive data.
Thanks for reading The Pipe & The Line! This post is public so feel free to share it.
Observability Is Non Negotiable
Recommended: Behind the Scenes of AI Observability in Production
Don’t settle for just logging tool calls and messages.
Observability is the key to evolving your AI Product and consistently make it more secure and performant.
The biggest challenges in AI Projects are silent errors. Agents trying to do things they shouldn’t, hallucinations on actions that were executed but nothing happened.
Take the time to implement Observability and know what’s happening behind the scenes.
🎯 The Main Character: ShamanAI Repository
All security implementations, guardrails, and hands-on examples live here:
What’s inside:
- Complete Agno framework implementation with multiple guardrails
- Prompt injection detection with custom patterns
- PII detection and masking with regex patterns
- OpenAI content moderation integration
- Structured outputs enforcement
- Custom guardrails (SecretProtectionGuardrail)
- CLI agent interface for testing
- Test suite for validation
The repository demonstrates real guardrails in action—try to make the agent break its rules and see the security layers prevent it.
When You Want Extra Security... Add Human In The Loop 👀
I haven’t implemented this one production. But there’s nothing better than forcing the agent to ask for permissions, input or extra validation to fully control its behaviour.
There are usually 2 types:
- User Permission
- User Input
Basically you control execution flow and there’s no way the LLM messes anything up unless the user allows it.
I’ve been evaluating \\Safety Operations\\ over the last months, to see if my agents comply with asking the user before destructive operations (e.g. DELETE REST API endpoints) and it was most of the time going through without asking.
This Human In The Loop approach is a code driven guardrail that will block execution if rejected, and the agent loop will also “break”.
I highly recommend exploring this, specially if the impact of destructive operations in your industry is high.
🛡️ Security Layers Summary
Your production-ready AI agent should have these defense layers:
Layer 1: Input Validation
- Text classifiers for prompt injection detection
- Input sanitization (remove malicious XML/Markdown)
- Custom pattern matching for known attacks
- Human In The Loop
Layer 2: Guardrails
- Topical guardrails to keep agent on-scope
- PII detection and masking
- Content moderation for harmful content
- LLM-as-judge for request appropriateness
Layer 3: Output Control
- Structured outputs with Pydantic schemas
- Response format enforcement
- Instructor library for automatic retries
- Output validation before downstream use
Layer 4: Architecture
- Least privilege database access & Scoped tool permissions
- Authentication inheritance for multi-tenant
- Comprehensive logging, monitoring & Observability Tracking
- Cloud content filtering
Recommended Documentation:
📝 Final Words
- Guardrails are not deterministic: They can fail to block something. This is part of real-world LLM security. Layer your defenses.
- Security is just good engineering: If you skip security principles in your daily work, your AI implementation will inherit those vulnerabilities.
- One layer is never enough: Combine prompting techniques, guardrails, structured outputs, and architectural controls. Defense in depth.
- Log everything: You cannot debug what you didn’t log. Track all agent actions for explainability and auditability.
If you enjoyed the content, hit the like ❤️ button, share, comment, repost, and all those nice things people do when like stuff these days. Glad to know you made it to this part!

Hi, I am Alejandro Aboy. I am currently working as a Data Engineer. I started in digital marketing at 19. I gained experience in website tracking, advertising, and analytics. I also founded my agency. In 2021, I found my passion for data engineering. So, I shifted my career focus, despite lacking a CS degree. I’m now pursuing this path, leveraging my diverse experience and willingness to learn.