<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: vinod</title>
    <description>The latest articles on DEV Community by vinod (@vinodnextcoder1).</description>
    <link>https://dev.to/vinodnextcoder1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3632671%2F47d3870e-2551-41c5-8c05-7af154107ac8.png</url>
      <title>DEV Community: vinod</title>
      <link>https://dev.to/vinodnextcoder1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vinodnextcoder1"/>
    <language>en</language>
    <item>
      <title>LangChain and OpenRouter in Python</title>
      <dc:creator>vinod</dc:creator>
      <pubDate>Thu, 27 Nov 2025 12:03:44 +0000</pubDate>
      <link>https://dev.to/vinodnextcoder1/langchain-and-openrouter-in-python-2b9f</link>
      <guid>https://dev.to/vinodnextcoder1/langchain-and-openrouter-in-python-2b9f</guid>
      <description>&lt;p&gt;&lt;strong&gt;🚀 create_agent Using LangChain and OpenRouter in Python&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Artificial Intelligence doesn’t have to be complicated. In this short tutorial, I’ll show you how to build a &lt;strong&gt;simple create_agent using Python, LangChain, and OpenRouter&lt;/strong&gt; in just a few steps. This is perfect for beginners who want to understand how AI APIs work in real projects.&lt;/p&gt;




&lt;p&gt;👉 &lt;strong&gt;GitHub Repository:&lt;/strong&gt; &lt;a href="https://github.com/vinodnextcoder/langchain-tutorials" rel="noopener noreferrer"&gt;https://github.com/vinodnextcoder/langchain-tutorials&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 What We’re Building
&lt;/h2&gt;

&lt;p&gt;We’ll create a small Python script that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connects to an AI model using &lt;strong&gt;OpenRouter&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Uses &lt;strong&gt;LangChain&lt;/strong&gt; to manage the conversation&lt;/li&gt;
&lt;li&gt;Asks a simple question&lt;/li&gt;
&lt;li&gt;Prints the AI’s response in the terminal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“What is artificial intelligence in simple terms?”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  📁 Project Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;langchain_python/
└── python_example/
    ├── createagent.py
    ├── .env
    └── README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✅ Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before starting, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Python 3.9+&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;OpenRouter API key&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Basic knowledge of running Python scripts&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📦 Step 1: Install Required Packages
&lt;/h2&gt;

&lt;p&gt;Go to the &lt;code&gt;python_example&lt;/code&gt; folder and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;langchain langchain-openai python-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔐 Step 2: Add Your API Key Safely
&lt;/h2&gt;

&lt;p&gt;Create a file named &lt;strong&gt;&lt;code&gt;.env&lt;/code&gt;&lt;/strong&gt; inside &lt;code&gt;python_example&lt;/code&gt; and add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OPENROUTER_API_KEY=your_api_key_here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This keeps your API key secure and out of your source code.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧑‍💻 Step 3: The Python Script (&lt;code&gt;createagent.py&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;Your script does the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loads the API key from &lt;code&gt;.env&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Connects to OpenRouter using an OpenAI-compatible setup&lt;/li&gt;
&lt;li&gt;Creates a simple LangChain agent&lt;/li&gt;
&lt;li&gt;Sends a user question&lt;/li&gt;
&lt;li&gt;Prints the AI’s response safely&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key idea is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;You send a message → the AI processes it → you print the response.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧑‍💻 Step 4: The Python code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;import os
from langchain.agents import create_agent
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

&lt;span class="c"&gt;# Load environment variables&lt;/span&gt;
load_dotenv&lt;span class="o"&gt;()&lt;/span&gt;

&lt;span class="c"&gt;# Get API key&lt;/span&gt;
OPENROUTER_KEY &lt;span class="o"&gt;=&lt;/span&gt; os.getenv&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"OPENROUTER_API_KEY"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;not OPENROUTER_KEY:
    raise ValueError&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Missing OPENROUTER_API_KEY in environment"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Set OpenAI-compatible environment variables for OpenRouter&lt;/span&gt;
os.environ[&lt;span class="s2"&gt;"OPENAI_API_KEY"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; OPENROUTER_KEY
os.environ[&lt;span class="s2"&gt;"OPENAI_BASE_URL"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"https://openrouter.ai/api/v1"&lt;/span&gt;

&lt;span class="c"&gt;# Create LLM&lt;/span&gt;
llm &lt;span class="o"&gt;=&lt;/span&gt; ChatOpenAI&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"mistralai/mistral-7b-instruct:free"&lt;/span&gt;,
    &lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.7,
    &lt;span class="nv"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;OPENROUTER_KEY,
&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Create agent&lt;/span&gt;
agent &lt;span class="o"&gt;=&lt;/span&gt; create_agent&lt;span class="o"&gt;(&lt;/span&gt;
    llm,
    &lt;span class="nv"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=[]&lt;/span&gt;,
    &lt;span class="nv"&gt;system_prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"You are a helpful assistant. Answer in simple words."&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# User input&lt;/span&gt;
user_question &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"What is artificial intelligence in simple terms?"&lt;/span&gt;

&lt;span class="c"&gt;# Invoke agent&lt;/span&gt;
response &lt;span class="o"&gt;=&lt;/span&gt; agent.invoke&lt;span class="o"&gt;({&lt;/span&gt;
    &lt;span class="s2"&gt;"messages"&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;
        &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;: &lt;span class="s2"&gt;"user"&lt;/span&gt;, &lt;span class="s2"&gt;"content"&lt;/span&gt;: user_question&lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;

&lt;span class="c"&gt;# Extract and print AI response safely&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"messages"&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;response and len&lt;span class="o"&gt;(&lt;/span&gt;response[&lt;span class="s2"&gt;"messages"&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 0:
    ai_message &lt;span class="o"&gt;=&lt;/span&gt; response[&lt;span class="s2"&gt;"messages"&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="nt"&gt;-1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    print&lt;span class="o"&gt;(&lt;/span&gt;ai_message.content&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;:
    print&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"No response received from the agent."&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ▶️ Step 5: Run the Project
&lt;/h2&gt;

&lt;p&gt;From the &lt;code&gt;python_example&lt;/code&gt; folder, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python createagent.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧾 Sample Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Artificial intelligence is when computers are taught to think and learn like humans to perform tasks such as answering questions and making decisions.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  💡 What You Learn From This Project
&lt;/h2&gt;

&lt;p&gt;With this small example, you learn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to use &lt;strong&gt;environment variables&lt;/strong&gt; in Python&lt;/li&gt;
&lt;li&gt;How to connect Python to an &lt;strong&gt;AI model&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;How &lt;strong&gt;LangChain agents&lt;/strong&gt; work at a basic level&lt;/li&gt;
&lt;li&gt;How to safely extract and print an AI response&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This project is a &lt;strong&gt;great starting point for anyone entering AI development with Python&lt;/strong&gt;. With just a few steps, you’ve built a working AI-powered chatbot using modern tools like &lt;strong&gt;LangChain and OpenRouter&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you’re learning AI integration, this is the perfect first milestone. ✅&lt;/p&gt;




</description>
      <category>python</category>
      <category>ai</category>
      <category>langchain</category>
      <category>openai</category>
    </item>
  </channel>
</rss>
