Building Your First AI App: LangChain Beginner to Intermediate Guide
You’ve heard the buzz. Everyone’s building AI apps. And somewhere in every tutorial, someone drops the word “LangChain” like it explains…
Building Your First AI App: LangChain Beginner to Intermediate Guide
You’ve heard the buzz. Everyone’s building AI apps. And somewhere in every tutorial, someone drops the word “LangChain” like it explains itself. It doesn’t. Let’s fix that.
Why LangChain?
Let’s be honest. Calling an LLM directly using the OpenAI API is straightforward enough. You pass a string, you get a string back. Simple.
But the moment you want something real — an app that remembers context, pulls from your own documents, or follows a multi-step reasoning process, you’re suddenly writing a lot of glue code. You’re managing prompt formatting, handling conversation history, parsing outputs, and stitching everything together manually.
LangChain exists to solve exactly that problem. At its core, LangChain is a Python and JavaScript framework that helps you build applications powered by large language models. It gives you reusable building blocks for prompts, memory, chains, agents, and tool integrations — so you can focus on what your app actually does, rather than how to wire everything together.

AI generated image
Think of it like Express.js for backend development. You could write a raw Node HTTP server from scratch. But why would you?
Setting Up: Get the Boring Part Done First
Before writing a single line of logic, let’s get the environment ready.
pip install langchain-groq langchain-core
Create a .env file in your project root:
OPENAI_API_KEY=your_api_key_here
Then load it at the top of your Python file:
from dotenv import load_dotenv
load_dotenv()
That’s it. Now let’s build something.
Understanding the Core Concepts
1. The LLM Wrapper: Your Connection to the Model
LangChain doesn’t replace your LLM. It wraps it. The first thing you’ll do in almost every LangChain project is initialize a model connection.
from langchain_groq import ChatGroq
llm = ChatGroq(
model="llama-3.1-8b-instant",
temperature=0.7
)
'''
The temperature parameter controls creativity.
Lower values (like 0.2) make the model more focused and deterministic.
Higher values (like 0.9) make it more creative and unpredictable.
For most apps, something between 0.5 and 0.8 hits the sweet spot.
'''
You can now call this directly:
response = llm.invoke("What is Docker in one sentence?")
print(response.content)
But this is just the beginning. Hardcoding a prompt string like this is fine for testing. In real apps, you need something far more flexible.
2. Prompt Templates : Stop Hardcoding Your Prompts
Here’s a scenario. You’re building a tool that explains any tech concept to a beginner. The concept changes every time a user asks. Do you write a new prompt for each one? Of course not. This is where Prompt Templates come in.
A Prompt Template is essentially a reusable prompt with placeholders. You define the structure once, and fill in the variables at runtime.
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful teacher who explains tech concepts simply."),
("human", "Explain {concept} to a complete beginner in under 100 words.")
])
# Now you can generate a formatted prompt for any concept:
formatted = prompt.format_messages(concept="Docker")
print(formatted)
This gives you a clean list of messages that you can pass directly to your LLM.
3. Chains: Where LangChain Gets Its Name
A Chain is a sequence of steps that are connected together. The output of one step becomes the input of the next.
The modern way to build chains in LangChain uses the LCEL (LangChain Expression Language) i.e a simple pipe | syntax that makes chains readable and composable.
chain = prompt | llm | parser
That’s it. That one line creates a full pipeline: your template gets formatted → gets sent to the LLM → returns a response.
Invoke it like this:
response = chain.invoke({"concept": "Docker"})
print(response.content)
You can extend this chain further. Want to parse the output into plain text automatically?
from langchain_core.output_parsers import StrOutputParser
chain = prompt | llm | StrOutputParser()
response = chain.invoke({"concept": "Kubernetes"})
print(response) # Plain string, no .content needed
This is the power of LCEL. Every component prompt, model, parser follows the same interface, so they snap together cleanly.
Let’s Build Something Real: A Tech Concept Explainer CLI
Enough theory. Let’s put this all together into a small but genuinely useful project, a command-line tool that explains any tech concept in simple terms, styled for beginners.
Here’s the complete code:
# tech_explainer.py
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
load_dotenv()
# 1. Initialize the model
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
# 2. Define the prompt template
prompt = ChatPromptTemplate.from_messages([
(
"system",
"""You are a senior developer who loves teaching.
When explaining concepts, you:
- Use simple, everyday analogies
- Avoid jargon unless you explain it immediately
- Give one short real-world example
- Keep it under 150 words"""
),
("human", "Explain '{concept}' to someone who just started learning programming.")
])
# 3. Build the chain
chain = prompt | llm | StrOutputParser()
# 4. Run it
def explain(concept: str) -> str:
return chain.invoke({"concept": concept})
if __name__ == "__main__":
import sys
topic = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else "APIs"
print(f"\n Explaining: {topic}\n")
print(explain(topic))
print()
Run it from your terminal:
python tech_explainer.py Docker
python tech_explainer.py "REST APIs"
python tech_explainer.py Kubernetes
Sample output for Docker:
Explaining: Docker
Think of Docker like a lunchbox. Instead of cooking at school (setting up
your environment on every computer), you pack everything you need at home -
food, utensils, napkins - into one sealed box. Docker does the same for
software: it bundles your app, its dependencies, and settings into a
"container" that runs the same way on any machine.
Real-world example: A developer builds an app on their MacBook. With Docker,
their colleague on Windows runs the exact same app without changing a thing.
Clean, useful, and powered by a proper LangChain chain under the hood.
Taking It Further: What’s Next?
You now understand the three building blocks that power 80% of real LangChain apps:
- LLMs: the model connection
- Prompt Templates: dynamic, reusable prompts
- Chains: composable pipelines that connect everything
But LangChain goes much deeper. Here’s what comes next as you level up:
Memory: Give your app the ability to remember past messages. Essential for any chatbot or assistant that needs context across turns.
Retrievers & RAG: Connect your chain to your own documents. Ask questions against a PDF, a codebase, or a database using Retrieval-Augmented Generation.
Agents: Instead of a fixed chain, let the LLM decide which tools to use and in what order. This is where things get genuinely powerful (and complex).
LangGraph: When your workflow needs conditional logic, loops, or parallel steps, LangGraph gives you graph-based control over your AI flows. If LangChain is a pipeline, LangGraph is a flowchart.
The Honest Truth About LangChain
LangChain isn’t magic, and it’s not without criticism. Some developers feel it adds unnecessary abstraction, especially for simple use cases. They’re not entirely wrong.
If you’re just making a single API call, you don’t need LangChain. But the moment your app needs composition, reusability, and scale that’s where the framework earns its place.
Start small. Build the explainer tool above. Swap “llama-3.1–8b-instant” for another model and notice nothing in your chain breaks. Add a second prompt template and chain it in with one more |. That's the experience LangChain is designed to give you.
The best way to understand a framework isn’t to read about it. It’s to feel it click — that moment when adding a new step to your pipeline takes three seconds instead of thirty minutes.
You’re closer to that moment than you think.
메타데이터
- post_id
- 52660495dbbb
- slug
- building-your-first-ai-app-langchain-beginner-to-intermediate-guide-52660495dbbb
- url
- https://towardsdev.com/building-your-first-ai-app-langchain-beginner-to-intermediate-guide-52660495dbbb
- canonical_url
- https://towardsdev.com/building-your-first-ai-app-langchain-beginner-to-intermediate-guide-52660495dbbb
- author_url
- https://medium.com/@isha372002
- status
- ok
- fetched_at
- 2026-06-17 08:20:12