| title | Langgraph |
|---|---|
| description | Build a basic chatbot with LangGraph and AgentOps tracking |
{/* SOURCE_FILE: examples/langgraph/langgraph_example.ipynb */}
_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/langgraph/langgraph_example.ipynb'} target={'blank'}>Github
This example shows you how to build a basic chatbot using LangGraph's StateGraph with comprehensive tracking via AgentOps.
A stateful chatbot using LangGraph fundamentals:
- 🗃️ StateGraph: Core LangGraph structure for managing conversation state
- 💬 Chat Model: LLM integration for generating responses
- 🔄 State Management: Automatic message history tracking with
add_messages - 🎯 Graph Flow: START → chatbot node → END pattern
With AgentOps, you'll get complete visibility into graph execution, state transitions, and LLM interactions.
Install LangGraph with your preferred chat model and AgentOps for tracking:
```bash pip pip install langgraph langchain agentops python-dotenv ``` ```bash poetry poetry add langgraph langchain agentops python-dotenv ``` ```bash uv uv pip install langgraph langchain agentops python-dotenv ```What AgentOps adds:
- 📊 Graph execution tracking with node transitions and timing
- 💰 LLM cost monitoring with token usage breakdown
- 🔄 State change visualization showing message flow
- 📈 Performance metrics for each graph execution
- 🐛 Execution replay for debugging graph flows
Create a simple Python project for your chatbot:
# Create project directory
mkdir langgraph_chatbot
cd langgraph_chatbot
# Create main chatbot file
touch chatbot.py
touch .envThis creates the basic structure:
langgraph_chatbot/
├── chatbot.py # Main chatbot implementation
└── .env # API keys
Create your .env file with the necessary API keys:
# .env
OPENAI_API_KEY=your_openai_api_key_here
AGENTOPS_API_KEY=your_agentops_api_key_hereGet your API keys:
- OpenAI API Key: OpenAI Platform
- AgentOps API Key: AgentOps Settings
Note: You can use any LangChain-compatible model (Anthropic, Google, etc.) by adjusting the imports and model initialization.
Edit chatbot.py to create your LangGraph chatbot:
# chatbot.py
import os
from typing import Annotated
from typing_extensions import TypedDict
from langchain.chat_models import init_chat_model
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
import agentops
from dotenv import load_dotenv
# Load environment variables and initialize AgentOps
load_dotenv()
agentops.init(auto_start_session=False)
# Define the State schema
class State(TypedDict):
# Messages have the type "list". The `add_messages` function
# in the annotation defines how this state key should be updated
# (in this case, it appends messages to the list, rather than overwriting them)
messages: Annotated[list, add_messages]Add the model and node function:
# Initialize the chat model (you can change to any provider)
llm = init_chat_model("openai:gpt-4o-mini")
# Create the chatbot node function
def chatbot(state: State):
"""Main chatbot function that processes messages and returns responses."""
return {"messages": [llm.invoke(state["messages"])]}Construct your LangGraph workflow:
# Create the StateGraph
graph_builder = StateGraph(State)
# Add the chatbot node
# The first argument is the unique node name
# The second argument is the function that will be called
graph_builder.add_node("chatbot", chatbot)
# Add entry point (where to start)
graph_builder.add_edge(START, "chatbot")
# Add exit point (where to end)
graph_builder.add_edge("chatbot", END)
# Compile the graph
graph = graph_builder.compile()Create the interactive chat interface with AgentOps tracking:
def stream_graph_updates(user_input: str):
"""Stream graph updates for the given user input."""
for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}):
for value in event.values():
print("Assistant:", value["messages"][-1].content)
def run_chatbot():
"""Main function to run the chatbot with AgentOps tracking."""
# Start AgentOps trace for this chat session
with agentops.start_trace(trace_name="LangGraph Chatbot", tags=["langgraph", "chatbot"]):
print("🤖 LangGraph Chatbot Started!")
print("Type 'quit', 'exit', or 'q' to stop.\n")
while True:
try:
user_input = input("User: ")
if user_input.lower() in ["quit", "exit", "q"]:
print("Goodbye!")
break
stream_graph_updates(user_input)
print() # Add blank line for readability
except KeyboardInterrupt:
print("\nGoodbye!")
break
except Exception as e:
print(f"Error: {e}")
break
# Main execution
if __name__ == "__main__":
run_chatbot()Execute your chatbot:
cd langgraph_chatbot
python chatbot.pyWhat happens:
- AgentOps trace starts
- Interactive chat loop begins
- Each user message flows through: START → chatbot node → END
- LLM generates responses based on conversation history
- AgentOps captures all state transitions and LLM interactions
- Trace ends when you type 'quit'
Example conversation:
🤖 LangGraph Chatbot Started!
Type 'quit', 'exit', or 'q' to stop.
User: Hello! What can you help me with?
Assistant: Hello! I'm a helpful AI assistant. I can help you with a wide variety of tasks...
User: Tell me a joke
Assistant: Why don't scientists trust atoms? Because they make up everything!
User: quit
Goodbye!
After running your chatbot, visit your AgentOps Dashboard to see:
- Graph Structure: Visual representation of your StateGraph (START → chatbot → END)
- State Transitions: How messages flow through the graph
- LLM Interactions: Every conversation turn with prompts and responses
- Execution Timing: How long each node takes to process
- Session Analytics: Conversation length, token usage, and costs
- Message History: Complete conversation flow with state management
Project structure you built:
chatbot.py- Complete LangGraph chatbot with AgentOps integration.env- API keys for OpenAI and AgentOps
AgentOps Integration Points:
agentops.init()- Enables automatic LangGraph instrumentationagentops.start_trace()- Wraps each chat session in a trace
- Add tools to your chatbot (web search, calculators, etc.)
- Implement more complex graph structures with conditional edges
- Add memory persistence across sessions
- Create multi-agent workflows with LangGraph
- Use AgentOps analytics to optimize conversation flows