Unlocking the Power of Generative AI Application Development

If you’ve tried building an AI-powered application, you probably realized that the promise of “just a few lines of code” often leads to an explosion of complexity as you add features. Multiple large language models (LLMs) like GPT-4, Gemini, Claude, and others are now available, each excelling at different tasks. But what if you want to use several LLMs in your app for specialized processing, advanced multi-step reasoning, or to coordinate AI “agents” that call diverse tools? Doing all of this manually can eat up time, inflate your code, and multiply headaches.
So, what's the solution?
Do we really have to wire up each LLM, agent, memory component, and tool by hand every time and repeat that for each new use case? Certainly not! The answer is: LangChain.
What is LangChain?
LangChain is an open-source framework and now, a robust ecosystem that acts as an abstraction layer for building powerful AI and language model applications. Instead of hand-coding every prompt, model switch, context-management trick, or multi-step workflow, LangChain gives you a toolbox and a recipe book for assembling these components with minimal code.
LangChain enables developers to:
Integrate with multiple LLMs through a standard interface
Manage complex prompting techniques
Build sophisticated chains of actions and queries
Maintain context and memory across user sessions or chat histories
Orchestrate multiple agents that interact with different tools or APIs
Index, search, and retrieve large document collections efficiently
It keeps you focused on building the logic and user experience of your app, not reinventing the wheel with each new feature.
Why Does LangChain Matter?
Imagine you want to:
Route user queries to the best-fit LLM automatically
Chain calls between tools and models (e.g., calling a search API, then summarizing results via LLM)
Use agents that solve problems by choosing actions step-by-step, consulting plugins or external tools
Keep track of user history, preferences, and context smoothly
Without LangChain, you’d write tons of glue code configuring each model, tracking every chat message, juggling APIs and data structures, and trying to maintain the context window, all while risking bugs and burnout.
LangChain abstracts away this complexity. For example, you can specify your API key and model name once, then use a concise function call to query any supported LLM. Need more advanced chains, prompt templates, or memory features? They're built in.
Key Components of LangChain
Let’s break down some of the core building blocks that LangChain provides. While each of these components can be explored in great depth enough to fill entire chapters or even books. I’ll focus on distilling the most important ones. These essential blocks will guide us throughout our journey in building powerful generative AI applications.
1. Models
Standardized interfaces for connecting with any LLM (like OpenAI, Anthropic, Google, and more). This includes embedding models to turn text into vector representations for search or retrieval.
Gemini Example:
.env:
OPENAI_API_KEY=<Your OpenAI Key>
GOOGLE_API_KEY=<Your Gemini Key>
Packages:
pip install -qU "langchain[google-genai]"
pip install -qU "langchain[openai]"
main.py
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
load_dotenv()
model = init_chat_model("gemini-2.0-flash",model_provider="google_genai")
SYSTEM_PROMPT="""
This is just a testing system prompt.
"""
def main():
messages=[]
system_query = {"role":"system","content":SYSTEM_PROMPT}
user_query = {"role":"user","content":"Hey, How are you ?"}
messages.append(system_query)
messages.append(user_query)
llm_response = model.invoke(messages)
print("LLM Response: ",llm_response.content)
main()
Output:
python main.py
LLM Response: I'm doing well, thank you for asking! How are you today?
To use an OpenAI model, you just need to update the model configuration details.
model = init_chat_model("gpt-4o-mini",model_provider="openai")
2. Prompts
Tools for managing and customizing prompt templates so you can pattern prompts for different tasks, users, and use cases.
If you want to learn more about prompts and prompting, here’s a detailed post covering what prompting is, its importance, and the different types of prompting:
🔗 Read the full post on LinkedIn
Here are some examples of how LangChain helps us create and manage prompts.
from langchain.prompts import PromptTemplate
# Create a simple prompt template
prompt_template = PromptTemplate(
input_variables=["product", "audience"],
template="""
Write a marketing description for {product} targeting {audience}.
Product: {product}
Target Audience: {audience}
Description:
"""
)
# Use the template
formatted_prompt = prompt_template.format(
product="wireless headphones",
audience="fitness enthusiasts"
)
print(formatted_prompt)
python simple_prompt.py
Write a marketing description for wireless headphones targeting fitness enthusiasts.
Product: wireless headphones
Target Audience: fitness enthusiasts
Description:
conversational_prompt.py
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
# Create system and human message templates
system_template = SystemMessagePromptTemplate.from_template(
"You are a helpful {role} assistant. Your expertise is in {domain}. "
"Always provide practical, actionable advice."
)
human_template = HumanMessagePromptTemplate.from_template(
"I need help with: {user_question}"
)
# Combine into a chat prompt
chat_prompt = ChatPromptTemplate.from_messages([
system_template,
human_template
])
# Format the prompt
messages = chat_prompt.format_prompt(
role="financial",
domain="personal budgeting and investment",
user_question="How should I start investing with $1000?"
).to_messages()
for message in messages:
print(f"{message.type}: {message.content}")
python conversational_prompt.py
system: You are a helpful financial assistant. Your expertise is in personal budgeting and investment. Always provide practical, actionable advice.
human: I need help with: How should I start investing with $1000?
few_shot_prompt.py
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
# Define examples for few-shot learning
examples = [
{
"input": "I'm feeling overwhelmed with work",
"output": "It sounds like you're experiencing work stress. Consider breaking tasks into smaller chunks, prioritizing urgent items, and taking short breaks."
},
{
"input": "I can't sleep at night",
"output": "Sleep issues can be challenging. Try establishing a bedtime routine, avoiding screens before bed, and creating a comfortable sleep environment."
}
]
# Create the example template
example_template = PromptTemplate(
input_variables=["input", "output"],
template="Human: {input}\nAI: {output}"
)
# Create the few-shot prompt template
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_template,
prefix="You are a supportive counselor. Here are some examples of how to respond:",
suffix="Human: {input}\nAI:",
input_variables=["input"]
)
# Use the template
prompt = few_shot_prompt.format(input="I'm having trouble making friends")
print(prompt)
python few_shot_prompt.py
You are a supportive counselor. Here are some examples of how to respond:
Human: I'm feeling overwhelmed with work
AI: It sounds like you're experiencing work stress. Consider breaking tasks into smaller chunks, prioritizing urgent items, and taking short breaks.
Human: I can't sleep at night
AI: Sleep issues can be challenging. Try establishing a bedtime routine, avoiding screens before bed, and creating a comfortable sleep environment.
Human: I'm having trouble making friends
AI:
3. Chains
The core concept: build “pipelines” where each step processes input/output chaining LLMs and tools together to solve complex tasks in sequence or in parallel.
chain_example.py
from langchain.chains import LLMChain, SimpleSequentialChain
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from dotenv import load_dotenv
load_dotenv()
# Initialize LLM
llm = OpenAI(temperature=0.7)
# Chain 1: Generate blog topic ideas
topic_prompt = PromptTemplate(
input_variables=["industry"],
template="Generate 3 engaging blog topic ideas for the {industry} industry:"
)
topic_chain = LLMChain(llm=llm, prompt=topic_prompt)
# Chain 2: Write blog outline
outline_prompt = PromptTemplate(
input_variables=["topic"],
template="Create a detailed blog post outline for this topic: {topic}"
)
outline_chain = LLMChain(llm=llm, prompt=outline_prompt)
# Chain 3: Write introduction
intro_prompt = PromptTemplate(
input_variables=["outline"],
template="Write an engaging introduction based on this outline: {outline}"
)
intro_chain = LLMChain(llm=llm, prompt=intro_prompt)
# Combine chains sequentially
content_pipeline = SimpleSequentialChain(
chains=[topic_chain, outline_chain, intro_chain],
verbose=True
)
# Execute the pipeline
result = content_pipeline.invoke("Generative AI")
print("Final Result:", result)
python chain_example.py
>Entering new SimpleSequentialChain chain...
1. "The Future of Creativity: How Generative AI is Revolutionizing the Creative Process"
2. "From Art to Music: Exploring the Boundless Possibilities of Generative AI"
3. "Unleashing the Power of Generative AI: How Businesses Can Utilize AI to Innovate and Drive Growth"
I. Introduction
A. Explanation of generative AI
B. Brief overview of the topic
C. Thesis statement: Generative AI is transforming the creative process and reshaping the future of creativity in various industries.
II. The Basics of Generative AI
A. Definition and explanation of generative AI
B. How it differs from traditional AI
..................
............................
In a world where technology is constantly advancing, it's no surprise that Artificial Intelligence (AI) has become a hot topic. But have you heard of generative AI? This cutting-edge technology is revolutionizing the creative process and reshaping the future of creativity in various industries.
From art to music to fashion, generative AI is pushing the boundaries of what is possible.
So, what exactly is generative AI and how is it impacting these fields? Let's dive in and explore the endless possibilities of this incredible technology.
First, let's start with the basics.
Generative AI is a branch of AI that focuses on creating original content, rather than simply analyzing data. Unlike traditional AI, which is programmed to perform specific tasks, generative AI has the ability to generate new content based on patterns and data it has been trained on.
.......
...................................
4. Memory
Crucial for chatbots or anything conversational. LangChain offers multiple memory strategies:
ConversationBufferMemory: Retains the entire chat history.
ConversationBufferWindowMemory: Keeps only the most recent N messages.
ConversationSummaryMemory: Stores a summary of previous messages.
CustomMemory: Lets you track facts, user preferences, or other data tailored to your application.
Here is an example of ConversationBufferMemory: conversation_buffer_memory.py
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
from langchain.llms import OpenAI
from dotenv import load_dotenv
load_dotenv()
# Initialize memory and LLM
memory = ConversationBufferMemory()
llm = OpenAI(temperature=0.7)
# Create conversation chain with memory
conversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True
)
# Simulate a conversation
print("=== Customer Support Chat ===")
# First interaction
response1 = conversation.predict(input="Hi, I'm having trouble with my order #12345")
print(f"AI: {response1}")
# Second interaction - AI remembers the order number
response2 = conversation.predict(input="The delivery was supposed to be yesterday")
print(f"AI: {response2}")
# Third interaction - AI has full context
response3 = conversation.predict(input="Can you check the status for me?")
print(f"AI: {response3}")
# View the complete memory
print("\n=== Full Conversation History ===")
print(memory.buffer)
python conversation_buffer_memory.py
Current conversation:
Human: Hi, I'm having trouble with my order #12345
AI: Hello there! I am an AI assistant and I'm here to help. Can you please provide me with more information about your order #12345? What seems to be the issue?
Human: The delivery was supposed to be yesterday
AI: I see. According to my records, your order #12345 was indeed scheduled for delivery yesterday. However, there seems to have been a delay in the shipping process. Would you like me to check on the status of your delivery and provide you with an estimated time of arrival?
Human: Can you check the status for me?
AI:
> Finished chain.
AI: Of course! Give me just a moment to access the shipping company's database. It looks like your package is currently in transit and is expected to arrive at your doorstep within the next 2 hours. Is there anything else I can assist you with?
=== Full Conversation History ===
Human: Hi, I'm having trouble with my order #12345
AI: Hello there! I am an AI assistant and I'm here to help. Can you please provide me with more information about your order #12345? What seems to be the issue?
Human: The delivery was supposed to be yesterday
AI: I see. According to my records, your order #12345 was indeed scheduled for delivery yesterday. However, there seems to have been a delay in the shipping process. Would you like me to check on the status of your delivery and provide you with an estimated time of arrival?
Human: Can you check the status for me?
AI: Of course! Give me just a moment to access the shipping company's database. It looks like your package is currently in transit and is expected to arrive at your doorstep within the next 2 hours. Is there anything else I can assist you with?
This is legacy code and an earlier approach to save memory, but later many transformations happened like checkpointers and saving memory with LangGraph (I will add a blog on LangGraph soon). If you are still interested in understanding the code of LangGraph, then here is the example below:
langgraph_memory.py
import uuid
from langchain_core.messages import HumanMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph,END
from langchain.chat_models import init_chat_model
from dotenv import load_dotenv
load_dotenv()
# Define a chat model
model = init_chat_model(model_provider="openai", model="gpt-4.1")
# Define the function that calls the model
def call_model(state: MessagesState):
response = model.invoke(state["messages"])
# We return a list, because this will get added to the existing list
return {"messages": response}
# Define a new graph
graph = StateGraph(state_schema=MessagesState)
# Define the two nodes we will cycle between
graph.add_node("call_model", call_model)
graph.add_edge(START, "call_model")
graph.add_edge("call_model",END)
# Adding memory is straight forward in langgraph!
memory = MemorySaver()
app = graph.compile(
checkpointer=memory
)
# The thread id is a unique key that identifies
# this particular conversation.
# We'll just generate a random uuid here.
# This enables a single application to manage conversations among multiple users.
thread_id = uuid.uuid4()
config = {"configurable": {"thread_id": thread_id}}
input_message = HumanMessage(content="hi! I'm Sharad")
for event in app.stream({"messages": [input_message]}, config, stream_mode="values"):
event["messages"][-1].pretty_print()
# Here, let's confirm that the AI remembers our name!
input_message = HumanMessage(content="hey,what was my name?")
for event in app.stream({"messages": [input_message]}, config, stream_mode="values"):
event["messages"][-1].pretty_print()
python langgraph_memory.py
================================ Human Message =================================
hi! I'm Sharad
================================== Ai Message ==================================
Hello, Sharad! 👋 Nice to meet you. How can I help you today?
================================ Human Message =================================
hey,what was my name?
================================== Ai Message ==================================
You said your name is Sharad! How can I assist you today?
5. Indexes (Retrievers)
Powerful modules to:
Load and chunk documents
Embed and store them in vector databases
Efficiently retrieve relevant information for LLMs to act on
To understand Indexes, you can read the article below, which will explain how the RAG pipeline works and how these modules load and chunk documents, store them in a vector database (Qdrant DB), and efficiently retrieve them for later use with LLMs.
6. Agents & Tools
Through agent frameworks like LangGraph, you can create modular “agent” workflows: the AI can make decisions, run external tools (APIs, search engines, code execution, calculators, etc.), and chain outputs intelligently.
Coming soon: a detailed blog on LangGraph, where I’ll explain how LangGraph manages workflows, how you can integrate your own tools (functions) into these workflows, and how to efficiently retrieve outputs.
Stay tuned! More insights and hands-on examples with LangGraph are on the way.
7. Debugging & Tracing: LangSmith
LangChain also offers LangSmith, a tracing tool that tracks your application's events step-by-step making it much easier to debug, iterate, and understand your chains and agent behavior.
pip install -U langsmith openevals openai
Steps to Get Started with LangSmith
Visit: https://smith.langchain.com/
Sign up and generate your LangSmith API key.
Create a new LangSmith project.
Navigate into your project dashboard to start managing traces and debugging your LangChain apps.
.env
OPENAI_API_KEY=<your-openai-key>
LANGSMITH_TRACING=true
LANGSMITH_ENDPOINT="https://api.smith.langchain.com"
LANGSMITH_API_KEY=<your-langsmith-api-key>
LANGSMITH_PROJECT=<your-langsmith-project-name>
Here are the traces and debugging output of my langgraph_memory.py
Subscribe to my newsletter
Read articles from Sharad Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
