Foundations: LangChain and LangGraph
LangChain Overview
LangChain is "the platform for reliable agents" - a framework for building applications powered by Large Language Models through a standard interface for models, embeddings, vector stores, and more [1].
Core Value Propositions:
- Real-time data augmentation: Connect LLMs to diverse data sources and external systems
- Model interoperability: Swap models easily as the industry evolves
- Rapid prototyping: Build and iterate quickly with modular, component-based architecture
- Production-ready features: Built-in support for monitoring, evaluation, and debugging through LangSmith
Installation:
pip install langchainCore Architecture Components
LangChain's architecture is designed to be modular and extensible [2]:
| Component | Description |
|---|---|
| Models | Standard interface for interacting with various LLMs (OpenAI, Anthropic, Hugging Face, etc.) |
| Chains | Sequences of calls to LLMs or other utilities that can be composed together |
| LCEL | LangChain Expression Language - declarative way to compose chains with streaming, batching, and async support |
| Agents | Systems that use LLMs to decide which actions to take |
| Tools | Functions that agents can use to interact with the world |
LangGraph: Stateful Agent Orchestration
LangGraph extends LangChain with the ability to coordinate multiple chains across multiple steps of computation in a cyclical manner [3]. It is specifically designed for building agent-like applications where control flow is not a simple linear sequence.
Key Features:
- Stateful, Long-Running Execution: Applications can run for extended periods, maintaining state and automatically resuming from where they left off
- Human-in-the-Loop: Seamless incorporation of human oversight into agentic workflows
- Comprehensive Memory: Both short-term working memory and long-term persistent memory across sessions
- Debugging with LangSmith: Deep visibility into agent behavior through visualization and tracing tools
State Graph Architecture
LangGraph represents workflows as graphs where:
- State: Represented by a
Stateobject that accumulates information - Nodes: Functions that update the state
- Edges: Control flow determining which node executes next
Integration Patterns
LangGraph supports several common workflow patterns [4]:
| Pattern | Description | Use Case |
|---|---|---|
| Prompt Chaining | Sequential LLM calls where each processes the previous output | Tasks broken into verifiable steps |
| Parallelization | Multiple LLM calls executed simultaneously | Independent subtasks, confidence checking |
| Routing | Input processing and direction to context-specific tasks | Specialized flows for complex tasks |
| Orchestrator-Worker | Central orchestrator coordinates multiple workers | Task planning and delegation |
| Evaluator-Optimizer | Evaluator assesses output, optimizer improves generator | High-quality, iterative refinement |
Code Example
# Basic LangChain Agent Example
# Source: LangChain Documentation
from langchain.agents import create_agent
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
agent = create_agent(
model="claude-sonnet-4-5-20250929",
tools=[get_weather],
system_prompt="You are a helpful assistant",
)
# Run the agent
agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)