Prompt Chaining with Claude: A Step-by-Step Guide to Improving Response Accuracy
Prompt chaining improves Claude's accuracy by breaking complex tasks into sequential turns where the model references its previous outputs and applies self-correction based on explicit follow-up instructions.
Prompt chaining is a powerful technique for steering Claude through multi-turn conversations that enable iterative refinement and error correction. The anthropics/prompt-eng-interactive-tutorial repository demonstrates this pattern in Anthropic 1P/10.1_Appendix_Chaining Prompts.ipynb, showing how to build a messages array that preserves conversation state across multiple API calls. By implementing prompt chaining, you can significantly reduce hallucinations and improve factual accuracy without complex fine-tuning.
What Is Prompt Chaining and Why It Improves Accuracy
Prompt chaining is the practice of sending multiple sequential requests to Claude while maintaining a running history of the conversation. Unlike single-turn prompting, this technique allows the model to:
- Self-correct by reviewing its previous outputs against new instructions
- Refine initial drafts based on specific constraints provided in follow-up turns
- Maintain context across complex multi-step reasoning tasks
According to the source code analysis, the key architectural insight is that Claude's API accepts a messages parameter containing an array of role-content dictionaries. By appending new entries to this array rather than replacing it, you create a persistent conversation state that enables the chaining pattern.
Core Architecture of Prompt Chaining in Claude
The Messages List Pattern
The foundation of prompt chaining in Claude is the messages list, an array of dictionaries where each dictionary contains a role ("user" or "assistant") and content string. As implemented in Anthropic 1P/10.1_Appendix_Chaining Prompts.ipynb, this list grows with each turn:
# Initialize with first user prompt
messages = [
{"role": "user", "content": "Name ten words that all end with the exact letters 'ab'."}
]
# First API call
first_response = get_completion(messages)
# Append assistant's response and follow-up user request
messages.append({"role": "assistant", "content": first_response})
messages.append({"role": "user", "content": "Please find replacements for all 'words' that are not real words."})
# Second API call for self-correction
second_response = get_completion(messages)
The get_completion Wrapper Function
The tutorial implements a thin wrapper around client.messages.create to standardize API calls. Located in Anthropic 1P/10.1_Appendix_Chaining Prompts.ipynb, this function accepts the accumulated messages list and an optional system_prompt:
import anthropic
client = anthropic.Anthropic(api_key=API_KEY)
def get_completion(messages, system_prompt=""):
response = client.messages.create(
model=MODEL_NAME,
max_tokens=2000,
temperature=0.0,
system=system_prompt,
messages=messages,
)
return response.content[0].text
This wrapper ensures consistent parameters (particularly temperature=0.0 for deterministic outputs) while allowing the messages list to grow across chaining steps.
Implementing Prompt Chaining: Code Examples
Step 1: Initialize the Anthropic Client
Before implementing chaining, instantiate the client using the pattern from the tutorial's hints.py and notebook setup:
import anthropic
# Retrieve stored credentials from the tutorial environment
%store -r API_KEY
%store -r MODEL_NAME
client = anthropic.Anthropic(api_key=API_KEY)
Step 2: First Turn - Generate Initial Content
Create the initial messages array with your first user prompt. This establishes the baseline response that subsequent turns will refine:
first_user = "Name ten words that all end with the exact letters 'ab'."
messages = [
{"role": "user", "content": first_user},
]
first_response = get_completion(messages)
print("Initial response:\n", first_response)
Step 3: Second Turn - Self-Correction
Append the assistant's previous response and a corrective user instruction. This is the core chaining pattern that enables Claude to identify and fix errors in its prior output:
second_user = "Please find replacements for all 'words' that are not real words."
messages.append({"role": "assistant", "content": first_response})
messages.append({"role": "user", "content": second_user})
second_response = get_completion(messages)
print("Corrected response:\n", second_response)
Step 4: Adding Confidence Guards
To further reduce hallucinations, incorporate a confidence guard as demonstrated in Chapter 8 of the tutorial. This system prompt instructs Claude to admit uncertainty rather than fabricate information:
system_prompt = (
"You are a meticulous fact-checker. If you are unsure about any part of your answer, "
'respond with "I don\'t know" instead of guessing.'
)
guarded_response = get_completion(messages, system_prompt=system_prompt)
Advanced: Prompt Chaining with AWS Bedrock
The tutorial repository extends the chaining pattern to AWS Bedrock environments. The AmazonBedrock/boto3/10.1_Appendix_Chaining_Prompts.ipynb file adapts the same messages array pattern using boto3 instead of the direct Anthropic SDK:
import boto3
import json
bedrock = boto3.client(service_name='bedrock-runtime')
def get_completion_bedrock(messages):
body = json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 2000,
"messages": messages
})
response = bedrock.invoke_model(
modelId=MODEL_NAME,
body=body
)
return json.loads(response['body'].read())['content'][0]['text']
Similarly, AmazonBedrock/anthropic/10.1_Appendix_Chaining_Prompts.ipynb demonstrates using the Anthropic SDK with Bedrock-specific model identifiers. Both implementations preserve the identical chaining logic: accumulate messages, append assistant responses, and add corrective user prompts.
Summary
- Prompt chaining improves Claude's accuracy by enabling multi-turn conversations where the model can reference and correct its previous outputs.
- The messages array pattern in
Anthropic 1P/10.1_Appendix_Chaining Prompts.ipynbmaintains conversation state by appending role-content dictionaries rather than replacing them. - The
get_completionwrapper standardizes API calls with deterministic parameters (temperature=0.0) while accepting growing message lists. - Self-correction turns explicitly ask Claude to review prior answers for errors, creating a feedback loop that reduces hallucinations.
- Confidence guards (system prompts requiring "I don't know" responses) can be combined with chaining for additional safety.
- The pattern is portable across environments, with equivalent implementations for direct API access and AWS Bedrock in the tutorial repository.
Frequently Asked Questions
How does prompt chaining differ from single-turn prompting?
Prompt chaining maintains a messages array across multiple API calls, allowing Claude to see its previous responses and apply corrections based on follow-up instructions. Single-turn prompting sends isolated requests without conversation history, preventing the model from self-correcting errors in prior outputs.
Can I use prompt chaining with AWS Bedrock instead of the direct Anthropic API?
Yes. The tutorial repository includes equivalent implementations in AmazonBedrock/boto3/10.1_Appendix_Chaining_Prompts.ipynb and AmazonBedrock/anthropic/10.1_Appendix_Chaining_Prompts.ipynb. These files demonstrate the same messages array pattern using either boto3 or the Anthropic SDK with Bedrock-specific model identifiers.
How many chaining turns should I use for optimal accuracy?
The tutorial demonstrates effective self-correction with two turns (initial generation plus one corrective pass). However, you can extend the pattern indefinitely by continuing to append assistant responses and corrective user prompts to the messages array. Each additional turn provides another opportunity for refinement, though latency and token costs increase linearly with conversation length.
What is the difference between prompt chaining and retrieval-augmented generation (RAG)?
Prompt chaining is a conversation management technique that structures multi-turn interactions to enable self-correction and iterative refinement. RAG is an information retrieval technique that injects external documents into the context window. These approaches are complementary—you can use prompt chaining to iteratively refine answers while using RAG to provide the external knowledge base for each turn.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →