# 2.1 Prompt chaining with gates

Sequential LLM calls separated by programmatic gates that localise retries.

1. Task decomposition: outline, draft, polish.
2. Gate 1: programmatic schema validation passes.
3. Gate 2 fails on a missing citation; retry is local.
4. Validation passes; the artifact enters the final stage.

- **Use when:** Fixed subtasks with cheap programmatic validators.
- **Avoid:** Dynamic decomposition or tight latency budgets.

```
outline = llm("Write an outline", topic)
assert has_all_sections(outline)      # check 1: plain code

for attempt in range(3):
    draft = llm("Write the draft", outline)
    if has_sources(draft):            # check 2: plain code
        break

return llm("Polish the text", draft)
```

## References

- [Building effective agents](https://www.anthropic.com/engineering/building-effective-agents) (Anthropic)
- [Workflows and agents](https://docs.langchain.com/oss/python/langgraph/workflows-agents) (LangChain)
- [Demystifying evals for AI agents](https://www.anthropic.com/engineering/demystifying-evals-for-ai-agents) (Anthropic)
