12-Factor Agents: Principles for Building Reliable LLM Applications

1,762 words → 377 · 7 min saved

Apply 12 concrete architectural principles to build AI agents where you control the prompts, context window, control flow, and human-in-the-loop checkpoints instead of delegating them to framework abstractions.

Key Observations from Production Agent Builders

Observation Detail
Most production "AI agents" are not very agentic Mostly deterministic code with LLM steps sprinkled in at just the right points
Frameworks rarely seen in production Most strong founders building customer-facing agents are rolling the stack themselves
Good agents are mostly just software They don't follow the "here's your prompt, here's a bag of tools, loop until you hit the goal" pattern

Evolution: Software to DAGs to Agents

  1. Traditional software is a directed graph, represented as flowcharts
  2. ~20 years ago, DAG orchestrators (Airflow, Prefect, Dagster, Inngest, Windmill) added observability, modularity, retries, and administration to the same graph pattern
  3. Agents promised you could throw the DAG away: give the LLM the "edges" of the graph and let it figure out the nodes in real time, writing less code and potentially finding novel solutions

The Core Agent Loop

context = [initial_event]
while True:
  next_step = await llm.determine_next_step(context)
  context.append(next_step)
  if next_step.intent == "done":
    return next_step.final_answer
  result = await execute_step(next_step)
  context.append(result)

Three-step loop: (1) LLM determines next step via structured JSON tool calling, (2) deterministic code executes the tool call, (3) result is appended to context window. Repeats until LLM determines "done." Initial context can be a user message, cron event, or webhook.

The Framework Trap

  1. Decide to build an agent, do product design and UX mapping
  2. Grab a framework to move fast and start building
  3. Get to 70-80% quality bar
  4. Realize 80% isn't good enough for customer-facing features
  5. Realize getting past 80% requires reverse-engineering the framework's prompts, flow, etc.
  6. Start over from scratch

The 12 Factors

Factor Principle
1 Natural Language to Tool Calls
2 Own your prompts
3 Own your context window
4 Tools are just structured outputs
5 Unify execution state and business state
6 Launch/Pause/Resume with simple APIs
7 Contact humans with tool calls
8 Own your control flow
9 Compact errors into context window
10 Small, focused agents
11 Trigger from anywhere, meet users where they are
12 Make your agent a stateless reducer

Core Thesis

Even as LLMs get exponentially more powerful, core engineering techniques will make LLM software more reliable, scalable, and maintainable

Going all-in on a framework and building a greenfield rewrite may be counter-productive

The fastest path: take small, modular concepts from agent building and incorporate them into your existing product

These modular concepts can be applied by most skilled software engineers, even without an AI background

Read original · ← Archive