π 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)