AgentFlow is a multi-utility AI chatbot built using LangGraph and Streamlit, designed around modular and stateful agent workflows.
The system combines Retrieval-Augmented Generation (RAG), dynamic tool calling, Model Context Protocol (MCP) integration, Human-in-the-Loop (HITL) capabilities, persistent conversation state, and observability into a single AI assistant.
The chatbot uses an open-source Large Language Model hosted through the Hugging Face Inference API. The LLM is accessed remotely using a Hugging Face API token rather than being executed locally.
For document retrieval, the project uses an open-source Hugging Face sentence-transformer embedding model together with FAISS for vector similarity search.
The chatbot is implemented using LangGraph's StateGraph.
The agent separates the reasoning and execution process into different components:
- LLM reasoning
- Tool selection
- Tool execution
- Conditional routing
- State management
- Human approval
- Persistent checkpointing
The main workflow is:
START
β
chat_node
β
tools_condition
ββββ tools β chat_node
β
ββββ END
This allows the agent to iteratively reason, invoke tools when required, process tool results, and generate a final response.
AgentFlow supports Human-in-the-Loop workflows using LangGraph's interrupt and resume capabilities.
For actions that require human approval, the workflow can pause before execution.
The user can then:
- Review the proposed action
- Approve the action
- Reject the action
- Modify the input before continuing
The workflow can then resume from the point where it was interrupted.
The general flow is:
LLM
β
Proposed Action
β
HITL Interrupt
β
Human Review
βββ Approve β Continue
βββ Reject β Stop / Modify
This provides better control over potentially sensitive or important agent actions.
AgentFlow allows users to upload PDF documents and ask questions about their contents.
The RAG pipeline is:
PDF
β
PyPDFLoader
β
Text Chunking
β
Hugging Face Embeddings
β
FAISS
β
Similarity Search
β
Relevant Context
β
Hugging Face LLM
β
Final Answer
- The user uploads a PDF.
PyPDFLoaderextracts the document text.RecursiveCharacterTextSplitterdivides the text into smaller chunks.- Each chunk is converted into an embedding using a Hugging Face sentence-transformer model.
- The embeddings are stored in a FAISS vector index.
- When the user asks a question, the query is converted into an embedding.
- FAISS performs similarity search.
- The most relevant chunks are retrieved.
- The retrieved context is provided to the LLM.
- The LLM generates a grounded response.
Example embedding model:
sentence-transformers/all-MiniLM-L6-v2
Each conversation thread can maintain its own document retriever.
For example:
Thread A
βββ research_paper.pdf
βββ FAISS Retriever
Thread B
βββ company_report.pdf
βββ FAISS Retriever
This helps prevent documents uploaded in one conversation from being used unintentionally in another conversation.
Each thread is identified using a unique thread_id.
AgentFlow supports multiple tools that the LLM can select dynamically based on the user's request.
Example tools include:
- Calculator
- DuckDuckGo web search
- Stock price retrieval
- PDF RAG retrieval
- Custom MCP tools
Instead of manually implementing routing logic such as:
if user_wants_calculation:
call_calculator()
elif user_wants_stock:
call_stock_api()
elif user_wants_document:
call_rag()the LLM receives the available tool definitions and determines when a tool is required.
LangGraph then routes the tool call to the appropriate execution node.
For example:
User
β
"What is 125 Γ 42?"
β
LLM
β
Calculator Tool
β
5250
β
LLM
β
Final Answer
AgentFlow integrates Model Context Protocol (MCP) to provide a standardized interface for connecting the agent with external tools.
The project contains both an MCP client and a custom MCP server.
The general architecture is:
LangGraph Agent
β
MCP Client
β
MCP Server
β
Custom / External Tools
The MCP client can discover available tools exposed by an MCP server and make them available to the agent.
This makes the architecture easier to extend because additional tools can be added through MCP without tightly coupling their implementation to the core agent.
AgentFlow uses an open-source instruction-tuned LLM from Hugging Face.
The model is not executed locally.
Instead, the application accesses the model through the Hugging Face Inference API using a Hugging Face API token.
Example model:
meta-llama/Llama-3.1-8B-Instruct
Other compatible open-source instruction-tuned models can also be used depending on Hugging Face provider availability and tool-calling support.
The architecture is:
User
β
Streamlit
β
LangGraph
β
LangChain Hugging Face Integration
β
Hugging Face Inference API
β
Open-Source LLM
β
Response
This allows the project to use an open-source LLM without requiring the model weights to be downloaded and executed on the local machine.
For document retrieval, AgentFlow uses an open-source sentence-transformer model from Hugging Face.
Example:
sentence-transformers/all-MiniLM-L6-v2
The embedding model converts text into numerical vectors.
For example:
"Machine learning is a subset of AI"
β
Embedding Model
β
[0.12, -0.31, 0.82, ...]
These vectors are stored and searched using FAISS.
Note: The LLM is accessed remotely through the Hugging Face Inference API. The embedding model is used within the application environment for document vectorization.
AgentFlow uses SQLite checkpointing through LangGraph's SqliteSaver.
Each conversation is associated with a unique thread_id.
The state flow is:
User
β
thread_id
β
LangGraph
β
Agent State
β
SqliteSaver
β
SQLite Database
This allows conversations to be:
- Paused
- Resumed
- Revisited
- Continued across sessions
The SQLite database stores LangGraph checkpoints so that previous conversation state can be restored.
The database file is:
chatbot.db
AgentFlow integrates LangSmith for tracing and observability.
LangSmith can be used to inspect:
- LLM calls
- Tool calls
- LangGraph execution
- Execution latency
- Errors
- Agent traces
- Retrieval behavior
- Intermediate execution steps
A typical execution can be visualized as:
User Request
β
LangGraph
β
LLM Call
β
Tool Call
β
Tool Result
β
LLM Call
β
Final Response
This helps identify problems such as:
- Incorrect tool selection
- Poor retrieval
- Unexpected model responses
- Tool failures
- Excessive LLM calls
- Slow execution
LangSmith can also be used to evaluate and improve agent behavior over time.
βββββββββββββββββββββββ
β Streamlit Frontend β
β β
β Chat UI β
β PDF Upload β
β Thread Management β
β HITL Interaction β
ββββββββββββ¬βββββββββββ
β
βΌ
βββββββββββββββββββββββ
β LangGraph Agent β
β β
β StateGraph β
β Chat Node β
β Tool Node β
β Conditional β
β Routing β
β HITL β
ββββββββββββ¬βββββββββββ
β
ββββββββββββββββββββΌβββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β RAG β β Tools β β MCP β
β β β β β β
β PDF β β Calculator β β MCP Client β
β Embeddings β β Web Search β β MCP Server β
β FAISS β β Stock API β β Custom Toolsβ
ββββββββ¬βββββββ βββββββββββββββ βββββββββββββββ
β
βΌ
βββββββββββββββ
β Hugging Faceβ
β Embeddings β
βββββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β Hugging Face β
β Inference API β
β β
β Open-Source LLM β
βββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β SQLite β
β Checkpointing β
βββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β LangSmith β
β Tracing & Evaluationβ
βββββββββββββββββββββββ
The core LangGraph workflow is:
START
β
βΌ
chat_node
β
βΌ
tools_condition
β
βββββββββββββββ No tool required βββββββββββββββ END
β
βββββββββββββββ Tool required
β
βΌ
tools
β
βΌ
chat_node
β
βΌ
tools_condition
β
...repeat...
The LLM receives:
- System instructions
- Conversation history
- Current user request
- Available tool definitions
It determines whether it can answer directly or needs additional information from a tool.
LangGraph checks whether the LLM response contains a tool call.
If a tool call exists:
chat_node β tools
Otherwise:
chat_node β END
The selected tool is executed and the result is added back to the graph state.
The LLM then receives the tool result and can decide whether another tool is required or whether it can generate the final answer.
Suppose the user uploads a research paper and asks:
"What methodology is used in this paper?"
The workflow is:
User
β
Streamlit
β
LangGraph
β
Hugging Face LLM
β
LLM selects RAG tool
β
RAG Tool
β
FAISS Similarity Search
β
Top-K Relevant Chunks
β
Context
β
Hugging Face LLM
β
Final Answer
This allows the LLM to generate an answer using information retrieved from the uploaded document instead of relying only on its pretrained knowledge.
| Tool | Purpose |
|---|---|
| Calculator | Performs basic arithmetic operations |
| DuckDuckGo Search | Searches the web for relevant information |
| Stock Price | Retrieves stock information through an external API |
| RAG Tool | Retrieves relevant information from uploaded PDFs |
| MCP Tools | Provides additional custom/external capabilities |
AgentFlow/
β
βββ README.md
βββ agent.py
βββ app.py
βββ chatbot.db
βββ config.py
βββ llm.py
βββ mcp_client.py
βββ mcp_server.py
βββ rag.py
βββ requirements.txt
βββ state.py
βββ tools.py
| File | Responsibility |
|---|---|
app.py |
Streamlit frontend and user interaction |
agent.py |
LangGraph agent and workflow |
state.py |
Defines the agent state |
llm.py |
Hugging Face LLM configuration |
rag.py |
PDF processing, embeddings, FAISS indexing and retrieval |
tools.py |
Pre-built and custom agent tools |
mcp_client.py |
MCP client and dynamic MCP tool integration |
mcp_server.py |
Custom MCP server and tool definitions |
config.py |
Application configuration and environment variables |
requirements.txt |
Python dependencies |
chatbot.db |
SQLite checkpoint database generated at runtime |
README.md |
Project documentation |
- Python 3.10+
- Hugging Face account
- Hugging Face API token
- LangSmith account/API key
- API key for any external service used by the tools
- Internet connection for Hugging Face Inference API and web/API tools
Important: You must provide your own API keys/tokens. The repository does not provide API credentials.
Create a .env file in the project root.
Example:
HUGGINGFACEHUB_API_TOKEN=your_huggingface_token
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=your_langsmith_api_key
LANGCHAIN_PROJECT=AgentFlow
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_api_keyReplace the placeholder values with your own credentials.
Required for accessing the Hugging Face-hosted LLM through the Inference API.
HUGGINGFACEHUB_API_TOKEN=your_huggingface_tokenRequired if you want to enable LangSmith tracing and observability.
LANGCHAIN_API_KEY=your_langsmith_api_keyTools such as stock-price retrieval may require an external API key.
For example:
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_api_keygit clone https://github.com/Pheonix-1002/AgentFlow.git
cd AgentFlowpython -m venv venv
venv\Scripts\activatepython3 -m venv venv
source venv/bin/activatepip install -r requirements.txtCreate a .env file:
HUGGINGFACEHUB_API_TOKEN=your_huggingface_token
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=your_langsmith_api_key
LANGCHAIN_PROJECT=AgentFlow
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_api_keyUse your own keys for all services.
streamlit run app.pyStreamlit will provide a local URL, usually:
http://localhost:8501
Ask general questions through the Streamlit chat interface.
The agent determines whether it can answer directly or needs a tool.
Upload a PDF through the application.
The document goes through:
PDF
β
Text Extraction
β
Chunking
β
Embeddings
β
FAISS
You can then ask questions about the uploaded document.
Example:
Calculate 1234 * 567
The agent can select the calculator tool.
Another example:
Search the web for the latest developments in generative AI.
The agent can select the web-search tool.
For documents:
According to my uploaded PDF, what is the proposed methodology?
The agent can use the RAG tool.
For actions configured for HITL, the graph pauses and waits for human approval before continuing.
Previous conversation threads can be selected and resumed.
SQLite checkpointing restores the saved LangGraph state.
With LangSmith enabled, you can inspect:
LLM Calls
Tool Calls
Graph Execution
Latency
Errors
Retrieval
Agent Traces
A complex request may require multiple steps.
For example:
User
β
"Find information about a company and calculate
its percentage change."
β
LangGraph
β
Hugging Face LLM
β
Web / Stock Tool
β
Tool Result
β
Calculator
β
Tool Result
β
Hugging Face LLM
β
Final Response
This demonstrates the iterative nature of the LangGraph agent.
The LLM can select a tool, receive its output, reason over the result, and request another tool if necessary.
AgentFlow is designed around several principles.
Different capabilities such as RAG, search, calculation, and MCP tools are separated into reusable modules.
LangGraph maintains the state of the conversation and workflow.
The LLM determines when a tool is required instead of relying entirely on manually written routing logic.
RAG provides relevant document context to improve answers over user-provided knowledge sources.
SQLite checkpointing allows conversations to be resumed later.
MCP provides a standardized mechanism for connecting additional tools and services.
HITL allows humans to review and approve selected actions.
LangSmith provides visibility into LLM calls, tools, graph execution and agent behavior.
Potential improvements include:
- Persistent FAISS indexes
- Multiple document support per thread
- Hybrid keyword + vector retrieval
- Reranking retrieved chunks
- Metadata filtering
- Streaming LLM responses
- Additional MCP servers
- More advanced HITL workflows
- Automated RAG evaluation
- Retrieval quality metrics
- Tool-call accuracy evaluation
- Authentication and multi-user support
- Production deployment
- Better error handling and retry mechanisms
- Conversation summarization for long-running threads
Pheonix-1002