๐ create_agent Using LangChain and OpenRouter in Python
Artificial Intelligence doesnโt have to be complicated. In this short tutorial, Iโll show you how to build a simple create_agent using Python, LangChain, and OpenRouter in just a few steps. This is perfect for beginners who want to understand how AI APIs work in real projects.
๐ GitHub Repository: https://github.com/vinodnextcoder/langchain-tutorials
๐ง What Weโre Building
Weโll create a small Python script that:
- Connects to an AI model using OpenRouter
- Uses LangChain to manage the conversation
- Asks a simple question
- Prints the AIโs response in the terminal
Example question:
โWhat is artificial intelligence in simple terms?โ
๐ Project Structure
langchain_python/
โโโ python_example/
โโโ createagent.py
โโโ .env
โโโ README.md
โ Prerequisites
Before starting, make sure you have:
- Python 3.9+
- An OpenRouter API key
- Basic knowledge of running Python scripts
๐ฆ Step 1: Install Required Packages
Go to the python_example folder and run:
pip install langchain langchain-openai python-dotenv
๐ Step 2: Add Your API Key Safely
Create a file named .env inside python_example and add:
OPENROUTER_API_KEY=your_api_key_here
This keeps your API key secure and out of your source code.
๐งโ๐ป Step 3: The Python Script (createagent.py)
Your script does the following:
- Loads the API key from
.env - Connects to OpenRouter using an OpenAI-compatible setup
- Creates a simple LangChain agent
- Sends a user question
- Prints the AIโs response safely
The key idea is simple:
You send a message โ the AI processes it โ you print the response.
๐งโ๐ป Step 4: The Python code
import os
from langchain.agents import create_agent
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
# Load environment variables
load_dotenv()
# Get API key
OPENROUTER_KEY = os.getenv("OPENROUTER_API_KEY")
if not OPENROUTER_KEY:
raise ValueError("Missing OPENROUTER_API_KEY in environment")
# Set OpenAI-compatible environment variables for OpenRouter
os.environ["OPENAI_API_KEY"] = OPENROUTER_KEY
os.environ["OPENAI_BASE_URL"] = "https://openrouter.ai/api/v1"
# Create LLM
llm = ChatOpenAI(
model="mistralai/mistral-7b-instruct:free",
temperature=0.7,
api_key=OPENROUTER_KEY,
)
# Create agent
agent = create_agent(
llm,
tools=[],
system_prompt="You are a helpful assistant. Answer in simple words."
)
# User input
user_question = "What is artificial intelligence in simple terms?"
# Invoke agent
response = agent.invoke({
"messages": [
{"role": "user", "content": user_question}
]
})
# Extract and print AI response safely
if "messages" in response and len(response["messages"]) > 0:
ai_message = response["messages"][-1]
print(ai_message.content)
else:
print("No response received from the agent.")
โถ๏ธ Step 5: Run the Project
From the python_example folder, run:
python createagent.py
๐งพ Sample Output
Artificial intelligence is when computers are taught to think and learn like humans to perform tasks such as answering questions and making decisions.
๐ก What You Learn From This Project
With this small example, you learn:
- How to use environment variables in Python
- How to connect Python to an AI model
- How LangChain agents work at a basic level
- How to safely extract and print an AI response
โ Final Thoughts
This project is a great starting point for anyone entering AI development with Python. With just a few steps, youโve built a working AI-powered chatbot using modern tools like LangChain and OpenRouter.
If youโre learning AI integration, this is the perfect first milestone. โ
Top comments (0)