0PricingLogin
AI Prompt Engineering · Lesson

Output-to-Input Patterns

Extracting structured data from Step 1 to inject into Step 2.

The Core Challenge: Extracting and Injecting

In a prompt chain, Step 1 produces text. Step 2 needs a specific piece of that text as input. The challenge is reliably extracting exactly the right field from Step 1's output and injecting it cleanly into Step 2's prompt.

If Step 1 returns unstructured prose, extraction is fragile. The solution is to design Step 1 prompts to return structured output — typically JSON — that can be parsed and injected programmatically.

Designing Step 1 for Machine Consumption

A prompt intended to feed a chain should always output structured data. Specify the exact JSON schema in the prompt:

import anthropic
import json

client = anthropic.Anthropic(api_key='YOUR_API_KEY')

step1_prompt = '''
<task>
Analyze the customer review below.
</task>

<review>
The onboarding was confusing and took 3 hours. The core feature works great though.
</review>

<output_format>
Return ONLY a JSON object. No other text.
{
  "sentiment": "positive|negative|mixed",
  "issues": ["string"],
  "positives": ["string"],
  "priority": "high|medium|low"
}
</output_format>
'''

response = client.messages.create(
    model='claude-opus-4-5',
    max_tokens=300,
    messages=[{'role': 'user', 'content': step1_prompt}]
)
print(response.content[0].text)

All lessons in this course

  1. What Is Prompt Chaining?
  2. Output-to-Input Patterns
  3. Sequential Transformation Chains
  4. Error Handling in Prompt Chains
← Back to AI Prompt Engineering