Skip to content
langchain-core-workflow-a logo

LangChain Core Workflow A: Chains & Prompts

langchain-core-workflow-a

Build LangChain chains and prompts for structured LLM workflows. Use when creating prompt templates, building LCEL chains, or implementing sequential processing pipelines. Trigger with phrases like "langchain chains", "langchain prompts", "LCEL workflow", "langchain pipeline", "prompt template".

SKILL.md

Full skill instructions

LangChain Core Workflow A: Chains & Prompts

Overview

Build production-ready chains using LangChain Expression Language (LCEL) with prompt templates, output parsers, and composition patterns.

Prerequisites

  • Completed langchain-install-auth setup
  • Understanding of prompt engineering basics
  • Familiarity with Python type hints

Instructions

Step 1: Create Prompt Templates

from langchain_core.prompts import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
    MessagesPlaceholder
)

# Simple template
simple_prompt = ChatPromptTemplate.from_template(
    "Translate '{text}' to {language}"
)

# Chat-style template
chat_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template(
        "You are a {role}. Respond in {style} style."
    ),
    MessagesPlaceholder(variable_name="history", optional=True),
    HumanMessagePromptTemplate.from_template("{input}")
])

Step 2: Build LCEL Chains

from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser

llm = ChatOpenAI(model="gpt-4o-mini")

# Basic chain: prompt -> llm -> parser
basic_chain = simple_prompt | llm | StrOutputParser()

# Invoke the chain
result = basic_chain.invoke({
    "text": "Hello, world!",
    "language": "Spanish"
})
print(result)  # "Hola, mundo!"

Step 3: Chain Composition

from langchain_core.runnables import RunnablePassthrough, RunnableParallel

# Sequential chain
chain1 = prompt1 | llm | StrOutputParser()
chain2 = prompt2 | llm | StrOutputParser()

sequential = chain1 | (lambda x: {"summary": x}) | chain2

# Parallel execution
parallel = RunnableParallel(
    summary=prompt1 | llm | StrOutputParser(),
    keywords=prompt2 | llm | StrOutputParser(),
    sentiment=prompt3 | llm | StrOutputParser()
)

results = parallel.invoke({"text": "Your input text"})
# Returns: {"summary": "...", "keywords": "...", "sentiment": "..."}

Step 4: Branching Logic

from langchain_core.runnables import RunnableBranch

# Conditional branching
branch = RunnableBranch(
    (lambda x: x["type"] == "question", question_chain),
    (lambda x: x["type"] == "command", command_chain),
    default_chain  # Fallback
)

result = branch.invoke({"type": "question", "input": "What is AI?"})

Output

  • Reusable prompt templates with variable substitution
  • Type-safe LCEL chains with clear data flow
  • Composable chain patterns (sequential, parallel, branching)
  • Consistent output parsing

Examples

Multi-Step Processing Chain

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(model="gpt-4o-mini")

# Step 1: Extract key points
extract_prompt = ChatPromptTemplate.from_template(
    "Extract 3 key points from: {text}"
)

# Step 2: Summarize
summarize_prompt = ChatPromptTemplate.from_template(
    "Create a one-sentence summary from these points: {points}"
)

# Compose the chain
chain = (
    {"points": extract_prompt | llm | StrOutputParser()}
    | summarize_prompt
    | llm
    | StrOutputParser()
)

summary = chain.invoke({"text": "Long article text here..."})

With Context Injection

from langchain_core.runnables import RunnablePassthrough

def get_context(input_dict):
    """Fetch relevant context from database."""
    return f"Context for: {input_dict['query']}"

chain = (
    RunnablePassthrough.assign(context=get_context)
    | prompt
    | llm
    | StrOutputParser()
)

result = chain.invoke({"query": "user question"})

Error Handling

ErrorCauseSolution
Missing VariableTemplate variable not providedCheck input dict keys match template
Type ErrorWrong input typeEnsure inputs match expected schema
Parse ErrorOutput doesn't match parserUse more specific prompts or fallback

Resources

Next Steps

Proceed to langchain-core-workflow-b for agents and tools workflow.

More Productivity & Planning skills

brainstorming logo
Productivity & Planning

brainstorming

Structured design dialogue that validates ideas before implementation begins.

295.4K 385.7K
View
ui-ux-pro-max logo
Productivity & Planning

ui-ux-pro-max

Comprehensive design intelligence for web and mobile UI/UX across 10 technology stacks.

133.1K 383.9K
View
writing-plans logo
Productivity & Planning

writing-plans

Comprehensive implementation plans for multi-step tasks, breaking down specs into bite-sized, testable steps.

295.4K 268K
View
using-superpowers logo
Productivity & Planning

using-superpowers

Introduction to the obra skills system with mandatory skill invocation rules and best practices.

295.4K 259.8K
View
executing-plans logo
Productivity & Planning

executing-plans

Execute a written implementation plan with critical review and task checkpoints.

295.4K 229.1K
View
dispatching-parallel-agents logo
Productivity & Planning

dispatching-parallel-agents

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View
using-git-worktrees logo
Productivity & Planning

using-git-worktrees

Isolated git worktrees with smart directory selection and safety verification.

295.4K 205.2K
View
webapp-testing logo
Productivity & Planning

webapp-testing

Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.

179.7K 170.6K
View
content-strategy logo
Productivity & Planning

content-strategy

Plan searchable and shareable content that drives traffic, builds authority, and generates leads.

53.3K 150.9K
View
repo-intake-and-plan logo
Productivity & Planning

repo-intake-and-plan

README-first repository scanner that extracts commands and classifies reproduction candidates without executing them.

497 139.6K
View
marketing-ideas logo
Productivity & Planning

marketing-ideas

Brainstorm and prioritize marketing strategies tailored to your SaaS stage, budget, and goals.

53.3K 137.2K
View
site-architecture logo
Productivity & Planning

site-architecture

Plan and optimize your website's page hierarchy, navigation, URL structure, and internal linking.

53.3K 112.8K
View

Productivity AI tools

Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View
SaveDay logo
Productivity

SaveDay

Capture, organize, and utilize your knowledge effortlessly.

Free
View
A
Productivity

Any Summary

Instant Summaries of Audio & Video Interviews with AnySummary

Freemium
View
M
Productivity

Map This

Transform PDFs into engaging mind maps.

Freemium
View
ChatPDF logo
Productivity

ChatPDF

Chat with any PDF instantly

Free
View
I
Productivity

intellisay

Create an optimal daily plan using your voice

Paid
View
A
Productivity

Aurora AI

A productivity platform to centralize organizational knowledge and workflows with contextual AI assistance.

Paid
View
AskYourPDF logo
Productivity

AskYourPDF

AskYourPDF Pricing Plans: Tailored to Your Needs

Free
View