# Your first feedback loop

A software factory is workflows plus feedback loops; this page explains the inner, quality and outer loops and how the earlier concepts combine into one.

> A feedback loop connects agent work to checks and feeds what it learns back into the setup. Your first one combines the earlier building blocks into a small working factory.

Source: https://ai-sw-factory.mellicci.dev/fundamentals/your-first-feedback-loop

**Diagram:** Building blocks combine into an agent loop, a quality loop and an outer loop that improves the setup, inside governance.

- Governance · permissions, sandbox (security model, sandboxing):
  - Work item — issue or task
  - → run
  - Quality loop:
    - Agent loop — model acts, tools run
    - → output
    - Checks — tests · hooks · review
  - → approved
  - Pull request — a person decides

One agent run is not a factory. A factory has a workflow that starts runs from work items, checks that turn output into a decision, and a way to improve from what went wrong. You can describe it as three nested loops. [Loops](https://ai-sw-factory.mellicci.dev/fundamentals/loops) teaches the control-loop primitive; this page combines all the primitives into one small factory.

The **inner loop** is the [agent loop](https://ai-sw-factory.mellicci.dev/fundamentals/how-coding-agents-work): the model acts and the harness runs tools. The **quality loop** wraps a change: tests, linters and [hooks](https://ai-sw-factory.mellicci.dev/fundamentals/hooks) run automatically, a [subagent](https://ai-sw-factory.mellicci.dev/fundamentals/subagents) can review, and a person approves. The **outer loop** carries an issue through a [headless run](https://ai-sw-factory.mellicci.dev/fundamentals/headless-execution) to a pull request, then feeds learnings back. That last return path is the one people forget: it changes the setup itself.

## Why it exists

Without feedback, the same mistakes recur. The agent guesses the wrong command in every run, and someone fixes it by hand in every pull request. Without gates, output quality depends on the model's mood. Without a workflow, a person starts every run.

## How it works

**Diagram:** Automation gates each run and feeds failures back, while you improve the setup whenever a failure repeats.

- One unattended run (sandbox, permissions):
  - Trigger — headless run, work item as input
  - →
  - Shaped by — instruction files and skills
  - → output
  - Gates — hooks and tests return a reason
  - → retry within limits
  - Judge — reviewer subagent or person

## Use it when

- The same kind of work item arrives repeatedly and its correctness can be checked.
- You can name at least one automated check that would have caught the last mistake.

## Use something else when

- The work is exploratory and needs steering → interactive use; see [How coding agents work](https://ai-sw-factory.mellicci.dev/fundamentals/how-coding-agents-work)
- You have no reliable check yet → start with [Hooks](https://ai-sw-factory.mellicci.dev/fundamentals/hooks) or tests before automating
- You cannot yet say what the agent may do unattended → [Security model](https://ai-sw-factory.mellicci.dev/fundamentals/security-model)
- You want to distribute the result across a team → [Plugins](https://ai-sw-factory.mellicci.dev/fundamentals/plugins)

## Key terms

- **Inner loop** — the agent loop itself.
- **Quality loop** — checks that approve or reject output.
- **Outer loop** — the workflow and the learning path back into the setup.
- **Gate** — a check that stops a change on failure.
