LangChain: Building LLM Applications with Python

Lesson 1 of 6

Why LangChain? Prompt Templates and Chat Models

Calling an LLM API directly (as in the RAG and AI Agent courses) works, but every real application ends up rebuilding the same pieces: swappable prompts, a consistent interface across model providers, and a way to chain steps together. LangChain is a Python framework that standardizes those pieces so you're not reinventing them for every project.

Chat models: one interface, many providers

Pythonshares state with other Python blocks

This needs a real API key and network access, so it's read-only here. The point of ChatOpenAI (and its siblings, ChatAnthropic, ChatGoogleGenerativeAI, and others) is that they all implement the same .invoke() interface, swapping providers later means changing one constructor call, not rewriting your application logic.

Prompt templates

Hardcoding a prompt as a plain string works until you need to reuse it with different inputs, a PromptTemplate (or ChatPromptTemplate for chat models) separates the fixed wording from the variables that change per call:

Pythonshares state with other Python blocks

{topic} is a placeholder filled in by whatever dictionary you pass to .invoke(), the same template can be reused with {'topic': 'closures'} or {'topic': 'binary search'} without touching the wording itself.

Why this matters over raw API calls

  • Consistency: the same .invoke() shape works whether the underlying model is from OpenAI, Anthropic, or a local model.
  • Reusability: a prompt template is a first-class object you can store, version, and reuse across an app, instead of a string interpolated in twelve different places.
  • Composability: templates and models are designed to be chained together, which is exactly what the next lesson builds on.

TIP

.invoke() runs synchronously and returns a single result, LangChain's models also support .stream() (token-by-token output) and .batch() (many inputs at once), same interface, different execution mode.

📝 LangChain Basics Quiz

Passing score: 70%
  1. 1.What is the main benefit of LangChain's ChatOpenAI, ChatAnthropic, etc. all sharing the same .invoke() interface?

  2. 2.In a ChatPromptTemplate, a value like {topic} is a ____ filled in from the dictionary passed to .invoke().

  3. 3.A PromptTemplate lets you reuse the same wording with different input variables instead of hardcoding a new string each time.