LangChain Tutorial: The Most Popular Framework for Building LLM Applications

# Tutorial LangChain: Framework Paling Populer untuk Membangun Aplikasi LLM LangChain adalah framework open-source yang dirancang untuk mempermudah pengembangan aplikasi berbasis Large Language Model...

By Ruby Abdullah · · tutorial
LangChainLLMRAGAI AgentsPython

LangChain Tutorial: The Most Popular Framework for Building LLM Applications

LangChain is an open-source framework designed to simplify the development of applications powered by Large Language Models (LLMs). With a mature ecosystem and a large community, LangChain has become the go-to choice for developers building chatbots, RAG systems, AI agents, and various other AI applications. This tutorial covers LangChain comprehensively, from installation to advanced usage patterns.

Why LangChain?

Building LLM applications from scratch requires significant boilerplate code: managing prompts, connecting to various model providers, implementing memory, orchestrating chains of thought, and much more. LangChain simplifies all of this by providing consistent and modular abstractions.

Key advantages of LangChain:

  • Consistent Model Abstraction: A single interface for OpenAI, Anthropic, Google, Ollama, and dozens of other providers
  • Composable Chains: Build complex pipelines by combining simple components
  • Complete Ecosystem: Integrations with vector databases, document loaders, tools, and external services
  • LangChain Expression Language (LCEL): Declarative syntax for building chains that natively support streaming and async
  • Large Community: Thousands of third-party integrations and comprehensive documentation

Installation and Setup

Basic Installation

pip install langchain langchain-core langchain-community

Provider-Specific Installation

# For OpenAI

pip install langchain-openai

For Anthropic (Claude)

pip install langchain-anthropic

For Google (Gemini)

pip install langchain-google-genai

For local models via Ollama

pip install langchain-ollama

For vector stores

pip install langchain-chroma # ChromaDB

pip install langchain-pinecone # Pinecone

pip install faiss-cpu # FAISS

API Key Configuration

import os

Set API keys as environment variables

os.environ["OPENAIAPIKEY"] = "sk-..."

os.environ["ANTHROPICAPIKEY"] = "sk-ant-..."

Or use .env file with python-dotenv

from dotenv import loaddotenv

loaddotenv()

Core Concepts

1. Chat Models

Chat models are the core component of LangChain. Here's how to use various providers:

from langchainopenai import ChatOpenAI

from langchainanthropic import ChatAnthropic

from langchainollama import ChatOllama

OpenAI GPT-4

llmopenai = ChatOpenAI(model="gpt-4o", temperature=0.7)

Anthropic Claude

llmclaude = ChatAnthropic(model="claude-sonnet-4-20250514", temperature=0.7)

Ollama (local models)

llmollama = ChatOllama(model="llama3.1")

Call the model

response = llmopenai.invoke("Explain what machine learning is in 2 sentences")

print(response.content)

2. Prompt Templates

Prompt templates allow you to create dynamic and reusable prompts:

from langchaincore.prompts import ChatPromptTemplate, MessagesPlaceholder

Simple prompt

prompt = ChatPromptTemplate.frommessages([

("system", "You are an AI assistant specialized in {domain}."),

("human", "{question}")

])

Use the prompt

formatted = prompt.invoke({

"domain": "data science",

"question": "What is the difference between supervised and unsupervised learning?"

})

print(formatted)

3. Output Parsers

Output parsers help transform LLM outputs into structured formats:

from langchaincore.outputparsers import JsonOutputParser, StrOutputParser

from langchaincore.pydanticv1 import BaseModel, Field

String output parser (simplest)

parser = StrOutputParser()

JSON output parser with schema

class BookReview(BaseModel):

title: str = Field(description="Book title")

rating: int = Field(description="Rating 1-5")

summary: str = Field(description="Review summary")

jsonparser = JsonOutputParser(pydanticobject=BookReview)

prompt = ChatPromptTemplate.frommessages([

("system", "Provide a book review in JSON format.\n{formatinstructions}"),

("human", "Review the book: {booktitle}")

Related Articles

Complete LangGraph Tutorial: Building Complex AI Agents

Tutorial Lengkap LangGraph: Membangun AI Agents yang Kompleks LangGraph adalah library dari LangChain untuk membangun st...

PydanticAI Tutorial: A Type-Safe Agent Framework for LLM Apps

Membangun Agen LLM yang Type-Safe dengan PydanticAI PydanticAI adalah framework agen dari tim di balik Pydantic, diranca...

RAGAS: Evaluation Framework for RAG Pipelines

RAGAS: Framework Evaluasi untuk Pipeline RAG Pendahuluan Retrieval-Augmented Generation (RAG) telah menjadi arsitektur s...

Complete LlamaIndex Tutorial: Building RAG Applications with LLMs

Tutorial Lengkap LlamaIndex: Membangun Aplikasi RAG dengan LLM LlamaIndex adalah framework data yang powerful untuk memb...