As Large Language Models (LLMs) become more powerful, developers increasingly need structured ways to build, manage, and monitor AI-driven workflows. Simple prompt calls are no longer sufficient for real-world applications that require memory, branching logic, tool usage, and observability. LangChain, LangGraph, and LangSmith are part of the same ecosystem designed to solve these challenges. Together, they help developers build reliable, testable, and production-ready AI applications. Let us explore how to use LangChain and LangGraph as a beginner’s guide to AI workflows.
1. Introduction to LangChain, LangGraph, and LangSmith
Modern AI applications rarely consist of a single prompt and response. Real-world systems often require multiple steps, external data sources, decision-…
As Large Language Models (LLMs) become more powerful, developers increasingly need structured ways to build, manage, and monitor AI-driven workflows. Simple prompt calls are no longer sufficient for real-world applications that require memory, branching logic, tool usage, and observability. LangChain, LangGraph, and LangSmith are part of the same ecosystem designed to solve these challenges. Together, they help developers build reliable, testable, and production-ready AI applications. Let us explore how to use LangChain and LangGraph as a beginner’s guide to AI workflows.
1. Introduction to LangChain, LangGraph, and LangSmith
Modern AI applications rarely consist of a single prompt and response. Real-world systems often require multiple steps, external data sources, decision-making logic, and continuous monitoring. The LangChain ecosystem addresses these needs through three complementary components: LangChain for building LLM-powered logic, LangGraph for orchestrating complex workflows, and LangSmith for observing, debugging, and evaluating those workflows in production.
1.1 LangChain
LangChain is a developer framework designed to simplify the process of building applications powered by large language models (LLMs). Instead of working directly with raw prompt strings and model APIs, LangChain provides higher-level abstractions that help structure LLM interactions in a modular and reusable way. At its core, LangChain offers components such as:
- Prompt Templates – Reusable, parameterized prompts that ensure consistent formatting.
- LLM Wrappers – Unified interfaces for models from OpenAI, Anthropic, Google, and others.
- Chains – Sequences of steps where the output of one component feeds into the next.
- Agents – Dynamic systems that can decide which tools or actions to use at runtime.
- Memory – Mechanisms for maintaining conversational or application state.
LangChain is commonly used when you need to combine LLMs with external systems such as databases, APIs, vector stores, or file systems. Typical use cases include:
- Chatbots and conversational AI with memory
- Document-based question answering systems
- Tool-using AI agents that call APIs or execute functions
- Retrieval-Augmented Generation (RAG) pipelines
1.2 LangGraph
LangGraph builds on top of LangChain by introducing a graph-based approach to workflow orchestration. While LangChain chains are linear by nature, LangGraph allows you to define workflows as directed graphs, where each node represents a computation step and edges define how data and control flow between those steps. In a LangGraph workflow:
- Nodes can represent LLM calls, tool invocations, or custom Python logic.
- Edges define transitions between nodes, including conditional and branching paths.
- State is explicitly managed and passed through the graph, enabling persistence.
This model is particularly useful for building more sophisticated AI systems that require iteration, decision-making, or parallel execution. Common scenarios include:
- Multi-step reasoning and planning workflows
- Stateful conversations that evolve over time
- Conditional logic such as retries, fallbacks, or approvals
- Human-in-the-loop systems where users can review or modify intermediate outputs
1.3 LangSmith
LangSmith is an observability, debugging, and evaluation platform designed specifically for LLM-powered applications built with LangChain and LangGraph. As AI workflows grow in complexity, understanding why a model produced a particular output becomes increasingly difficult. LangSmith addresses this challenge by providing deep visibility into every step of the workflow. With LangSmith, developers can:
- Trace individual LLM calls, prompts, and responses across chains and graphs
- Debug unexpected behavior by inspecting intermediate inputs and outputs
- Evaluate model performance using datasets and automated metrics
- Monitor production systems for latency, errors, and output quality
In practice, LangSmith acts as the feedback and monitoring layer of the LangChain ecosystem. It enables teams to iterate faster during development and maintain confidence when deploying AI workflows to production environments.
1.4 Key Differences
| Component | Purpose | Best For | Key Capabilities | Typical Users |
|---|---|---|---|---|
| LangChain | Building modular, LLM-powered application logic | Linear workflows, agents, and RAG pipelines | Prompt templates, chains, agents, tools, memory, model abstractions | Application developers building chatbots, QA systems, and AI assistants |
| LangGraph | Orchestrating AI workflows using a graph-based model | Complex, stateful, and branching workflows | Nodes and edges, conditional routing, persistent state, human-in-the-loop support | Teams building advanced reasoning systems and multi-step AI processes |
| LangSmith | Observability, tracing, and evaluation of LLM applications | Debugging, testing, and monitoring AI systems | End-to-end tracing, dataset-based evaluation, performance monitoring | Developers and ML engineers operating AI workflows in production |
2. Code Example: LangChain + LangGraph + LangSmith Workflow
2.1 Dependencies
pip install langchain langgraph langsmith openai
2.2 LangSmith Configuration
LangSmith works by automatically tracing LangChain and LangGraph calls when environment variables are configured.
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=your_langsmith_api_key
export LANGCHAIN_PROJECT=langgraph-demo
Once enabled, every LLM call, node execution, and state transition is captured in the LangSmith dashboard.
2.3 Code Example
The following example demonstrates a complete LangChain, LangGraph, and LangSmith workflow that classifies a user query and routes it through a graph-based AI pipeline.
# Filename: langgraph_demo.py
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
from langchain.memory import ConversationBufferMemory
from langgraph.graph import StateGraph
from langgraph.checkpoint.sqlite import SqliteSaver
# Initialize LLM (automatically traced by LangSmith)
llm = ChatOpenAI(model="gpt-4", temperature=0)
# Conversational memory (in-memory)
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
# Persistent state store (SQLite)
checkpointer = SqliteSaver.from_conn_string("langgraph_state.db")
# Shared state across the graph
class GraphState(dict):
pass
# Node 1: Classify the question
def classify_question(state: GraphState):
question = state["question"]
response = llm([
HumanMessage(
content=f"Classify this question as 'tech' or 'general': {question}"
)
])
state["category"] = response.content.lower()
return state
# Node 2: Technical answer with memory
def tech_response(state: GraphState):
history = memory.load_memory_variables({}).get("chat_history", [])
response = llm(
history + [
HumanMessage(
content=f"Provide a technical explanation for: {state['question']}"
)
]
)
memory.save_context(
{"input": state["question"]},
{"output": response.content}
)
state["answer"] = response.content
return state
# Node 3: General answer with memory
def general_response(state: GraphState):
history = memory.load_memory_variables({}).get("chat_history", [])
response = llm(
history + [
HumanMessage(
content=f"Provide a simple general answer for: {state['question']}"
)
]
)
memory.save_context(
{"input": state["question"]},
{"output": response.content}
)
state["answer"] = response.content
return state
# Build LangGraph workflow
graph = StateGraph(GraphState)
graph.add_node("classify", classify_question)
graph.add_node("tech", tech_response)
graph.add_node("general", general_response)
graph.set_entry_point("classify")
graph.add_conditional_edges(
"classify",
lambda state: "tech" if "tech" in state["category"] else "general"
)
graph.set_finish_point("tech")
graph.set_finish_point("general")
# Compile graph with persistence enabled
app = graph.compile(checkpointer=checkpointer)
# Invoke workflow with a persistent session ID
result = app.invoke(
{"question": "What is dependency injection in Spring?"},
config={"configurable": {"thread_id": "user-123"}}
)
print(result["answer"])
2.3.1 Code Explanation
This code builds a conversational question-answering workflow using LangChain and LangGraph, where an OpenAI chat model (ChatOpenAI) is used to classify a user’s question and then answer it appropriately while maintaining memory and persistence. It initializes an LLM with deterministic output, sets up in-memory conversational context using ConversationBufferMemory, and enables durable state storage via a SQLite-based checkpointer from LangGraph. A shared GraphState dictionary holds data such as the question, category, and answer as it flows through the graph. The workflow consists of three nodes: one to classify the question as technical or general, one to generate a technical response using prior conversation history, and one to generate a simpler general response, both of which also save interactions back to memory. These nodes are wired together in a LangGraph state machine that conditionally routes execution based on the classification result. Finally, the graph is compiled with persistence enabled and invoked with a question and a persistent thread ID, ensuring that conversation context is preserved across runs, and the computed answer is printed as output.
2.3.2 Code Run and Output
To run this code, first ensure Python 3.9 or later is installed, then install the required dependencies. Next, create a LangSmith account and set the required environment variables along with your OpenAI API key in your system environment. Save the code into a Python file (for example, langgraph_demo.py), open a terminal in the same directory, and execute it using python langgraph_demo.py. When the script runs, the workflow will automatically start at the classification node, route the question through the appropriate branch, print the final answer to the console, and simultaneously generate a detailed execution trace in the LangSmith dashboard for inspection and debugging.
Trace: langgraph-demo
--------------------------------
Node: classify
Category: tech
Node: tech
Answer:
Dependency Injection in Spring is a design pattern where object dependencies
are provided by the Spring container rather than created by the objects
themselves. This promotes loose coupling, easier testing, and better
separation of concerns.
The code output and LangSmith trace together illustrate both the functional result of the AI workflow and the observability benefits provided by LangSmith. When the workflow is invoked with the question “What is dependency injection in Spring?”, the LangGraph execution begins at the classify node, where the language model correctly categorizes the question as tech; this classification decision is visible in the trace and determines the subsequent execution path. Based on this result, the graph conditionally routes execution to the tech node, which generates a technically detailed explanation of dependency injection in Spring and stores it in the shared graph state as the final answer. The printed output shown in the console reflects this final state, displaying a concise yet accurate technical definition emphasizing externalized dependency management, loose coupling, and improved testability. Simultaneously, LangSmith captures an end-to-end execution trace under the project name langgraph-demo, recording each node execution, the prompts sent to the LLM, the model’s responses, and the transitions between nodes. This trace allows developers to visually inspect how the question flowed through the graph, verify that the correct conditional branch was taken, analyze latency and token usage, and debug or refine prompts if needed, making the workflow not only functional but also transparent, debuggable, and production-ready.
If the workflow is invoked again with the same thread_id:
app.invoke(
{"question": "Can you give a simple example?"},
config={"configurable": {"thread_id": "user-123"}}
)
The following output will be returned:
Sure. For example, in Spring, you might have a Service class that depends on a Repository. Instead of creating the Repository inside the Service, Spring injects it for you.
Because we discussed dependency injection earlier, this example builds on that idea: the Service focuses only on business logic, while Spring manages object creation and wiring, making the code easier to test and maintain.
3. Conclusion
LangChain, LangGraph, and LangSmith together form a powerful toolkit for building modern AI applications. LangChain simplifies LLM integration, LangGraph enables complex and stateful workflows, and LangSmith provides critical observability for debugging and evaluation.
For beginners, starting with LangChain and gradually introducing LangGraph for advanced workflows is an ideal learning path. As applications grow in complexity, these tools help ensure your AI systems remain reliable, scalable, and production-ready.