DEV Community

Ramesh S
Ramesh S

Posted on

LangChain for Absolute Beginners - Part 1: What Is LangChain & Your First AI-Powered Script

If you've played with the OpenAI or Anthropic API and thought "this is cool, but wiring together prompts, tools, and memory by hand is getting messy" - that's exactly the problem LangChain was built to solve.

This is Part 1 of a beginner-friendly series that will take you from "never touched LangChain" to "built and understood a working AI agent." No prior AI framework experience needed - just basic Python.

What You'll Learn in This Series

  1. Part 1 (this article) - What LangChain is, installing it, and your first script
  2. Part 2 - Chat models, prompts, and chaining steps together
  3. Part 3 - Tools and building your first real agent
  4. Part 4 - Retrieval-Augmented Generation (RAG): letting your agent read your documents
  5. Part 5 - Memory and middleware: agents that remember and follow rules
  6. Part 6 - Debugging and observing agents with LangSmith

So, What Is LangChain?

LangChain is an open-source Python (and JavaScript) framework for building applications powered by large language models - chatbots, research assistants, document Q&A tools, and autonomous agents that can call external tools on their own.

Instead of manually stitching together an LLM API call, a prompt template, a tool the model can invoke, and the logic that loops until the model is "done," LangChain gives you pre-built building blocks for all of it. The centerpiece of the modern LangChain API is a function called create_agent, which assembles a model, a prompt, and a set of tools into a working agent in just a few lines.

Under the hood, LangChain agents actually run on LangGraph, LangChain's lower-level orchestration engine. You don't need to learn LangGraph to follow this series - think of it as the engine bolted underneath the car you're about to drive.

Why Not Just Call the API Directly?

You certainly can call openai or anthropic Python SDKs directly, and for a single prompt-in, text-out use case, that's often enough. LangChain starts paying off once you need:

  • The same code to work across different model providers (OpenAI, Anthropic, Google, etc.)
  • A model that can decide, on its own, to call a search tool, a calculator, or your company's internal API
  • Retrieval from your own documents (PDFs, wikis, databases) blended into the model's answers
  • Conversation memory that persists across turns
  • Guardrails - like limiting how many tool calls an agent can make, or redacting sensitive data

We'll build toward all of these over the series. Today, we just get the lights turned on.

Step 1: Set Up Your Environment

Create a project folder and a virtual environment, exactly like we did for the Python and TypeScript series:

mkdir langchain-beginner
cd langchain-beginner
python -m venv venv
source venv/bin/activate   # On Windows: venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Step 2: Install LangChain

pip install langchain langchain-openai
Enter fullscreen mode Exit fullscreen mode

langchain is the core framework. langchain-openai is the integration package for OpenAI models - swap it for langchain-anthropic or langchain-google-genai if you prefer a different provider. LangChain supports 1,000+ integrations, but you only install the ones you actually use.

Step 3: Add Your API Key

Create a .env file in your project folder:

OPENAI_API_KEY=your-key-here
Enter fullscreen mode Exit fullscreen mode

Then install python-dotenv so Python can read it:

pip install python-dotenv
Enter fullscreen mode Exit fullscreen mode

Never commit your .env file to GitHub. Add it to .gitignore immediately.

Step 4: Your First LangChain Script

Let's write the simplest possible LangChain program - send a message to a chat model and print the response.

from dotenv import load_dotenv
from langchain.chat_models import init_chat_model

load_dotenv()

model = init_chat_model("gpt-5", model_provider="openai")

response = model.invoke("Explain what LangChain is in one sentence, for a total beginner.")
print(response.content)
Enter fullscreen mode Exit fullscreen mode

Run it:

python first_script.py
Enter fullscreen mode Exit fullscreen mode

You should see a plain-English explanation printed to your terminal.

What Just Happened?

  • init_chat_model is LangChain's universal entry point for chat models. Change the model name and provider string, and the rest of your code doesn't change - that's the "swap models without rewriting your app" promise in action.
  • model.invoke(...) sends your message and blocks until the full response comes back.
  • The response isn't a plain string - it's a Message object. .content gives you the text; other attributes (which we'll use later) carry token usage, tool calls, and more.

Step 5: Your First Taste of an Agent

Let's go one step further and see the feature that makes LangChain worth learning: an agent that can decide to use a tool.

from dotenv import load_dotenv
from langchain.agents import create_agent

load_dotenv()

def get_word_length(word: str) -> int:
    """Return the number of letters in a word."""
    return len(word)

agent = create_agent(
    "gpt-5",
    tools=[get_word_length],
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "How many letters are in 'LangChain'?"}]}
)

print(result["messages"][-1].content)
Enter fullscreen mode Exit fullscreen mode

Here, create_agent builds a small loop: the model reads your question, notices it has a get_word_length tool available, decides to call it, receives the answer, and then writes a final reply - all automatically. This loop (model → tool call → tool result → model again) repeats until the model is confident it has a final answer, and that entire cycle is what people mean when they say "AI agent."

You just built one, in about ten lines.

What's Next

In Part 2, we'll slow down and properly cover prompt templates and how to chain multiple steps together using LangChain Expression Language (LCEL) - the glue that lets you go from "one model call" to "a real pipeline."

If you run into installation issues or want to follow along on GitHub, drop a comment below and I'll help you troubleshoot.


This is Part 1 of a 6-part LangChain beginner series.

Top comments (0)