Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›LangChain›LLMs and Chat Models

Core Concepts

LLMs and Chat Models

LLMs and Chat Models serve as the fundamental reasoning engines within LangChain by transforming input sequences into generated text outputs. Understanding their distinction is critical because it dictates how you format prompts, manage state, and structure interactions for different application types. You reach for these abstractions whenever you need to integrate generative intelligence into your workflows, ranging from simple text completion to complex, multi-turn conversational agents.

The Fundamental LLM Abstraction

The LLM abstraction in LangChain is designed for text-in, text-out tasks where the primary goal is raw completion. Unlike models trained for dialogue, these underlying engines expect a raw string as input and return a raw string as output. The core reasoning here is that by normalizing these disparate providers into a unified interface, you can swap between different foundation models without rewriting your entire application logic. When using an LLM, the model does not inherently understand conversational history; it simply calculates the most statistically probable continuation of the provided string. This makes it perfect for tasks like summarization, creative writing, or data extraction where the instruction is singular and explicit. By wrapping these in a standard interface, LangChain allows you to leverage common configuration parameters like temperature, top-k, and max_tokens across any integrated model, ensuring a consistent development experience while focusing on prompt engineering over implementation details.

from langchain_openai import OpenAI

# Initialize the raw LLM interface
# This model expects a single string input and returns a string completion
llm = OpenAI(temperature=0.7)

# Perform a direct completion task
result = llm.invoke("Explain the concept of entropy in one sentence:")
print(result)

Transitioning to Chat Models

Chat Models represent an evolution of the traditional LLM, specifically optimized for multi-turn conversations. While raw LLMs accept a single string, Chat Models accept a list of message objects, each with a specific role: 'system', 'human', or 'ai'. This structural difference is significant because it allows the model to maintain context across multiple interaction turns, acknowledging the nuance of dialogue. The reasoning behind this architecture is that modern models are fine-tuned on dialogue datasets, expecting a structured history of back-and-forth interactions rather than just a flat text stream. By providing these roles, you allow the model to differentiate between developer instructions (system messages), user input (human messages), and its own previous output (ai messages). This structure is the backbone of robust conversational applications, as it allows the model to reason about its past responses and stay aligned with the behavioral guidelines established in the system message, leading to much more coherent and controllable outputs than flat completion models.

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

# Chat models accept structured lists of messages
chat = ChatOpenAI(model="gpt-4")
messages = [
    SystemMessage(content="You are a helpful expert assistant."),
    HumanMessage(content="Why is chat structure important?")
]

# The invoke call handles the list of messages effectively
response = chat.invoke(messages)
print(response.content)

Message Role Semantics

To build reliable applications, you must master the semantics of message roles. The System message is the most critical for shaping model behavior, as it defines the 'persona' and operational constraints that persist across the entire conversation. Human messages are the primary input mechanism, representing the user's intent or query at a specific point in the sequence. AI messages act as the historical records of the model's own output, which are essential when you are implementing a long-running conversation and need to feed the model its own past answers to maintain coherence. The reason this role-based approach is superior to simple string concatenation is that it prevents the model from conflating instructions with actual user data. By separating these concerns, LangChain ensures that the underlying model can parse the 'intent' from the 'context' reliably, preventing common errors like prompt injection or identity confusion where the model might accidentally start following instructions embedded within the user's input string.

from langchain_core.messages import AIMessage, HumanMessage

# Building context manually using role-specific classes
conversation = [
    HumanMessage(content="Calculate the area of a circle with radius 5."),
    AIMessage(content="The area is approximately 78.54."),
    HumanMessage(content="Now double that result.")
]

# The chat model maintains the state through the history list
response = chat.invoke(conversation)
print(response.content)

Handling Model Outputs

When interacting with these models, you often need more than just the raw text response; you may require metadata like usage statistics or log-probabilities. LangChain addresses this by wrapping the output in a comprehensive 'AIMessage' object. This object contains the content, but also metadata like the token count, finish reason, and specific provider-level information. Understanding why this is useful is straightforward: in production environments, you need to track costs and identify why a generation stopped (e.g., reaching a token limit vs. a content policy violation). By standardizing the output structure, LangChain allows you to write middleware that tracks these metrics automatically, regardless of which model provider you are using. This design philosophy enables you to build observability, logging, and cost-tracking wrappers around your model calls without ever needing to inspect the proprietary response formats of different vendors, effectively decoupling your business logic from the specific implementation details of any given model provider's API structure.

# Accessing the full response object
response = chat.invoke([HumanMessage(content="Hello!")])

# The response is an AIMessage object containing more than just text
print(f"Content: {response.content}")
print(f"Metadata: {response.response_metadata}")

Configuring Model Parameters

Both LLMs and Chat Models allow for granular control via configuration parameters. Parameters like 'temperature' control the randomness of the output: a value near zero results in deterministic, focused responses, whereas higher values encourage creativity and variance. 'Max tokens' serves as a hard limit on the generated response size, which is vital for cost management and preventing run-away generation in automated scripts. The reasoning here is that different applications have different requirements for 'intelligence' versus 'consistency'. For instance, a data extraction agent requires a temperature of zero to ensure the output remains strictly formatted, while a creative assistant requires higher entropy to produce engaging prose. By allowing these configurations at the object initialization or invocation level, LangChain gives developers the precision to optimize the behavior of their reasoning engines for specific tasks, ensuring that the model not only generates correct content but also adheres to the operational constraints required for a production-grade system.

# Configuring parameters for specific use-cases
# Low temperature for strict, data-heavy tasks
structured_llm = ChatOpenAI(temperature=0, max_tokens=50)

# High temperature for creative generation
creative_llm = ChatOpenAI(temperature=1.2)

# Both share the same interface but behave radically differently
print(structured_llm.invoke([HumanMessage(content="Define AI.")]))

Key points

  • Raw LLMs are designed for simple string-to-string completions without inherent conversational context.
  • Chat Models utilize structured lists of messages to maintain state across multi-turn interactions.
  • The System role defines the persona and behavioral boundaries for the Chat Model.
  • Human messages represent the user input while AI messages represent the recorded conversation history.
  • Standardizing output into AIMessage objects allows for consistent logging and metadata tracking across providers.
  • Temperature settings control the balance between deterministic output and creative variance in responses.
  • Message role separation prevents the model from confusing instructions with user-provided content.
  • LangChain abstracts individual vendor APIs to provide a uniform developer experience for all model types.

Common mistakes

  • Mistake: Treating Chat Models like standard LLMs. Why it's wrong: Chat models require specific message formatting (System, Human, AI) rather than raw text completions. Fix: Always use ChatPromptTemplate and message objects to maintain structure.
  • Mistake: Hardcoding API keys in source files. Why it's wrong: This is a security risk and makes the application non-portable. Fix: Use environment variables and the LangChain configuration helper to load secrets.
  • Mistake: Over-stuffing the context window. Why it's wrong: Exceeding token limits causes errors or truncated responses. Fix: Use retrievers with document splitters and compression to ensure only relevant information is passed.
  • Mistake: Forgetting to track conversation history. Why it's wrong: LLMs are stateless by default and will lose context between turns. Fix: Wrap the chain in a ConversationBufferMemory or similar memory object.
  • Mistake: Failing to define a specific System Prompt. Why it's wrong: Without a persona, the model may be too vague or hallucinate. Fix: Always prepend a SystemMessage that defines the AI's role and boundaries.

Interview questions

What is the primary role of a PromptTemplate in LangChain, and why is it considered a best practice over manual string concatenation?

A PromptTemplate in LangChain is an abstraction that manages the construction of input prompts by defining a template string with placeholders. Using manual string concatenation in code often leads to fragile, hard-to-read, and error-prone logic. By using LangChain's PromptTemplate, you separate the static structure of your prompt from the dynamic user input. This modularity makes it significantly easier to version control prompts, reuse them across different parts of your application, and maintain consistent formatting, which is crucial for ensuring the LLM receives the expected input structure every time.

How does the 'ConversationBufferMemory' work in LangChain, and why is it essential for stateful interactions?

The ConversationBufferMemory component in LangChain stores the raw history of an interaction, maintaining a growing list of messages exchanged between the user and the model. This is essential for stateful interactions because LLMs are inherently stateless, meaning they have no memory of previous prompts. Without this memory, the model would treat every request as an entirely new conversation. By injecting the buffer into the prompt context, we provide the LLM with necessary historical context, allowing it to provide coherent, follow-up responses that feel natural and continuous to the user.

Explain the function of an 'OutputParser' in LangChain and why it is a critical component for building robust production applications.

An OutputParser is responsible for taking the raw string output from an LLM and structuredly transforming it into a more usable format, such as a JSON object, a Pydantic model, or a specific list. This is critical for production because LLMs are non-deterministic and can output varied text. Without a parser, downstream systems expecting strict data formats would crash. Using a parser allows you to enforce schema validation and error handling, ensuring that your application logic receives predictable, machine-readable data regardless of the unstructured text generated by the model.

Compare 'Chains' and 'LCEL' (LangChain Expression Language) for building complex workflows. Which should you prefer?

Traditional Chains are pre-built, class-based structures designed for specific common tasks, offering quick integration but limited customizability. Conversely, LCEL is a declarative, functional composition language that allows you to pipe components together using a simple '|' operator. You should prefer LCEL for almost all modern workflows because it provides superior readability, easier debugging via tracing, and better control over data streaming and parallel execution. LCEL treats every component as a Runnable, making it significantly more flexible than the rigid, older class-based chain implementations.

What is the purpose of a 'RetrievalQA' chain, and why is it superior to simply pasting a large document into a prompt?

A RetrievalQA chain is designed to retrieve relevant information from a vector store based on a user query and feed that specific context into the LLM. Simply pasting a massive document into a prompt is inferior because it quickly exceeds the context window limits of the model, which leads to truncation or degradation in performance. Additionally, sending irrelevant data increases latency and costs. RetrievalQA ensures only the most relevant document chunks are sent, resulting in higher accuracy, cost-efficiency, and adherence to the model's token limits.

How do LangChain Agents utilize Tools, and why is this approach more powerful than using a standard LLM call?

LangChain Agents act as a reasoning engine that selects and executes external tools—like search engines, calculators, or database queries—to satisfy a complex user request. A standard LLM call is limited to the static knowledge embedded during its training phase and cannot interact with the real-time world. By using Agents, the model gains the ability to reason about which actions to take, observe the output of those actions, and dynamically iterate until the goal is achieved. This transforms the model from a simple text generator into an autonomous agent capable of solving multi-step, real-world problems.

All LangChain interview questions →

Check yourself

1. When building a LangChain application, why do we use ChatPromptTemplate instead of a standard PromptTemplate?

  • A.It automatically compresses the chat history for performance.
  • B.It manages the structured sequence of System, Human, and AI messages required for chat models.
  • C.It automatically switches the model provider based on user load.
  • D.It bypasses the need for API keys during the testing phase.
Show answer

B. It manages the structured sequence of System, Human, and AI messages required for chat models.
Chat models expect specific role-based message objects. The second option is correct because ChatPromptTemplate facilitates this. The others are incorrect because they describe features like memory compression, provider switching, or authentication, which are handled by different components.

2. What is the primary function of a 'Chain' in LangChain when interacting with an LLM?

  • A.To convert all prompts into vector embeddings.
  • B.To link multiple components together to form a coherent sequence of operations.
  • C.To replace the need for an LLM entirely.
  • D.To force the model to output JSON only.
Show answer

B. To link multiple components together to form a coherent sequence of operations.
Chains are the glue that connects prompts, models, and memory. The other options are wrong: embeddings are separate, chains do not replace models, and output formatting is usually handled by output parsers.

3. If you are building a bot that needs to remember previous user inputs, why is a Memory component required?

  • A.LLMs are naturally stateless and do not store dialogue context from one call to the next.
  • B.The memory component increases the LLM's long-term training weights.
  • C.Memory is required to validate the user's login credentials.
  • D.It is used to store the user's vector embeddings in a database.
Show answer

A. LLMs are naturally stateless and do not store dialogue context from one call to the next.
LLMs operate on single requests (stateless). The first option is correct because the app must manually manage history. The others are wrong because memory doesn't change model weights, handle auth, or store embeddings.

4. In LangChain, what role does an 'Output Parser' perform?

  • A.It sends the prompt to the model and waits for the response.
  • B.It transforms the raw text output from an LLM into a specific format like a list or JSON.
  • C.It measures the number of tokens consumed by a request.
  • D.It cleans the input text for better sentiment analysis.
Show answer

B. It transforms the raw text output from an LLM into a specific format like a list or JSON.
Output Parsers restructure raw model strings. The other options are incorrect: invoking the model is the chain's job, token counting is handled by callbacks, and cleaning input is part of prompt preprocessing.

5. When using a 'RetrievalQA' chain, what is the role of the VectorStore?

  • A.To write the source code for the AI agent.
  • B.To serve as a knowledge base where semantic search can find relevant document fragments.
  • C.To act as the primary interface for the user's chat input.
  • D.To handle the API connection to the underlying model provider.
Show answer

B. To serve as a knowledge base where semantic search can find relevant document fragments.
VectorStores store embeddings for semantic lookup. The second option is correct. The others are wrong because vector stores don't write code, aren't user interfaces, and aren't responsible for provider API connections.

Take the full LangChain quiz →

← PreviousLangChain Architecture and LCELNext →Prompt Templates

LangChain

24 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app