# Prompt Chaining

> The Prompt Chaining is a technique of splitting a complex task into a sequence of prompts where each uses the previous result.

## What is prompt chaining in LLM apps?

The technique of breaking a task into steps, such as outline, draft and review, each handled by a separate prompt that receives the output of the previous one. Makes results more reliable and each step easier to debug.

- Topic: Prompting
- Type: technique

## Example

Wrong:

```js
const outline = await llm.generate({ prompt: outlinePrompt(topic) });
const draft = await llm.generate({ prompt: draftPrompt(outline.text) });
```

Right:

```js
const outline = await llm.generate({ prompt: outlinePrompt(topic) });
const sections = parseOutline(outline.text);
if (sections.length === 0) throw new Error('Outline step failed');
const draft = await llm.generate({ prompt: draftPrompt(sections) });
```

Validating each step's output stops a bad outline from silently ruining every step after it.

## Common mistakes

- One bad step silently corrupts every step after it.

## Key points

- Each step's output is validated before it is passed to the next step.
- Each step has one clear task and a defined input and output format.
- A failed step is retried or stops the chain instead of passing bad data forward.

## Interview questions

### Prompt chaining vs one big prompt?

A chain splits the task so each step is simpler to test and debug. One big prompt needs fewer calls but is harder to control.

### Can steps in a prompt chain run in parallel?

Yes. Steps that do not depend on each other can run at the same time, and their results are merged later.

### Prompt chaining vs AI agents?

In a chain, code fixes the order of steps. An agent lets the model choose the next step itself.

## Related cards

- [Agentic Workflow](https://wonderium.dev/ai/agentic-workflow.md)
- [Structured Output](https://wonderium.dev/ai/structured-output.md)
- [Prompt Template](https://wonderium.dev/ai/prompt-template.md)
- [Multi-Agent System](https://wonderium.dev/ai/multi-agent-system.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/prompt-chaining
- Last updated: 2026-11-25
