LangChain Python Tutorial: Build Intelligent Agents with LLMs


LangChain Python Tutorial


Introduction

As generative AI applications become more complex, developers are searching for tools to simplify LLM integration and orchestration. The LangChain API provides a flexible, open-source framework to do just that—especially with Python.

In this LangChain Python tutorial, you’ll learn how to build intelligent applications and agents powered by LLMs like OpenAI’s GPT-4. We'll cover step-by-step instructions to set up LangChain, build your first chain, integrate memory, and even connect vector stores like Pinecone.


What is LangChain?

LangChain is an orchestration framework that allows developers to:

  • Connect with multiple LLMs (OpenAI, Cohere, Hugging Face, etc.)

  • Build multi-step reasoning pipelines with chains

  • Create autonomous agents that interact with tools

  • Maintain context using memory modules

  • Retrieve documents using semantic search from vector stores

Python is LangChain’s most mature and feature-rich implementation—making it ideal for production-ready LLM apps.




Getting Started with LangChain in Python

Step 1: Install LangChain

bash
pip install langchain langchain-openai

If you plan to use vector databases or additional integrations:

bash
pip install langchain[all]

Step 2: Configure LangChain with OpenAI

You’ll need an OpenAI API key. Set it in your environment:

bash
export OPENAI_API_KEY="your-api-key"

In Python:

python
from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-4")

This completes your basic LangChain OpenAI integration.


Step 3: Create Your First LLM Chain

Use a prompt template to generate structured outputs.

python
from langchain.prompts import PromptTemplate from langchain.chains import LLMChain prompt = PromptTemplate.from_template("Summarize this: {text}") chain = LLMChain(llm=llm, prompt=prompt) result = chain.run("LangChain makes it easier to build apps with LLMs.") print(result)

Step 4: Add Memory to Your App

LangChain supports memory for conversational and multi-turn use cases.

python
from langchain.memory import ConversationBufferMemory from langchain.chains import ConversationChain memory = ConversationBufferMemory() chat_chain = ConversationChain(llm=llm, memory=memory) chat_chain.run("Hi, I’m Alex.") chat_chain.run("What’s my name?")

Use the LangChain memory module to maintain context and user history.


Build AI Agents with LangChain

Agents in LangChain allow LLMs to take actions using tools like search, math functions, or APIs.

python
from langchain.agents import load_tools, initialize_agent tools = load_tools(["serpapi", "llm-math"], llm=llm) agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) response = agent.run("What is the weather in Paris and square root of 144?") print(response)

With agents, you can create LangChain autonomous agents that plan, act, and reason over multiple steps.


LangChain Vector Store Integration: Pinecone

For knowledge retrieval and RAG apps, integrate with a vector database like Pinecone.

python
from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import PineconeVectorStore embeddings = OpenAIEmbeddings() vectorstore = PineconeVectorStore.from_existing_index("your-index", embeddings)

Now your app can fetch relevant content and summarize it—an essential RAG technique.


Setting Up API Keys

LangChain typically requires API keys for LLM providers such as OpenAI, Hugging Face, or others.


Option 1: Set as Environment Variable (Recommended)

bash
export OPENAI_API_KEY="your_openai_key"

Option 2: Pass Directly in Python Code

python
from langchain.llms import OpenAI llm = OpenAI(openai_api_key="your_openai_key")

Key Components of LangChain


Prompt Templates

Prompt templates allow you to define reusable, parameterized prompts for your LLMs.

python
from langchain import PromptTemplate template = """Question: {question} Let's think step by step. Answer: """ prompt = PromptTemplate(template=template, input_variables=["question"]) formatted_prompt = prompt.format( question="Can Barack Obama have a conversation with George Washington?" ) print(formatted_prompt)

Language Models (LLMs)

LangChain supports a variety of LLM providers.


Using OpenAI:

python
from langchain.llms import OpenAI llm = OpenAI(model_name="text-davinci-003", openai_api_key="your_openai_key") response = llm("Tell me a joke about data scientist") print(response)

Using Hugging Face:

python
from langchain import HuggingFaceHub llm = HuggingFaceHub( repo_id="google/flan-t5-xl", huggingfacehub_api_token="your_hf_token" ) print(llm("Tell me a joke about data scientist"))

Chains

Chains allow you to combine prompts and LLMs into reusable, multi-step workflows.


Simple Chain with One LLM

python
from langchain.chains import LLMChain llm_chain = LLMChain(prompt=prompt, llm=llm) question = "Can Barack Obama have a conversation with George Washington?" print(llm_chain.run(question))

Sequential Chain with Multiple Prompts

python
from langchain.chains import SimpleSequentialChain # First prompt: get the most popular city in a country first_prompt = PromptTemplate( input_variables=["country"], template="What is the most popular city in {country} for tourists? Just return the name of the city." ) chain_one = LLMChain(llm=llm, prompt=first_prompt) # Second prompt: get top things to do in the city second_prompt = PromptTemplate( input_variables=["city"], template="What are the top three things to do in {city} for tourists? Just return three bullet points." ) chain_two = LLMChain(llm=llm, prompt=second_prompt) # Combine chains overall_chain = SimpleSequentialChain(chains=[chain_one, chain_two], verbose=True) final_answer = overall_chain.run("Canada") print(final_answer)

LangChain Chatbot Development

Combine LLM chains, memory, and vector search to build fully functional chatbots:

  • ConversationChain for contextual dialogue

  • RetrievalQA for knowledge-base questions

  • ConversationalRetrievalChain for hybrid chat + RAG

Use these tools to build AI customer support bots, sales assistants, and more.




LangChain for Enterprise Use

LangChain supports:

  • OpenAI, Cohere, Google Vertex AI

  • Document loaders (PDF, Notion, Markdown)

  • Tools and plugins (Google Search, Python REPL, APIs)

  • Cloud integration (AWS, GCP)

  • Deployment with LangServe

  • Observability via LangSmith

  • Orchestration via LangGraph

LangChain for enterprise AI is reliable, scalable, and model-agnostic.




Test, Monitor, and Evaluate

Use tools like:

  • LangSmith: Monitor agent decisions, trace errors

  • Callbacks: Log data at each stage

  • LangGraph: Manage multi-agent or multi-chain workflows


Full Python Tutorial Recap

Component Purpose
Installation pip install langchain langchain-openai
LLM Setup ChatOpenAI(model="gpt-4")
Chain Prompt → LLM → Response
PromptTemplate Template-based prompt formatting
Memory ConversationBufferMemory for context retention
Agent Multi-tool reasoning capability
Vector Store Retrieval with Pinecone-powered embeddings

The official LangChain tutorials cover all these as well as advanced topics like RAG, agents, tool chaining, migrations, and memory management.


Learn by Example

For hands-on learning:

  • SitePoint's guide on LangChain in Python details agent, model, chunk, and chain components.

  • DataCamp’s tutorial walks through LLM app development with LangChain.

  • Try interactive YouTube crash courses, such as “LangChain Tutorial in Python”


Community Learning

The LangChain subreddit emphasizes the importance of practice-based learning:

“Just set a goal and code something using it… Keep a tab open with the documentation.”


Next Steps

After mastering the basics, scale up:

  1. Use Retrieval QA chains over PDFs or knowledge bases.

  2. Migrate memory flows to LangGraph for long-term context.

  3. Monitor production flows using LangSmith.

  4. Deploy via LangServe.

  5. Integrate more tools and implement custom agen


LangChain vs CrewAI

Feature LangChain CrewAI
Use Case General LLM apps Task-driven agent teams
Agent Logic Dynamic and modular Pre-configured team roles
Developer Control High Medium
Best For Custom workflows Team-based agent logic

LangChain gives developers flexibility; CrewAI offers a no-code-like agent team experience.


Tips for Learning LangChain


Conclusion

This LangChain Python tutorial has shown you how to:

  • Set up LangChain with OpenAI

  • Build prompt chains and agents

  • Add memory and vector search

  • Scale apps for real-world use

Whether you're building simple chatbots or complex autonomous systems, LangChain in Python provides the structure, flexibility, and power needed to succeed in the world of LLMs.