12-factor-agents README

The project's README from github.com/humanlayer/12-factor-agents. As one markdown file: README.md.

12-Factor Agents - Principles for building reliable LLM applications

Code License: Apache 2.0 Content License: CC BY-SA 4.0 Discord Server YouTube
Deep Dive YouTube
Deep Dive

In the spirit of 12 Factor Apps. The source for this project is public at https://github.com/humanlayer/12-factor-agents, and I welcome your feedback and contributions. Let's figure this out together!

[!TIP] Missed the AI Engineer World's Fair? Catch the talk here

Looking for Context Engineering? Jump straight to factor 3

Want to contribute to npx/uvx create-12-factor-agent - check out the discussion thread

Screenshot 2025-04-03 at 2 49 07 PM

Hi, I'm Dex. I've been hacking on AI agents for a while.

I've tried every agent framework out there, from the plug-and-play crew/langchains to the "minimalist" smolagents of the world to the "production grade" langraph, griptape, etc.

I've talked to a lot of really strong founders, in and out of YC, who are all building really impressive things with AI. Most of them are rolling the stack themselves. I don't see a lot of frameworks in production customer-facing agents.

I've been surprised to find that most of the products out there billing themselves as "AI Agents" are not all that agentic. A lot of them are mostly deterministic code, with LLM steps sprinkled in at just the right points to make the experience truly magical.

Agents, at least the good ones, don't follow the "here's your prompt, here's a bag of tools, loop until you hit the goal" pattern. Rather, they are comprised of mostly just software.

So, I set out to answer:

What are the principles we can use to build LLM-powered software that is actually good enough to put in the hands of production customers?

Welcome to 12-factor agents. As every Chicago mayor since Daley has consistently plastered all over the city's major airports, we're glad you're here.

Special thanks to @iantbutler01, @tnm, @hellovai, @stantonk, @balanceiskey, @AdjectiveAllison, @pfbyjy, @a-churchill, and the SF MLOps community for early feedback on this guide.

The Short Version: The 12 Factors

Even if LLMs continue to get exponentially more powerful, there will be core engineering techniques that make LLM-powered software more reliable, more scalable, and easier to maintain.

Visual Nav

factor 1 factor 2 factor 3
factor 4 factor 5 factor 6
factor 7 factor 8 factor 9
factor 10 factor 11 factor 12

How we got here

For a deeper dive on my agent journey and what led us here, check out A Brief History of Software - a quick summary here:

The promise of agents

We're gonna talk a lot about Directed Graphs (DGs) and their Acyclic friends, DAGs. I'll start by pointing out that...well...software is a directed graph. There's a reason we used to represent programs as flow charts.

010-software-dag

From code to DAGs

Around 20 years ago, we started to see DAG orchestrators become popular. We're talking classics like Airflow, Prefect, some predecessors, and some newer ones like (dagster, inggest, windmill). These followed the same graph pattern, with the added benefit of observability, modularity, retries, administration, etc.

015-dag-orchestrators

The promise of agents

I'm not the first person to say this, but my biggest takeaway when I started learning about agents, was that you get to throw the DAG away. Instead of software engineers coding each step and edge case, you can give the agent a goal and a set of transitions:

025-agent-dag

And let the LLM make decisions in real time to figure out the path

026-agent-dag-lines

The promise here is that you write less software, you just give the LLM the "edges" of the graph and let it figure out the nodes. You can recover from errors, you can write less code, and you may find that LLMs find novel solutions to problems.

Agents as loops

As we'll see later, it turns out this doesn't quite work.

Let's dive one step deeper - with agents you've got this loop consisting of 3 steps:

  1. LLM determines the next step in the workflow, outputting structured json ("tool calling")
  2. Deterministic code executes the tool call
  3. The result is appended to the context window
  4. Repeat until the next step is determined to be "done"
initial_event = {"message": "..."}
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)

Our initial context is just the starting event (maybe a user message, maybe a cron fired, maybe a webhook, etc), and we ask the llm to choose the next step (tool) or to determine that we're done.

Here's a multi-step example:

027-agent-loop-animation

GIF Version

027-agent-loop-animation

Why 12-factor agents?

At the end of the day, this approach just doesn't work as well as we want it to.

In building HumanLayer, I've talked to at least 100 SaaS builders (mostly technical founders) looking to make their existing product more agentic. The journey usually goes something like:

  1. Decide you want to build an agent
  2. Product design, UX mapping, what problems to solve
  3. Want to move fast, so grab $FRAMEWORK and get to building
  4. Get to 70-80% quality bar
  5. Realize that 80% isn't good enough for most customer-facing features
  6. Realize that getting past 80% requires reverse-engineering the framework, prompts, flow, etc.
  7. Start over from scratch
Random Disclaimers

DISCLAIMER: I'm not sure the exact right place to say this, but here seems as good as any: this in BY NO MEANS meant to be a dig on either the many frameworks out there, or the pretty dang smart people who work on them. They enable incredible things and have accelerated the AI ecosystem.

I hope that one outcome of this post is that agent framework builders can learn from the journeys of myself and others, and make frameworks even better.

Especially for builders who want to move fast but need deep control.

DISCLAIMER 2: I'm not going to talk about MCP. I'm sure you can see where it fits in.

DISCLAIMER 3: I'm using mostly typescript, for reasons but all this stuff works in python or any other language you prefer.

Anyways back to the thing...

Design Patterns for great LLM applications

After digging through hundreds of AI libriaries and working with dozens of founders, my instinct is this:

  1. There are some core things that make agents great
  2. Going all in on a framework and building what is essentially a greenfield rewrite may be counter-productive
  3. There are some core principles that make agents great, and you will get most/all of them if you pull in a framework
  4. BUT, the fastest way I've seen for builders to get high-quality AI software in the hands of customers is to take small, modular concepts from agent building, and incorporate them into their existing product
  5. These modular concepts from agents can be defined and applied by most skilled software engineers, even if they don't have an AI background

The fastest way I've seen for builders to get good AI software in the hands of customers is to take small, modular concepts from agent building, and incorporate them into their existing product

The 12 Factors (again)

Honorable Mentions / other advice

Contributors

Thanks to everyone who has contributed to 12-factor agents!

dexhorthy Sypherd tofaramususa a-churchill Elijas hugolmn jeremypeters

kndl maciejkos pfbyjy 0xRaduan zyuanlim lombardo-chcg sahanatvessel

License

All content and images are licensed under a CC BY-SA 4.0 License

Code is licensed under the Apache 2.0 License

12-factor-agents (humanlayer/12-factor-agents)

Searching Database…

这是什么?这是一份写给工程师的指南,包含 12 条原则(另加一条额外的第 13 条),用于构建足够可靠、能服务生产环境客户的大语言模型(LLM)驱动智能体软件。这些原则提炼自作者在 HumanLayer 的经验,以及与许多 SaaS 创始人的交流。
这是一个需要安装的代码框架,还是只需阅读的指南?这是一个文档与文章仓库,而不是可安装的框架或库。原则配有 TypeScript 代码片段作为示例,但目的是让你把这些原则应用到自己的技术栈中,而不是把它当作依赖引入。
这份指南是如何组织的?我应该从哪里开始?全文按编号依次讲解 12 个因素(例如:自己掌控提示词、自己掌控上下文窗口、通过工具调用联系人类),另有一个荣誉提及的第 13 条因素。每个因素都是独立的链接章节,包含解释和示例。请从 Factor 1: Natural Language to Tool Calls 开始,按顺序阅读。
要运用这些思路,我需要任何智能体框架(如 LangChain、CrewAI 等)吗?不需要。核心观点正是:你无需采用某个框架。作者认为,大多数面向生产环境的智能体产品,主要是确定性代码,只是在其中零星穿插了 LLM 调用。这些原则可以直接应用到现有代码库中。
这份指南是为谁写的?面向希望让现有产品变得更具智能体特性的技术创始人和软件工程师,帮助他们突破通用智能体框架普遍遇到的 70%–80% 质量上限。
什么是第 13 条附加原则?提前获取你可能需要的全部上下文。这是在原有 12 条之后新增的一条荣誉提及因素。
作者为什么不直接使用现成的智能体框架,而要自己写这份指南?作者几乎试用了所有框架(crew/langchain、smolagents、langgraph、griptape),并与许多创始人交流后发现,大多数面向客户的生产级智能体几乎根本不用框架。于是他提炼出真正让这些智能体变得可靠的一小组工程思路。
这份指南是否涵盖 MCP(Model Context Protocol,模型上下文协议)?没有。作者明确表示,本指南不会讨论 MCP。
同一套方法论还催生了哪些相关项目?作者的团队基于 got-agents/agents 方法论构建了开源智能体,还开发了一个用于在 Kubernetes 中运行分布式智能体的框架。
Creator
humanlayer
Size
44.4 MB
Stars
26.7K
Updated
CommitSep 21
Age
1y
Commits · 90 Days