DEV Community

Arhan Khan
Arhan Khan

Posted on

LangChain for Beginners: Build Your First AI App With Chains

Calling a language model API is easy. Building a real app around it — with prompts, memory, tools, and data — is where things get messy. LangChain is a framework that gives you the building blocks: prompts, chains, memory, and agents, so you can compose AI features instead of wiring everything by hand.

The smallest useful pattern is a chain: a prompt template plus a model, run together.

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_template(
    "Explain {topic} to a beginner in two short paragraphs."
)
model = ChatOpenAI(model="gpt-4o-mini")
parser = StrOutputParser()

chain = prompt | model | parser
print(chain.invoke({"topic": "photosynthesis"}))
Enter fullscreen mode Exit fullscreen mode

The | operator links the pieces: the prompt formats the input, the model generates text, and the parser turns the result into a plain string. From here you can add memory so the app remembers earlier messages, add a retriever so it answers from your own documents, or add tools so it can search the web or call your API.

LangChain works with many models — OpenAI, Anthropic, Google, and local models through Ollama — so you are not locked into one provider. It also has LangSmith, a tool for tracing and debugging what your chains actually do, which becomes essential once prompts get complicated.

A word of caution: LangChain adds abstraction, and abstraction can hide what is really happening. For a tiny script, a direct API call is simpler. But once your app has more than one prompt, or needs memory and tools, LangChain saves you from reinventing the same plumbing.

LangChain is for developers who want to build chatbots, summarizers, and AI assistants without hand-rolling every piece. And if you want a structured way to go deeper into AI beyond one framework, Skill Library has a curated collection of AI learning material worth exploring.

Top comments (0)