Docsity

Tutorial about how use AI best, Sintesi del corso di Machine learning

Toggle sidebar
bg1
■ AI TUTORIAL #2
Prompt Chaining
Making AI Do Multi-Step Reasoning
What You'll Learn
Instead of one large, complex prompt, you break tasks into a chain of smaller, focused prompts. Each
step's output becomes the next step's input — giving you more control, easier debugging, and far more
reliable results from any LLM.
The Core Idea
Think of it like an assembly line. Each station does one job well, rather than one worker trying to do
everything at once:
Step 1 Prompt: "Extract the 3 main claims from this article: {article}"
↓
Step 2 Prompt: "For each claim, find a counterargument: {claims}"
↓
Step 3 Prompt: "Write a balanced summary using both sides: {claims + counterargs}"
Implementation (Python Pseudocode)
Any LLM API works here — OpenAI, Anthropic, or a local model via Ollama:
def llm(prompt):
# Call your preferred LLM API here
...
# Step 1: Extract claims
claims = llm(f"Extract 3 main claims from: {article}")
# Step 2: Find counterarguments
counterargs = llm(f"Find counterarguments for: {claims}")
# Step 3: Synthesise
summary = llm(f"Write a balanced summary using:\n{claims}\n{counterargs}")
print(summary)
When to Use Prompt Chaining
• Research tasks — gather, then evaluate, then synthesise
pf2

Anteprima parziale del testo

Scarica Tutorial about how use AI best e più Sintesi del corso in PDF di Machine learning solo su Docsity!

n AI TUTORIAL #

Prompt Chaining

Making AI Do Multi-Step Reasoning

What You'll Learn

Instead of one large, complex prompt, you break tasks into a chain of smaller, focused prompts. Each

step's output becomes the next step's input — giving you more control, easier debugging, and far more

reliable results from any LLM.

The Core Idea

Think of it like an assembly line. Each station does one job well, rather than one worker trying to do

everything at once:

Step 1 Prompt: "Extract the 3 main claims from this article: {article}" ↓ Step 2 Prompt: "For each claim, find a counterargument: {claims}" ↓ Step 3 Prompt: "Write a balanced summary using both sides: {claims + counterargs}"

Implementation (Python Pseudocode)

Any LLM API works here — OpenAI, Anthropic, or a local model via Ollama:

def llm(prompt):

Call your preferred LLM API here

...

Step 1: Extract claims

claims = llm(f"Extract 3 main claims from: {article}")

Step 2: Find counterarguments

counterargs = llm(f"Find counterarguments for: {claims}")

Step 3: Synthesise

summary = llm(f"Write a balanced summary using:\n{claims}\n{counterargs}")

print(summary)

When to Use Prompt Chaining

  • Research tasks — gather, then evaluate, then synthesise
  • Code generation — plan, then implement, then review
  • Content creation — outline, then draft, then edit for tone
  • Data analysis — clean, then analyse, then explain findings

Why It Works Better Than One Big Prompt

Large, multi-objective prompts force the model to juggle competing goals simultaneously, which degrades

quality. Chaining gives each step a single, clear objective. It also means that when something goes wrong,

you know exactly which step failed — and you only need to re-run that step, saving time and API cost.

Advanced pattern: Add a validation step between each chain link — a short prompt that checks whether

the previous output meets quality criteria before passing it on. This turns your chain into a self-correcting

pipeline.

Common Pitfalls to Avoid

Don't make your chain too long — 3 to 5 steps is usually optimal. Each handoff introduces a small chance

of error propagation, so keep prompts tight and outputs structured (e.g., numbered lists or JSON) so the

next step can parse them reliably.

AI Tutorial Series · Generated April 2026