This repository is for onboarding SWE interns to LangChain by building a simple AI agent. Each engineer will learn how to use LangChain’s core components — models, tools, and agents — to create a runnable prototype demonstrating structured reasoning and output generation.
git clone https://github.com/saanviibrahim45/langchain-practice.git
cd langchain-practicepython3 -m venv .venv
source .venv/bin/activate # macOS/Linux
.venv\Scripts\activate # Windowspip install -r requirements.txtCheck that all key libraries are installed and importable:
pip show langchain langgraph langchain-openai python-dotenvThen confirm imports work and your environment is set up correctly:
python - <<'PY'
try:
import langgraph, langchain, langchain_openai, dotenv
print("Imports OK")
except Exception as e:
print("Import error:", e)
PYYou should see:
Imports OK
Next, verify your OpenAI key is loaded:
python - <<'PY'
import os
print("OPENAI_API_KEY present:", bool(os.environ.get("OPENAI_API_KEY")))
PYYou should see:
OPENAI_API_KEY present: True
Confirm that your OpenAI model can respond:
python - <<'PY'
from langchain_openai import ChatOpenAI
m = ChatOpenAI(model="gpt-4o-mini", temperature=0)
resp = m.invoke([{"role":"user","content":"Say hello in one short sentence."}])
print(resp.content)
PYExpected output:
Hello there! How can I help you today?
From the project root, run:
python src/base_agent.pyYou should see:
LangGraph base agent ready. Type 'exit' to quit.
Use 'calc: 2+2' to try the calculator tool.
Example Run
You: calc: 5+5
AI: The result is 10.
You: What did I just calculate?
AI: You just calculated 5 plus 5, which equals 10.Each SWE must:
- Work in their own branch (named after them) and add a new file in
src/to build their agent. - Implement one working LangChain agent with structured output (JSON or clearly formatted text).
- Write a short
README.mdin their folder explaining what their agent does and how it works. - Ensure their
.envfile is configured so their code runs locally end-to-end.
When finished, submit a pull request or share your branch for review.
- Keep your API keys private — never commit
.envfiles. - If you add new dependencies, update
requirements.txt. - Write clean, modular, and well-commented code.
- Test your agent manually using interactive input.
git clone https://github.com/saanviibrahim45/langchain-practice.git
cd langchain-practice
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
export OPENAI_API_KEY="your_key_here"
python src/base_agent.pyThe included src/base_agent.py demonstrates how to build a minimal LangGraph-powered agent using LangChain’s OpenAI integration. It models an LLM-driven reasoning flow using modular nodes that communicate through a shared state.
1. StateGraph and MessagesState
StateGraphdefines a directed workflow of agent steps.- Each node processes and returns a
MessagesState(a structured list ofHumanMessage,AIMessage, andSystemMessageobjects). - This enables memory and context passing across nodes.
2. Nodes and Edges
- Nodes are Python functions that take in state and return updates.
- Edges define the flow of execution (
START → tool_node → llm_node → END).
| Node | Purpose |
|---|---|
tool_node |
Detects calculator commands like calc: 2+2 and appends tool output |
llm_node |
Reads recent messages and produces an AI response via OpenAI |
3. Tools
-
A “tool” is a callable function the agent can invoke when needed.
-
The calculator function (
calculator_tool) is a simple example — future tools can include:- Web search (e.g. Tavily, Serper)
- Data analysis
- File summarization
4. Execution Loop
- The interactive loop lets you type messages directly into the terminal.
- Each message is wrapped as a
HumanMessageand passed through the compiled graph. - The agent then decides how to respond based on the state.
Example Run
You: calc: 10*(5+2)
AI: The result is 70.
You: What did I just calculate?
AI: You just calculated 10 times (5 plus 2), which equals 70.