# LangChain basics for beginners

LangChain is a framework for building applications on top of language models. It
gives you one interface across providers, standard pieces for prompts, tools, and
structured output, and a clean way to wire them together. This guide is the
foundation you build agents on in the rest of this phase.

## What is LangChain for, and when should I skip it?

LangChain earns its place when you want to swap models without rewriting, when
you need standard handling for messages, tools, and [structured output](/guides/structured-outputs), or when
you are building an agent loop. Its value is the abstraction layer.

Be honest about the flip side: if you only ever call one provider with a simple
prompt and no agent loop, the provider's own SDK is simpler and has fewer moving
parts. Reach for LangChain when the abstraction pays for itself, not by default.

> Worth knowing: LangChain 1.0 (late 2025) slimmed the core `langchain` package
> down to agent essentials and moved older chain classes into a separate
> `langchain-classic` package. If you find a tutorial importing from
> `langchain.chains`, it is pre-1.0.

## How do I install LangChain?

Install the core plus the provider you want. Each provider is its own package:

```bash
pip install -U langchain langchain-core langchain-anthropic
```

Set your `ANTHROPIC_API_KEY` in the environment and you are ready.

## What are the core pieces of LangChain?

A few building blocks cover most of what you do.

**Models.** Initialise a chat model. The provider-agnostic helper lets you swap
providers with a string:

```python
from langchain.chat_models import init_chat_model

model = init_chat_model("claude-sonnet-4-6", model_provider="anthropic")
```

**Messages and prompts.** Conversations are lists of messages, and prompt
templates fill in variables:

```python
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("Answer in one sentence: {question}")
```

**Output parsing.** Parsers turn the model's reply into the shape you want, from
a plain string up to a validated object:Do not treat `with_structured_output` as a guarantee that the field types are correct. It enforces the schema's shape, but the model still picks the values, so a confident wrong `year: 2011` parses clean and slips through. Keep your own sanity checks on the numbers that matter, and prefer descriptive field names and docstrings on the Pydantic model so the model fills them right in the first place.

```python
from pydantic import BaseModel

class Movie(BaseModel):
    title: str
    year: int

structured = model.with_structured_output(Movie)
structured.invoke("Give me details about Inception")  # -> a Movie
```

**Tools.** Decorate a function to [make it callable by the model](/guides/give-your-agent-tools):

```python
from langchain.tools import tool

@tool
def get_weather(city: str) -> str:
    """Return the weather for a city."""
    return f"Sunny in {city}"

model_with_tools = model.bind_tools([get_weather])
```

## How do I compose a chain with the pipe operator?

LangChain's composition style chains pieces with the `|` operator. Each piece
takes the previous one's output, and the whole chain shares one interface:
`invoke`, `stream`, `batch`, and their async versions.A bare `prompt | model` chain returns an `AIMessage`, not a string, so `print()`-ing it shows the whole object repr and `result.upper()` throws. Add `| StrOutputParser()` (or read `.content`) when you want plain text out.

```python
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_template("Answer in one sentence: {question}")
model = init_chat_model("claude-sonnet-4-6", model_provider="anthropic")
parser = StrOutputParser()

chain = prompt | model | parser
print(chain.invoke({"question": "What is a chain?"}))
```

That is a complete LangChain program: a prompt, a model, and a parser, composed
into one runnable thing.

## When should I use LangChain instead of a plain SDK?

If your task is "prompt in, text out", a chain like the one above is plenty. The
moment you need the model to loop, call tools, branch, and remember state, a flat
chain is not enough. That is what graphs are for, and the next guide introduces
[LangGraph](/guides/langgraph-basics).
