Scrapeless Wiki

What Is LangChain? – Definition and How It Works

Informational P1 what is langchain

Learn what LangChain is, what its core abstractions do, how chains differ from graphs, and when a framework helps rather than adds indirection.

LangChain is an open-source framework for building applications on top of language models. It supplies the parts that sit around the model β€” prompt templates, model adapters, output parsers, retrieval, tools, and memory β€” so those pieces do not have to be rebuilt per project.

1. What Is LangChain?

LangChain is a composition layer between an application and one or more model providers.

  • Key idea: treat prompting, retrieval, tool use, and parsing as composable components with a common interface.
  • Mechanism: a shared runnable interface that lets components be piped together, invoked, streamed, and batched.
  • Goal: reduce the glue code between a model, its data, and the tools it calls.

Example of Composition

chain = prompt | model | output_parser
result = chain.invoke({"question": "What is a residential proxy?"})

Each element is swappable. Changing provider means changing the model object, not the surrounding code.

2. The Core Pieces

Component What it does
Model adapters One interface across chat models and embedding models from different providers
Prompt templates Parameterised prompts with variables, few-shot examples, and message roles
Output parsers Turn generated text into typed objects, with validation and retry-on-parse-failure
Retrievers Fetch relevant documents from a vector store, a search API, or a database
Tools Functions a model may call, wrapped with the schemas providers expect
Memory Carry conversation state between turns

3. Chains Versus Graphs

A chain is a linear pipeline: input flows through steps in order. That covers a large share of real work β€” retrieve, prompt, generate, parse.

Agents need cycles: call a tool, look at the result, decide whether to call another. Expressing that as a chain becomes awkward, which is why the ecosystem added a graph-based runtime where nodes are steps and edges are transitions, including loops and conditional branches. Chains for pipelines, graphs for agents.

4. What It Is Good At

  • Swapping providers without rewriting application logic.
  • Standard RAG assembly, where loaders, splitters, vector stores, and retrievers already exist as components.
  • Structured output, where a schema is declared once and validation is handled by the parser.
  • Streaming and batching, available uniformly across composed components.
  • Tracing, since a composed graph can be instrumented step by step rather than logged ad hoc.

5. When a Framework Adds Indirection

A single prompt with one provider does not need a framework; a direct SDK call is shorter and easier to debug. Abstractions also lag providers, so a brand-new provider feature may be reachable only by dropping to the underlying client.

The honest test is whether you are using at least two of retrieval, tool calling, structured output, and provider portability. One of them alone rarely repays the extra layer.

6. Real-World Examples

  • A documentation assistant β€” loader, splitter, embeddings, vector store, retriever, and a prompt, mostly assembled from stock components.
  • A research agent β€” a graph that searches, fetches pages, and loops until it has enough sources.
  • An extraction service β€” a schema, a parser, and a retry path that re-prompts when validation fails.
  • A provider comparison β€” the same chain run against several models to compare cost and quality on identical inputs.

7. Common Misconceptions

  • "LangChain is a model." It is a framework. It ships no model of its own and calls providers you configure.
  • "Using it locks you into one provider." The adapter layer exists to do the opposite; the lock-in risk is to the framework's own abstractions, not to a vendor.
  • "Chains are enough for agents." A linear pipeline cannot express "call a tool, look, decide again". Cyclic behaviour needs the graph runtime.
  • "The framework fixes retrieval quality." It supplies the components; chunking, embedding choice, and re-ranking still decide whether the right passage is found.

8. Summary

LangChain standardises the components around a language model and lets them be composed, which pays off when an application combines retrieval, tools, and typed output, or must stay portable across providers.

For a single prompt against a single provider it is overhead. Choose it for the surrounding machinery, not for the model call itself.