This project demonstrates a simple two-step conversational agent built using LangChain and LangGraph. It separates the reasoning process into two distinct stages:
- Reasoning Stage: Generates an intermediate "thought" by prompting the LLM to think step by step about the user input.
- Action Stage: Constructs a concrete action plan based on the original input and the generated "thought".
A state machine (StateGraph) orchestrates these stages in sequence and produces both the reasoning trace and final plan.
- Modular agent design with clear separation of reasoning and planning.
- Uses Pydantic models (
AgentState) to define and validate agent state. - Easy-to-extend workflow graph via
StateGraph. - Visualization of the workflow as a Mermaid diagram.
- Python 3.8+
langchainlanggraphpydanticopenai(for theChatOpenAIintegration)
Install dependencies via pip:
pip install langchain langgraph pydantic openai-
Define the Agent State
AgentStatecapturesinput_text,thought, andoutput_action.
-
Implement Nodes
reasoning_agent: Prompts the LLM to think step by step about the input.action_agent: Builds an action plan using both input and intermediate thought.
-
Build the Workflow
- Instantiate
StateGraph, add nodes, set entry point, and define transitions. - Compile the graph to obtain an executable workflow.
- Instantiate
-
Invoke the Workflow
- Create an initial
AgentStatewith the user request. - Call
graph.invoke(initial_state)to execute reasoning and planning.
- Create an initial
-
Visualize the Graph
- Generate a Mermaid PNG diagram via
graph.get_graph(xray=1).draw_mermaid_png().
- Generate a Mermaid PNG diagram via
Example:
# Prepare initial state
initial_state = AgentState(
input_text="Build a 3-month plan to study the basics of English language",
thought="",
output_action=""
)
# Run the workflow
result = graph.invoke(initial_state)
print(result.thought)
print(result.output_action)
# Save workflow diagram
png = graph.get_graph(xray=1).draw_mermaid_png()
open("workflow.png", "wb").write(png)flowchart TD
A[User Input] --> B[Reasoning Agent]
B --> C[Action Agent]
C --> D[Final Plan]
- StateGraph manages transitions between nodes.
- ChatOpenAI provides the LLM backend for both stages.
- Add more nodes for validation, execution, or feedback loops.
- Customize prompts or integrate additional tools.
- Adjust state schema to capture more context as needed.
This project is released under the MIT License.
