Study guide

7.2 Workflow Patterns: Prompt Chaining & Routing

Claude Certified Developer - Foundations CCDV-F

Track your progress as you read

Finished sections get a check mark.

1 Chapter 1: Core Messages API & Streaming

1.1 Messages API Fundamentals & Request Lifecycle 1.2 Streaming & Server-Sent Events (SSE) 1.3 Thinking: Adaptive Mode, Effort & Reasoning Budgets

2 Chapter 2: Optimization & Asynchronous Batches

2.1 Prompt Caching Mechanics & Breakpoints 2.2 Message Batches API & Asynchronous Workloads

3 Chapter 3: Structured Outputs, Documents & Files API

3.1 Structured Outputs & JSON Extraction 3.2 Files API, Document Blocks & Citations

4 Chapter 4: Application Design, Resilience & Delivery Life Cycle

4.1 API Resilience, Error Handling & Retries 4.2 Production Backend Architecture & State Management 4.3 Requirements Analysis & Claude Application Design 4.4 The Claude Application Life Cycle & Configuration Management

5 Chapter 5: LLM Fundamentals, Model Selection & Versioning

5.1 LLM & Technical Fundamentals: Tokens, Context Windows & Sampling 5.2 Claude Model Family Selection 5.3 Model Versioning, Snapshot Pinning & Migration 5.4 Latency & Throughput Optimization

6 Chapter 6: Cost Engineering & Context Management

6.1 Production Cost Optimization Strategies 6.2 Context Window Management & Compaction

7 Chapter 7: Foundations of Workflows & Deterministic Patterns

7.1 Agent vs. Workflow Architectural Foundations 7.2 Workflow Patterns: Prompt Chaining & Routing 7.3 Workflow Patterns: Parallelization & Voting

8 Chapter 8: Autonomous Agents & Multi-Step Systems

8.1 Workflow Patterns: Orchestrator-Workers & Evaluator-Optimizer 8.2 Agent Loop Implementation & State Tracking 8.3 Agent Frameworks & Human-in-the-Loop (HITL)

9 Chapter 9: Prompt and Context Engineering

9.1 System Prompt Design & Role Framing 9.2 XML Tags & Structural Prompt Delimiters 9.3 Few-Shot Prompting & In-Context Learning 9.4 RAG Context Injection & Document Formatting

10 Chapter 10: Tool Calling & Model Context Protocol (MCP)

10.1 Tool Schema Design & Parameter Validation 10.2 Tool Execution, Multi-Tool Calls & Error Handling 10.3 MCP Architecture & Core Primitives 10.4 MCP Transports & Client/Server Integration 10.5 Agentic Customization: Built-in Tools, Custom Tools, Skills & MCP

11 Chapter 11: Security, Safety & Governance

11.1 Direct & Indirect Prompt Injection Defenses 11.2 Tool Permissions, Sandboxing & Least Privilege 11.3 Data Privacy, Redaction & Audit Logging 11.4 Identity, Secrets & API Key Management

12 Chapter 12: Claude Code Developer Tooling

12.1 Claude Code Architecture & Project Configuration 12.2 Permissions, Modes & Hooks

13 Chapter 13: Evaluation, Testing & Production Observability

13.1 Evaluation Design & Golden Datasets 13.2 Production Observability, Tracing & Debugging

7.2 Workflow Patterns: Prompt Chaining & Routing

Key Takeaways Explain This Topic AI

Prompt Chaining decomposes complex cognitive transformations into a sequential pipeline of highly focused prompts, reducing instruction drift and token context clutter.

Programmatic gates between chain steps implement deterministic validation (schema checks, assertions, unit tests) to enforce early error termination, circuit breaking, and isolated step retries.

Intelligent Model Routing optimizes both cost and latency by classifying incoming requests and dispatching them dynamically to the most cost-effective model tier (e.g., Claude Haiku 5.5 for triage vs. Claude Sonnet 5.5 for deep reasoning).

Task-based routing enforces prompt specialization, replacing bloated, monolithic system prompts with lean, dedicated instructions tailored to specific operational domains.

Last updated: October 2026

Workflow Patterns: Prompt Chaining & Routing

Exam Blueprint Focus: The CCDV-F examination places heavy emphasis on building reliable, cost-effective deterministic workflows. Candidates must master Prompt Chaining with programmatic validation gates (fail-fast checks, circuit breaking, and selective retries) as well as Intelligent Routing (dispatching queries across model tiers like Claude Haiku 5.5 and Claude Sonnet 5.5, and partitioning monolithic prompts into specialized domain handlers).

The Prompt Chaining Pattern: Sequential Task Decomposition

In production LLM application design, attempting to solve a multi-faceted business problem using a single massive prompt is a well-known anti-pattern. Monolithic prompts that ask the model to ingest guidelines, extract structured data, perform complex reasoning, cross-reference rules, draft prose, and format outputs frequently suffer from instruction drift , missed edge cases, and high failure rates.

Prompt Chaining solves this by decomposing a complex, multi-stage task into a sequential pipeline of smaller, highly targeted prompts where the output of step N N N becomes part of the input context for step N + 1 N+1 N + 1 . [Raw User Input]

  • │
  • ▼
  • ┌──────────────┐ Structured ┌──────────────┐ Refined Output ┌──────────────┐

│ Step 1: LLM │ ──────────────────► │ Step 2: LLM │ ─────────────────────► │ Step 3: LLM │ ──► [Final Response]

  • │ (Extraction) │ Intermediate │ (Reasoning) │ Intermediate │ (Formatting) │
  • └──────────────┘ Payload └──────────────┘ Payload └──────────────┘
  • Why Prompt Chaining Outperforms Monolithic Prompts

Cognitive Load Reduction: Large language models have finite attentional capacity. By constraining each call to a single cognitive task (e.g., Step 1: extract data; Step 2: verify against policy; Step 3: draft email), the model achieves significantly higher accuracy and strict adherence to negative constraints.

Context Window Hygiene: Rather than accumulating the entire conversational reasoning trace, intermediate steps can discard irrelevant background text and pass only clean, distilled artifacts to subsequent steps.

Modular Unit Testing and Evals: Each step in the chain has explicit inputs and outputs, allowing engineering teams to write isolated unit tests, track token costs per stage, and run regression evaluations against specific prompt versions without testing the entire system end-to-end.

Targeted Model Selection: Different steps can utilize different models. Step 1 (simple extraction) can run on Claude Haiku 5.5 (from $0.10/MTok input), Step 2 (deep synthesis) on Claude Sonnet 5.5 ($2.00/MTok input), and Step 3 (formatting) back on Haiku, slashing overall pipeline costs.

Programmatic Gates: Deterministic Verification Between Steps

A naive prompt chain simply pipes the raw string output of one LLM directly into the input of the next. In an enterprise system, this is fragile: if Step 1 hallucinates or outputs malformed text, that error cascades down the pipeline, causing subsequent steps to fail.

An enterprise-grade prompt chain introduces Programmatic Gates (deterministic code validations) between LLM steps: [Step 1: LLM Extraction]

  • │
  • ▼
  • ┌──────────────────────────────────────┐
  • │ Programmatic Gate (Host Code) │
  • │ - Pydantic / Zod Schema Validation │
  • │ - Deterministic Business Rules │
  • │ - Regex & Range Assertions │
  • └──────────────────────────────────────┘
  • │ │
  • [Pass] [Fail]
  • │ ▼
  • ▼ ┌──────────────────────────────────────┐
  • [Step 2: LLM Synthesis] │ Circuit Breaker / Targeted Retry │
  • │ - Retry Step 1 with validation error │
  • │ - Or fail-fast to human escalation │
  • └──────────────────────────────────────┘
  • Core Functions of Programmatic Gates
  • 1. Early Error Termination (Fail-Fast)

If the output of Step 1 violates fundamental invariants (e.g., missing required JSON fields, negative monetary values, invalid account IDs), execution halts immediately. This prevents burning tokens and latency on downstream steps that are guaranteed to fail.

2. Circuit Breaking

If a step fails validation repeatedly (e.g., three consecutive attempts), the circuit breaker trips. The system aborts the automated pipeline and cleanly falls back to a default safe response, an asynchronous human-in-the-loop queue, or an alert to the operations team.

3. Targeted Retries with Error Feedback

When an intermediate step fails validation, the system does not need to restart the entire multi-step pipeline from scratch. Instead, it re-invokes only that specific step , injecting the deterministic error message back into the prompt:

"Your previous output failed validation with error: 'Field interest_rate must be a float between 0.0 and 1.0, received 4.5'. Please correct this and re-generate the JSON object."

The Routing Pattern: Intent Classification & Dynamic Dispatch

While Prompt Chaining connects steps sequentially, Routing is a branching architecture. The routing pattern classifies an incoming request once and dispatches it to a specialized downstream handler, model tier, or system prompt. [Incoming User Request]

  • │
  • ▼
  • ┌──────────────────────────────────┐
  • │ Classifier / Router │
  • │ (Claude Haiku 5.5 / Schema) │
  • └──────────────────────────────────┘
  • │ │ │
  • ┌───────────────────┘ │ └───────────────────┐
  • ▼ ▼ ▼
  • [Route A: Technical] [Route B: Billing] [Route C: General/Fallback]
  • Claude Sonnet 5.5 - Claude Haiku 5.5 - Claude Haiku 5.5
  • Deep Coding Prompt - Account DB Schema - FAQ Knowledge Base
  • Code Sandbox Tools - SQL Query Tools - Standard Persona
  • The Two Primary Dimensions of Routing
  • 1. Complexity-Based Model Routing (Cost & Latency Optimization)

In many production environments, a large share of user queries is routine (e.g., checking status, FAQ lookup, basic formatting), while a smaller share needs deeper reasoning. Measure your own traffic mix before sizing the saving.

Sending every query to Claude Sonnet 5.5 ($2.00 / $10.00 per MTok) wastes budget on routine requests.

Sending complex queries to Claude Haiku 5.5 (from $0.10 / $0.50 per MTok) risks lower-quality answers and task failure.

The Router Solution: A lightweight Claude Haiku 5.5 classification call at low effort evaluates the complexity of the request quickly. Simple queries are handled directly by Haiku, while complex reasoning queries are routed to Sonnet. The blended saving depends on how much traffic the cheap tier can resolve at acceptable quality, which an eval should confirm.

2. Task-Based Prompt Specialization (Eliminating Monolithic System Prompts)

A common anti-pattern is writing a single, sprawling 5,000-token system prompt that instructs Claude how to handle billing questions, security disputes, technical bug reports, API integration, and general chit-chat.

The Problem: Monolithic prompts suffer from token waste (every user turn pays for 5,000 tokens of mostly irrelevant instructions) and degraded instruction adherence.

The Router Solution: The router classifies the user's intent into a specific domain ( "billing" , "technical_support" , "security" , "sales" ). The request is then dispatched to a dedicated micro-prompt of only 400 tokens tailored specifically to that domain.

  • Implementation Patterns in Python
  • 1. Robust Classification via Anthropic Tool Choice

When implementing a router, relying on free-form text output (e.g., asking the model to respond with just the word "BILLING") is brittle. The model might output "The category is BILLING" or " \n BILLING." , breaking application code.

The production best practice is to constrain the output to an enum schema: either structured outputs ( output_config.format ) or a classification tool marked strict: true . On Claude Haiku 5.5 you can also force that tool with tool_choice ; Claude Opus 5.5, Claude Sonnet 5.5, and Claude Fable 5.1 return a 400 error for a forced tool_choice , so on those models use structured outputs or auto with a prompt instruction naming the tool. Do not send temperature : Claude 4.7 and later models reject a non-default value. import anthropic

from typing import Literal

client = anthropic.Anthropic()

  • # Define the routing schema using Anthropic tool definitions
  • routing_tool = {
  • "name": "route_inquiry",
  • "description": "Classify the user inquiry into the appropriate operational category.",
  • "strict": True,
  • "input_schema": {
  • "type": "object",
  • "properties": {
  • "category": {
  • "type": "string",
  • "enum": ["billing_refund", "technical_code", "general_faq", "account_security"],
  • "description": "The classified operational category."
  • },
  • "complexity": {
  • "type": "string",
  • "enum": ["low", "high"],
  • "description": "Assess whether deep multi-step reasoning or code analysis is required."
  • },
  • "confidence": {
  • "type": "number",
  • "description": "Confidence score between 0.0 and 1.0."
  • }
  • },
  • "required": ["category", "complexity", "confidence"],
  • "additionalProperties": False
  • }
  • }

def route_incoming_query(user_query: str) -> dict:

# Use Claude Haiku 5.5 for fast, low-cost classification.

# Haiku 5.5 accepts a forced tool_choice (the forced call skips thinking);

# no temperature parameter: Claude 4.7+ models reject non-default values.

  • response = client.messages.create(
  • model="claude-haiku-5-5",
  • max_tokens=150,
  • tools=[routing_tool],
  • tool_choice={"type": "tool", "name": "route_inquiry"},
  • messages=[{"role": "user", "content": user_query}]
  • )

# Extract deterministic structured arguments directly from tool_use block

for block in response.content:

if block.type == "tool_use" and block.name == "route_inquiry":

return block.input

  • # Safe fallback for unexpected edge cases
  • return {"category": "general_faq", "complexity": "low", "confidence": 0.5}
  • 2. End-to-End Chaining with Pydantic Validation Gate

Here is a complete, production-grade pattern combining sequential chaining with a programmatic validation gate: from pydantic import BaseModel, Field, ValidationError

class ExtractedFinancialData(BaseModel):

  • company_name: str
  • fiscal_year: int = Field(ge=2000, le=2030)
  • revenue_usd: float = Field(gt=0)
  • net_income_usd: float

def step_1_extract_metrics(raw_filing_text: str) -> str:

  • """Step 1: Extract financial metrics as JSON with structured outputs."""
  • # Structured outputs replace the old "{" prefill, which returns a 400 error

# on Claude 4.6 and later models.

  • response = client.messages.parse(
  • model="claude-haiku-5-5",
  • max_tokens=1024,
  • system="Extract the company's financial metrics from the filing.",
  • output_format=ExtractedFinancialData,
  • messages=[{"role": "user", "content": raw_filing_text}],
  • )
  • return response.parsed_output.model_dump_json()

def programmatic_gate(raw_json_str: str) -> ExtractedFinancialData:

"""Programmatic Gate: Validate schema and business rules."""

try:

  • data = ExtractedFinancialData.model_validate_json(raw_json_str)
  • # Enforce business logic constraint

if data.net_income_usd > data.revenue_usd:

  • raise ValueError("Net income cannot exceed total revenue.")
  • return data

except (ValidationError, ValueError) as e:

raise ValueError(f"Programmatic Gate Failed: {e}")

def step_2_generate_briefing(validated_data: ExtractedFinancialData) -> str:

  • """Step 2: Generate executive briefing on validated data."""
  • prompt = (
  • f"Generate an executive briefing for {validated_data.company_name} (FY{validated_data.fiscal_year}).\n"
  • f"Revenue: ${validated_data.revenue_usd:,.2f} | Net Income: ${validated_data.net_income_usd:,.2f}."
  • )
  • response = client.messages.create(
  • model="claude-sonnet-5-5",
  • max_tokens=2048,
  • system="You are a senior equity analyst. Provide a 3-bullet financial briefing.",
  • messages=[{"role": "user", "content": prompt}]
  • )

# Thinking is on by default on Sonnet 5.5, so select the text block by type.

return next(b.text for b in response.content if b.type == "text")

def execute_pipeline(raw_filing: str) -> str:

  • # Step 1: LLM Extraction
  • raw_json = step_1_extract_metrics(raw_filing)

# Programmatic Gate: Deterministic Validation & Circuit Breaker

try:

validated_metrics = programmatic_gate(raw_json)

except ValueError as err:

  • # Circuit Breaker: Fail-fast or route to human auditor
  • return f"Pipeline Aborted by Gate: {err}"
  • # Step 2: High-Reasoning LLM Briefing
  • return step_2_generate_briefing(validated_metrics)
  • Comparing Prompt Chaining vs. Routing

Architectural Dimension Prompt Chaining Intelligent Routing

Pipeline Topology Sequential (Pipeline of stages: A → B → C A \to B \to C A → B → C ) Branching (One-to-many dispatch: A → B A \to B A → B OR C C C OR D D D )

Primary Purpose Decompose complex multi-stage tasks; reduce cognitive load Direct query to optimal model tier or specialized domain prompt

Latency Profile Cumulative (Sum of t i t_i t i ​ across all sequential steps) Minimal ( t router + t specialist t_{\text{router}} + t_{\text{specialist}} t router ​ + t specialist ​ )

Token Economics Increases total tokens across steps, but each prompt is lean Cuts blended costs in proportion to the share of queries the cheaper tier resolves

Error Mechanics Cascading failure risk mitigated by programmatic gates Misclassification risk mitigated by confidence scores & fallbacks

Gate Placement Between every sequential transformation node Directly on classifier output before dispatch

Loading diagram... Prompt Chaining with Programmatic Gates vs. Intelligent Routing