The 12-Factor Agent: Best Practices for Building Reliable LLM Applications
📖 New Ebook Available
Build Your First MCP Server: A Developer's Guide to Wrapping Existing APIs for AI Agents to Use
Learn to create powerful AI integrations step by step
I've been building AI agents for a while now, and I kept running into the same patterns and problems. How do you make agents reliable? How do you handle errors gracefully? How do you maintain control over what they're doing? I thought I was figuring this stuff out on my own, developing my own best practices through trial and error.
Then I discovered Dex's talk on the 12-Factor Agent principles from HumanLayer, and it was like someone had been watching over my shoulder, documenting exactly what I'd been learning the hard way.
These principles aren't just theoretical - they're battle-tested approaches that solve real problems every agent builder faces. Whether you're just starting out or you've been building agents for months, these factors will make your applications more reliable, maintainable, and user-friendly.
The 12 Factors
Here are the core principles that every reliable AI agent should follow:
Factor 1: Natural Language to Tool Calls
Convert user intent into structured function calls. This is the foundation - your agent needs to reliably translate what users want into actionable tool invocations.
Factor 2: Own Your Prompts
Don't let frameworks hide your prompts. You need visibility and control over exactly what's being sent to your model.
Factor 3: Own Your Context Window
Manage context deliberately. Don't let it grow unbounded or let frameworks make context decisions for you.
Factor 4: Tools Are Just Structured Outputs
Think of tool calls as structured data generation, not magic. This mental model helps you debug and reason about agent behavior.
Factor 5: Unify Execution State and Business State
Your agent's execution context should align with your application's business logic. Don't create artificial boundaries between them.
Factor 6: Launch/Pause/Resume with Simple APIs
Build agents that can be controlled programmatically. Users need to be able to stop, start, and resume agent workflows.
Factor 7: Contact Humans with Tool Calls
When agents need human input, make it a first-class tool call. Don't hack in human-in-the-loop as an afterthought.
Factor 8: Own Your Control Flow
Don't let frameworks control when and how your agent makes decisions. You need explicit control over the execution flow.
Factor 9: Compact Errors into Context Window
When things go wrong, summarize errors efficiently so your agent can learn and recover without burning through tokens.
Factor 10: Small, Focused Agents
Build single-purpose agents rather than trying to create one agent that does everything. Specialization leads to reliability.
Factor 11: Trigger from Anywhere, Meet Users Where They Are
Your agents should be accessible through multiple interfaces - web, API, Slack, wherever your users actually work.
Factor 12: Make Your Agent a Stateless Reducer
Design your agent to be a pure function: given the same inputs and state, it should produce the same outputs. This makes debugging and scaling much easier.
Why These Resonated With Me
Reading through these factors was honestly a bit surreal. I've been unknowingly implementing most of these patterns in my own agent builds:
- Own your prompts: I learned this the hard way after debugging why my Pydantic AI agents weren't behaving as expected
- Small, focused agents: Every successful agent I've built does one thing well rather than trying to be everything
- Control flow ownership: I've moved away from frameworks that hide too much of the decision-making process
- Stateless design: My most reliable agents treat each interaction as a fresh start with explicit state management
It's validating to see these patterns formalized into principles. Even more importantly, the factors I haven't fully embraced yet (like human-in-the-loop tool calls) are clearly areas where I can improve my agent architecture.
Take Action
I highly recommend watching Dex's full talk and diving deeper into each principle. The GitHub repository has detailed explanations and examples for each factor:
⭐ Star the 12-Factor Agents repository
Whether you're using Pydantic AI, LangGraph, or building agents from scratch, these principles will make your applications more robust. I know I'll be referring back to them as I continue building and refining my own agent systems.
The best part? You don't need to implement all 12 factors at once. Pick the ones that solve your current pain points and gradually adopt the others as your agent systems mature.
Want to Chat About AI Engineering?
I hold monthly office hours to discuss your AI Product, MCP Servers, Web Dev, systematically improving your app with Evals, or whatever strikes your fancy. These times are odd because it's weekends and before/after my day job, but I offer this as a free community service. I may create anonymized content from our conversations as they often make interesting blog posts for others to learn from.
Book Office Hours