How to Build a Prompt Chain Step by Step with LangChain, LangGraph, and Claude Agent SDK in 2026

TL;DR
- A prompt chain is a data pipeline. Map its execution pattern — linear, conditional, or stateful — before touching any framework.
- LangChain 1.3.11 handles sequential LCEL pipes; LangGraph 1.2.6 owns conditional flows and stateful graphs.
- Specify input and output contracts at every node boundary. The chain fails where contracts break, not where models fail.
The framework-selection argument starts before developers have written down what kind of chain they’re actually building. You pick LangChain because you’ve seen it in three blog posts. Two nodes in, you discover your chain needs conditional routing — and LangChain’s LCEL pipe just doesn’t model it cleanly. You migrate to LangGraph mid-build. Two days gone, zero specification written. The framework wasn’t wrong. The specification was missing.
Before You Start
You’ll need:
- Python 3.10+ (required for Claude Agent SDK)
- Familiarity with Prompt Chaining as a concept — what a chain does, not how to code one
- A clear picture of your chain’s intended behavior before you read further
This guide teaches you: how to decompose a prompt chain into its execution pattern, specify node-level contracts, match the pattern to the right framework, and validate that the chain does what you specified.
The Framework-First Trap
Here is the failure I see regularly. Developer picks a framework, builds the chain, discovers a mismatch between the chain’s execution logic and what the framework models naturally, then either hacks a workaround into place or restarts. The code changes. The specification never existed.
This isn’t a framework problem. LangChain, LangGraph, and the Claude Agent SDK are all well-built tools. The problem is that each one models a different execution topology — and choosing one before you know your topology means you’re debugging the mismatch instead of building the solution.
Step 1: Identify Your Chain’s Execution Pattern
Before any framework, identify the topology. Most prompt chains fit one of three patterns.
Linear: Input flows through fixed steps in order. Step N always follows step N-1, with no branching and no memory across calls. A document-summarization pipeline is linear. A translation-then-polish chain is linear.
Conditional: At least one step routes differently based on what a previous step returned. A content-moderation chain that sends flagged content to a different handler is conditional. A retry loop that re-prompts on validation failure is conditional.
Stateful: The chain stores and retrieves data between calls or across conversation turns. A research agent that tracks what it has already looked up is stateful. A workflow that resumes from a checkpoint is stateful.
Write yours down. One word: linear, conditional, or stateful. If it’s a mix — conditional with state, or linear with one branching step — write that too.
The Architect’s Rule: If you can describe your chain as “step 1, then step 2, then step 3,” it’s linear. If you find yourself saying “step 2, unless the output is X,” it’s conditional. If the chain needs to remember something from a previous call, it’s stateful. Topology drives tool selection — write it down before you open a framework’s docs.
Step 2: Specify the Contract at Every Node
Pick one node. Write down four things:
- What it receives — exact format (string, JSON object, Pydantic model, field names)
- What it returns — exact format, every field, nullable fields labeled
- What it must never do — the explicit anti-requirements
- How it fails — what happens on timeout, malformed input, or an output that doesn’t validate
Do this for every node before you open an IDE.
Context checklist:
- Model name and version pinned
- Output format defined per node (free text, JSON schema, or a typed Pydantic model)
- Validation rule for every field in the output
- Timeout handling defined per node — not globally
- What the chain does when a node returns unexpected output: raise, retry, or pass through with a fallback value
The Spec Test: If you cannot describe what your step-three node returns when the input is malformed, the model will guess — and undefined contracts mean inconsistent output. Add the error case before you build step three.
One deprecation that will catch you: LLMChain is no longer the pattern in LangChain (LangChain Docs). The current syntax is LCEL pipe chains: prompt | llm | output_parser. If your team has existing code using LLMChain, you’re running deprecated infrastructure. Update before you build on top of it.
Step 3: Match the Framework to the Execution Pattern
You’ve identified your pattern. You’ve written the contracts. Now pick the tool.
Linear sequential chain — LangChain 1.3.11 LCEL
For a linear flow, LangChain 1.3.11 with LCEL pipe chains is the right call. The pipe operator — prompt | llm | output_parser — is readable and directly testable at each step (LangChain Docs). No breaking changes are planned until v2.0 (LangChain Blog). One constraint: keep the chain linear. If you find yourself fighting the pipe to add branching, your chain has outgrown it.
Conditional or stateful flow — LangGraph 1.2.6
LangGraph owns the conditional and stateful cases. Version 1.2, released May 11, 2026, added finer-grained node timeouts, improved error recovery per node, and a streaming API v3 (LangGraph Releases). Use it when your chain branches, retries conditionally, or needs checkpoint-based state across calls.
One critical migration: create_react_agent from langgraph.prebuilt is deprecated as of LangGraph 1.0 — the module was removed. The replacement is create_agent in langchain.agents (LangChain Blog). Do not import from langgraph.prebuilt in new code.
Harness-managed agent loop — Claude Agent SDK 0.2.110
The Claude Agent SDK (v0.2.110, released June 24, 2026, Claude Agent SDK PyPI) is the same harness Claude Code runs on internally. Install with pip install claude-agent-sdk, Python 3.10+ required. Import via from claude_agent_sdk import query, ClaudeAgentOptions, authenticate with ANTHROPIC_API_KEY (Claude Code Docs). It was renamed from “Claude Code SDK” in September 2025.
Use it when you need Claude to manage tool calls and multi-step reasoning internally, as a harness rather than a chain you orchestrate externally. One billing note for teams on existing plans: from June 15, 2026, SDK usage draws from dedicated plan credits, not regular Claude plan limits — check your plan before scaling (Totalum Blog).
Type-safe structured chains — Pydantic AI 2.0.0
PydanticAI 2.0.0, the first stable release, shipped June 23, 2026 (PydanticAI PyPI). If your codebase is already Pydantic-heavy and strict type enforcement at every node boundary is the primary constraint, it’s worth evaluating. It’s not a replacement for LangChain or LangGraph — it’s an alternative for teams where schema violations at runtime are a hard failure condition.
Security & compatibility notes:
- langchain-core (Path Traversal, CVSS 7.5): CVE-2026-34070 in versions below 1.2.22. Current version 1.4.8 is safe. If your environment pins below 1.2.22, update before deploying.
- langchain-core (Serialization Injection “LangGrinch”, CVSS 9.3): CVE-2025-68664 in versions below 1.2.5. Attack surface: serialized chain objects. Safe at 1.2.5+.
- langgraph-checkpoint-sqlite (SQL Injection, CVSS 7.3): CVE-2025-67644 in versions below 3.0.1. If you use SQLite checkpointing, pin to 3.0.1+.
- langgraph-checkpoint-redis (RediSearch Query Injection, CVSS 6.5): CVE-2026-27022 in versions below 1.0.1. Pin to 1.0.2+.
Source: BeyondScale, LangChain LangGraph Security: 2026 CVE Hardening Guide.
Step 4: Validate That the Chain Actually Chains
Built. Now prove it works end-to-end. Not by reading three outputs and nodding — by running assertions against known inputs.
Validation checklist:
- Node output format check — does each node return exactly what the next node’s contract declares as its input? Failure looks like:
KeyError,AttributeError, or the final output arriving in the wrong structure. - Contract boundary test — pass known-bad input into each node individually. Verify that the error surfaces at the node level, not silently at the end of the chain. Failure looks like: the chain completes but returns garbage.
- Conditional branch coverage — if your chain branches, write a test input that triggers each path. Failure looks like: one branch never fires during testing, only in production.
- Idempotency check — run the same input three times. Output content will differ; output structure must not. Failure looks like: the same key is sometimes present, sometimes absent.

Common Pitfalls
| What You Did | Why the Chain Failed | The Fix |
|---|---|---|
| Started by choosing a framework | Framework topology doesn’t match your execution pattern | Write the pattern first — one word: linear, conditional, or stateful |
| Used LCEL for conditional logic | Pipe operator is linear; branching fights the abstraction | Switch to LangGraph for any step that routes on output value |
Imported create_react_agent from langgraph.prebuilt | Module deprecated in LangGraph 1.0, removed | Use create_agent from langchain.agents |
| Left output format undefined at any node | Model chooses format at runtime, inconsistently | Declare exact output schema before writing the node function |
| Tested happy-path inputs only | Contract violations only appear on edge cases | Add explicit error cases to the validation checklist |
Pro Tip
Before you open your IDE, write a one-page data flow document. One row per node: input schema, output schema, error behavior. If you can read that document top-to-bottom and understand what the chain does without seeing the code, your AI coding tool can build it correctly. If the document is ambiguous, the generated code will be ambiguous — and you won’t find out until the edge case hits.
Frequently Asked Questions
Q: How do I build a prompt chain from scratch in Python?
A: Start with the data flow document — one row per node with input schema, output schema, and error behavior. For LangChain 1.3.11, implement with LCEL pipe syntax, not the deprecated LLMChain. For LangGraph 1.2.6, define the graph nodes and edges before writing any node function. One watch-out: pin langchain-core to 1.2.22+ before your first install — older versions carry a path traversal vulnerability (CVE-2026-34070) that affects any environment where chains are deserialized (BeyondScale).
Q: LangChain vs LangGraph vs Claude Agent SDK for prompt chaining — which to choose in 2026?
A: Match the tool to the execution pattern. LangChain 1.3.11 LCEL for linear sequential pipes — no state, no branching. LangGraph 1.2.6 for conditional routing, stateful flows, and checkpoint-based recovery — a free Developer tier on LangGraph Cloud covers lower-traffic projects (LangChain Pricing). Claude Agent SDK for harness-managed loops where Claude handles internal tool calls. PydanticAI 2.0.0 for strict schema enforcement across nodes in Pydantic-heavy stacks. No single framework covers all four patterns — pick by topology, not by familiarity.
Your Spec Artifact
By the end of this guide, you should have:
- A pattern classification — one word: linear, conditional, or stateful (or a documented composition)
- A node contract table — input schema, output schema, error behavior for every node
- A validation checklist with specific failure symptoms for each node boundary
Your Implementation Prompt
Copy this into Claude Code, Cursor, or your preferred AI coding tool after completing every bracket from the node contract table you built in Step 2. Leave no brackets unfilled — blank brackets produce hallucinated defaults.
Build a prompt chain with the following specification.
EXECUTION PATTERN: [linear / conditional / stateful — one word from your Step 1 classification]
FRAMEWORK: [LangChain 1.3.11 LCEL / LangGraph 1.2.6 / Claude Agent SDK 0.2.110]
NODES:
Node 1:
- Name: [descriptive name for this step]
- Input: [exact format — string, JSON with field names, or Pydantic model name]
- Output: [exact format — string, JSON with field names, nullable fields labeled]
- Error behavior: [raise ValueError / return None / retry N times with backoff]
- Must NOT: [explicit anti-requirement — e.g., "must not make external HTTP calls"]
Node 2:
- Name: [descriptive name]
- Input: [must match Node 1's declared output format exactly]
- Output: [exact format]
- Error behavior: [raise / return None / retry]
- Must NOT: [anti-requirement]
[Add one Node block for each step in your chain.]
CONDITIONAL LOGIC (if applicable):
- Branch condition: [which output value triggers which branch, e.g., "if output.category == 'flagged'"]
- Branch A runs: [Node name]
- Branch B runs: [Node name]
VALIDATION REQUIREMENTS:
- Assert each node's return value matches its declared output schema before passing to the next node
- Pass [specific malformed input you identified in Step 4] and verify the error surfaces at node level
- Run with [specific test input] three times and assert output structure is identical across all three runs
Do not add nodes that are not in the specification above. Do not select output formats that differ from the declared schemas. Raise an error at the node boundary if the input does not match the declared input format — do not coerce silently.
Ship It
You can now derive the framework choice from the chain’s execution pattern instead of guessing at the start. The node contract table turns an ambiguous requirement into a testable artifact. That’s the shift — and it’s what separates chains that work on the first deployment from ones that surprise you six nodes in.
— Deploy safe, Max.
AI-assisted content, human-reviewed. Images AI-generated. Editorial Standards · Our Editors