Logo Twelve Factor

Twelve Factor

Cộng đồng
citypaul
twelve-factor

Twelve-Factor App patterns for software-as-a-service and long-running process applications. Use when configuring deploy-time environment, backing services, process startup/shutdown, or operational parity. Apply individual principles to frontends, serverless functions, and CLIs only where their runtime and platform make them relevant.

Tổng quan

Nhà phát hànhcitypaul
Kho mã.dotfiles
Tên kỹ năngtwelve-factor
Sao
740
Bản fork
96
Tệp đi kèm
1
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 1 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được citypaul công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Twelve Factor trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/citypaul/.dotfiles/tree/main/claude/.claude/skills/twelve-factor
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/citypaul/.dotfiles.git /tmp/.dotfiles
mkdir -p .claude/skills
cp -r /tmp/.dotfiles/claude/.claude/skills/twelve-factor .claude/skills/twelve-factor
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Twelve Factor trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Twelve Factor ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Twelve Factor với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Twelve-Factor App Patterns

The original methodology targets software-as-a-service applications. Its principles can inform other deployables, but port binding, concurrency, process disposability, runtime config, and artifact parity do not map literally to every frontend, serverless function, or CLI.

Based on 12factor.net. All 12 factors are covered below: rules, anti-patterns, and code-level implications live here; Node/TypeScript implementation examples live in resources/.

See the typescript-strict skill for schema-first patterns at trust boundaries. See the testing skill for how to TDD these patterns — config validation, shutdown behavior, and backing service integration are all testable through behavior-driven tests.

Deep-dive resources are in the resources/ directory. Load them on demand:

ResourceLoad when...
node-patterns.mdImplementing any factor in Node/TypeScript — config schema validation, options-object injection, .env.example, system dependency checks, backing service factories, Redis session store, web/worker entry points, health checks, graceful shutdown, structured logger, admin scripts

When to Apply

  • Greenfield SaaS/process applications: assess all twelve factors and adopt the ones that fit the runtime and platform.
  • Brownfield applications: adopt improvements incrementally from observed risk. A common starting order is:
    1. Config (Factor III) — add env var validation without restructuring
    2. Logs (Factor XI) — switch to structured process-stream logging
    3. Disposability (Factor IX) — add graceful shutdown handlers
    4. Backing services (Factor IV) — inject platform-appropriate resource bindings
    5. Stateless processes (Factor VI) — migrate in-memory state to backing services

Codebase (Factor I)

One codebase tracked in revision control, many deploys. A codebase can be a repository or a clearly owned part of a monorepo; do not split repositories merely to satisfy the label. Share code through an explicit owned package or module rather than copy-paste.

In a monorepo, each service should have its own entry point, its own deploy pipeline, and its own set of backing service connections. A single repo is fine as long as each service deploys independently.

Config (Factor III)

Store deploy-varying configuration outside the build. Environment variables are the Twelve-Factor default; use an equivalent platform-native secret or configuration injection mechanism when the runtime requires it. Never hardcode credentials or environment-specific endpoints.

Rules:

  • Validate config at startup with a schema — fail fast (exit non-zero, clear error) if config is invalid
  • Inject config via options objects — never import process.env deep in the call tree
  • When you introduce or change a config module, move every process.env read in the application into it in the same change — including settings the reported problem never named, such as a log level, an output path or a feature toggle. A config module that leaves other env reads scattered through request handlers, domain modules and start-up code has relocated the drift, not removed it. Finish by grepping the source for process.env and confirming the only hits are the config module and the process entry points that call it.
  • Document required configuration for the operator, not only in code. Where environment variables are the configuration interface, write or update .env.example in the same change as the schema: one line per setting the schema requires, placeholder values only, never a real credential, and never a committed .env. A validation schema tells the process what is wrong at boot; it does not tell a person what to put on the container — ship both. Where the platform's own manifest or secret-declaration format is the configuration interface, update that instead, by the same rule.

See resources/node-patterns.md for the Zod config schema, options-object injection, and .env.example examples.

Config Anti-Patterns

typescript
const DB_HOST = 'prod-db.internal.example.com';

if (process.env.NODE_ENV === 'production') {
  connectTo('prod-db');
} else {
  connectTo('localhost');
}

const config = require(`./config.${process.env.NODE_ENV}.json`);

Why these are wrong: Config that varies by deploy belongs outside source and build artifacts, supplied through the runtime's supported injection mechanism. Environment-name branching creates combinatorial explosion and breaks dev/prod parity.

The fix: name the thing the branch was actually deciding and make it its own validated setting — SECURE_COOKIES=true, DATABASE_URL=... — then delete the branch. Remove every environment-name comparison in the files you touch, not only the ones covering the settings you were asked about: two deploys given identical settings must behave identically, and one surviving NODE_ENV comparison is exactly what breaks that. Flagging such a branch in your summary instead of removing it leaves the drift in place — fix it, then say you did.

Dependencies (Factor II)

Explicitly declare all dependencies. Never rely on implicit system-wide packages.

Rules:

  • Every dependency in package.json (or equivalent manifest)
  • Lockfile (package-lock.json, pnpm-lock.yaml) committed to repo
  • Dependencies are isolated — the app does not leak from or depend on the system environment (use node_modules, not global installs)
  • System tools invoked through subprocesses are explicit runtime dependencies: document, version where practical, and check them at startup or build time

Backing Services (Factor IV)

Treat every backing service (database, cache, queue, email, storage) as an attached resource supplied at composition time through injected configuration. A URL is common, but platform bindings, socket paths, resource names, handles, and structured credentials are valid when they are the platform contract.

Domain code makes no distinction between local and third-party services. Swapping equivalent providers changes injected resource configuration, not domain behavior; a provider with a different protocol may also require a different adapter. See resources/node-patterns.md for a factory that wires URL-based backing services as one common example.

For projects using hexagonal architecture, backing services map naturally to ports (interfaces) and adapters (implementations). See the hexagonal-architecture skill.

Stateless Processes (Factor VI)

Execute the app as stateless, share-nothing processes. Any data that must persist lives in a backing service. See resources/node-patterns.md for a Redis-backed session store.

Stateless Anti-Patterns

typescript
const sessions = new Map<string, UserSession>();

app.post('/upload', (req, res) => {
  fs.writeFileSync(`/tmp/uploads/${req.file.name}`, req.file.data);
});

let requestCount = 0;
app.use(() => { requestCount++; });

setInterval(() => sendReport(), 60_000);

Why these are wrong: In-memory state is lost on restart and invisible to other process instances. Local filesystem state cannot be shared across processes. An in-process scheduler runs once in every replica, causing duplicate jobs unless a separate coordination mechanism intervenes. Use backing services (Redis, S3, database) and an external scheduler or explicit distributed coordination instead.

See the functional skill for immutable data patterns that naturally support statelessness.

Concurrency (Factor VIII)

Scale out via the process model. Design the app so work can be divided across process types.

Rules:

  • Separate entry points for each process type (web, worker, scheduler) — see resources/node-patterns.md
  • Move durable or long-running background work to an appropriate worker/queue; keep small request-owned work inline when that is simpler and meets latency/retry requirements
  • Each process type scales independently
  • Declare process types in the platform's existing configuration (Procfile is one example)
web: node dist/web.js
worker: node dist/worker.js

Disposability (Factor IX)

Maximize robustness with fast startup and graceful shutdown. See resources/node-patterns.md for health check routes and a full graceful shutdown implementation.

Rules:

  • Handle SIGTERM and SIGINT for graceful shutdown
  • Set a drain timeout — force exit if shutdown hangs
  • Await server.close() to drain in-flight connections
  • Close database pools, Redis connections, queue consumers
  • Exit with non-zero code on shutdown failure
  • Keep startup fast without moving unsafe initialization failures into the first live request
  • Design background jobs to be reentrant/idempotent so interrupted work can be safely retried
  • Provide the health/readiness mechanism expected by the deployment platform when it uses probes

Logs (Factor XI)

Treat logs as event streams. Write structured records to the process streams captured by the deployment platform; stdout is the normal application stream and stderr may carry error records or diagnostics when the collector captures both. Never route or store logs in files from within the app.

A logging change is not finished until three things are true, whatever the request named: records are structured, they go to the process streams and nowhere else, and every record written while serving a request carries that request's identifier. The third is the one most often skipped — a structured stdout log that cannot be filtered to one request still cannot be used during an incident — so bind a per-request child logger at the entry to each handler before you migrate its call sites, and check the handlers for the id before you report the work done.

This factor owns log transport and shape. For what goes into the stream — wide events / canonical log lines, traces, SLOs, alerting — see the observability skill.

For internet-facing servers, RFC 6302 (BCP 162) specifies minimum logging requirements: source and destination addresses and ports, timestamps (preferably UTC), and transport protocol. These should be captured at the server/framework level in addition to application-level structured logging.

Semantic Requirements

For long-running services, start from these properties and defer exact fields to the repository's observability and platform contracts:

  • Structured output — logs are machine-parseable (JSON preferred), not free-form strings
  • process streams only — use the platform's documented stdout/stderr contract; the app never writes to log files or configures file transports
  • Useful severity — use the levels the platform recognises, rank them, and compare every record against a threshold before emitting it. The threshold comes from outside the logger: an option on the factory with a sensible default (so an existing zero-argument call site keeps working), or one deploy-time setting read where config is built and injected. A factory that takes nothing has hard-coded its threshold, and nobody can turn debug on for one deploy without shipping code.
  • Contextual data — logs accept structured metadata (key-value pairs), not just message strings
  • Timestamp included — every log entry includes an ISO 8601 timestamp
  • Request correlation — every record emitted while serving a request carries a request or trace identifier. Take it from the inbound trace or request-id header when one arrives and generate one per request when none does; then give it to each log call for that request, usually by binding a per-request child logger at the entry to the handler and logging through that. "No id arrived" is not a reason to omit the field: an operator who cannot pull one request's lines out of an hour of them cannot use the logs at all. Prefer the platform's W3C trace context integration where it exists.

Projects may use any logging library (pino, winston with console transport, OpenTelemetry, custom) as long as these semantics are met. If an existing logger is missing levels or structured data support, adapt it to meet these requirements. See resources/node-patterns.md for an illustrative logger implementation.

Logging Anti-Patterns

typescript
import fs from 'fs';
fs.appendFileSync('/var/log/app.log', message);

import winston from 'winston';
const logger = winston.createLogger({
  transports: [new winston.transports.File({ filename: 'error.log' })],
});

console.log(`User ${userId} logged in`);

Why these are wrong: File transports mean the app is routing its own logs. Unstructured string interpolation produces logs that cannot be parsed or queried. The execution environment (container orchestrator, PaaS) captures its documented process streams and routes them to the appropriate destination.

Build, Release, Run (Factor V)

Strictly separate build and run stages. Config is injected at release/run time, never baked into the build.

Code-level implications for runtime-configured process apps:

  • Prefer the same build artifact across environments
  • Inject deploy-varying config at release/run time. Compile-time configuration is a different contract and may be necessary for static frontends
  • Releases are immutable — code changes require a new build, not runtime patching

Port Binding (Factor VII)

The app is self-contained and exports its service by binding to a port.

typescript
const server = app.listen(config.PORT, () => {
  logger.info('Server started', { port: config.PORT });
});

This factor applies to long-running web services. Static sites, serverless handlers, and platforms that intentionally own the server lifecycle follow their platform contract instead.

Dev/Prod Parity (Factor X)

Keep development and production similar in the characteristics that affect behavior. Exact products may differ when a compatible substitute is deliberate and contract-tested.

Rules:

  • Prefer production-equivalent backing services for integration and parity tests where semantic differences matter
  • Use the repository's existing local-service mechanism; containers are one option, not a requirement
  • Config schema validation (Factor III) catches mismatches at startup

Admin Processes (Factor XII)

Run admin tasks (migrations, data fixes, console sessions) as one-off processes using the same codebase and config. See resources/node-patterns.md for a migration script example.

Admin scripts live in the repo alongside application code (e.g. scripts/migrate.ts). They are not separate tools or ad-hoc shell commands. Admin processes run in an identical environment to the app — same release, same config, same dependencies.

Testing 12-Factor Patterns

12-factor patterns are testable through behavior-driven tests:

  • Config: test that createConfig throws on missing required vars and returns correct defaults
  • Disposability: test that shutdown closes all connections (inject test doubles for db/cache)
  • Backing services: test supported resource bindings through injected configuration
  • Statelessness: test that request handlers do not depend on prior request state

Config injection via options objects makes all of these patterns naturally testable without mocking process.env or global state. See the testing skill for factory patterns and behavior-driven test examples.

Checklist

  • One owned codebase has many deploys; shared code has an explicit owner
  • Runtime-configured process apps prefer the same build artifact across environments
  • Deploy-varying config is injected outside the build and validated at startup with a schema
  • Startup fails fast with a clear error message if config is invalid
  • An operator-facing example documents every required setting alongside the schema — .env.example where environment variables are the interface, the platform's manifest where that is (placeholders only, no real credentials, no committed .env)
  • All dependencies explicitly declared in manifest with lockfile committed
  • Backing services are injected as platform-appropriate resource locators, handles, or credentials; equivalent providers are swappable at composition time
  • No in-memory session state, no local filesystem state between requests
  • Separate process types exist where work has distinct scaling or lifecycle needs
  • SIGTERM/SIGINT handlers with drain timeout for graceful shutdown
  • Database pools and connections closed on shutdown
  • Platform-required health/readiness probes are implemented
  • Logs are structured on the platform-captured process streams, with no file transports
  • Every request-scoped log record carries a request or trace id, taken from the inbound header or generated for the request when none arrives
  • Long-running web services satisfy the platform's port-binding contract
  • Development and production backing services are parity-tested where differences matter
  • Admin scripts live in the repo and use the same config/dependencies

Tệp đi kèm

Khi kỹ năng đã được tải, mô hình có thể đọc các tệp này nếu cần. Các tệp này chỉ dùng để đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Twelve Factor làm được gì?

Twelve-Factor App patterns for software-as-a-service and long-running process applications. Use when configuring deploy-time environment, backing services, process startup/shutdown, or operational parity. Apply individual principles to frontends, serverless functions, and CLIs only where their runtime and platform make them relevant.

Vì sao nên dùng Twelve Factor trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Twelve Factor là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Twelve Factor trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/citypaul/.dotfiles/tree/main/claude/.claude/skills/twelve-factor. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm rồi cài đặt cả gói dưới dạng một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Twelve Factor?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Twelve Factor với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Twelve Factor với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Twelve Factor có miễn phí không?

Kỹ năng này được citypaul phát hành trên GitHub. Hãy xem điều khoản giấy phép trong kho mã. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇