A small but complete AI agent built with LangGraph, Flask, OpenAI, SerpAPI, and a safe calculator tool.
The app routes each user message to the right capability: normal chat, web search, translation, or calculation. It includes both a command-line interface and a modern chat-style web UI with Server-Sent Events for a faster response experience.
This project demonstrates practical AI engineering skills:
- building an agent workflow with LangGraph
- routing user intent with an LLM
- connecting external tools such as search and calculator functions
- keeping tool code separate from graph orchestration
- building a usable Flask frontend for the agent
- handling environment variables safely
- passing recent chat history back into the model
- AI intent classification
- normal chat support
- real Google search results through SerpAPI
- safe math calculator using Python
ast - English translation route
- Flask chat interface
- Server-Sent Events for responsive message updates
- browser-session chat history
- command-line mode for quick testing
- clean folder structure for learning and teaching
User message
|
v
Flask UI or CLI
|
v
LangGraph state
|
v
intent_detection node
|
v
conditional routing
|
+-- chat --------> chat_node --------> response
+-- search ------> search_node ------> SerpAPI + LLM summary
+-- calculator --> calculator_node --> safe math result
+-- translator --> translator_node --> English translation
+-- fallback ----> fallback_node ----> general LLM answer
.
|-- agent.py # CLI entry point
|-- app.py # Flask web app
|-- langgraph_agent/
| |-- graph.py # LangGraph nodes and edges
| |-- intent.py # LLM intent classifier
| |-- llm.py # OpenAI setup
| |-- nodes.py # Agent node functions
| `-- state.py # Shared graph state
|-- tools/
| |-- calculator.py # Safe calculator tool
| `-- search_tool.py # SerpAPI search tool
|-- templates/
| `-- index.html # Tailwind chat UI
|-- static/
| `-- styles.css # Small custom CSS
|-- .env.example # Environment template
`-- requirements.txt
- Python
- LangGraph
- LangChain OpenAI
- OpenAI API
- SerpAPI
- Flask
- Tailwind CSS
- Server-Sent Events
Create and activate a virtual environment:
python -m venv .venv
.\.venv\Scripts\Activate.ps1Install dependencies:
pip install -r requirements.txtCreate your environment file:
Copy-Item .env.example .envFill in .env:
OPENAI_API_KEY=your_openai_key
OPENAI_MODEL=gpt-4.1-mini
SERPAPI_API_KEY=your_serpapi_key
FLASK_SECRET_KEY=change-this-for-productionpython app.pyOpen:
http://127.0.0.1:5000
python agent.py "25 * 12"
python agent.py "Who won the World Cup in 2022?"
python agent.py "Bonjour comment allez-vous"
python agent.py "Tell me a simple joke about Python"Interactive mode:
python agent.py- Ask a normal question:
Explain LangGraph in simple words.
- Search the web:
What are the latest OpenAI models?
- Calculate:
sqrt(144) + 6
- Translate:
Bonjour comment allez-vous
Intent detection always uses the OpenAI model, so OPENAI_API_KEY is required to route every query.
Search uses SerpAPI, so search questions also require SERPAPI_API_KEY.
The web app stores recent chat history in memory for the current running server. For production, this could be replaced with Redis, SQLite, Postgres, or another persistent store.
Do not commit your .env file. This project includes .gitignore rules to keep local keys private.
If an API key is ever shared publicly, rotate it immediately from the provider dashboard.
Recommended:
langgraph-multi-tool-agent
Other strong options:
langgraph-ai-agent
ai-agent-tool-router
flask-langgraph-agent
multi-tool-ai-assistant
serpapi-langgraph-agent
This project shows how to build a real AI agent that can classify user intent, call external tools, use web search, remember recent conversation, and provide a polished chat interface.