Skip to main content

07. Frameworks & Libraries

Building AI Applications with LangChain

Building with LangChain means wrapping runnables in a real app: auth, sessions, RAG or agent flows, evals, traces, and budgets. The framework wires LLM pieces; your product code owns orchestration, safety, and shipping quality.

What you will learn

  • Sketch a layered LangChain application architecture
  • Build a RAG path: ingest โ†’ retrieve โ†’ generate โ†’ cite
  • Choose chain vs agent for a product feature
  • List production must-haves (evals, traces, budgets, safety)
  • Answer interview questions with short, solid answers
Layered LangChain app from UI through orchestration to backends
Interview staple: LangChain sits under your orchestration โ€” not instead of it.

Definition

A LangChain application is a product that uses LangChain (or LangChain.js) runnables โ€” chains, retrievers, tools, agents, memory โ€” behind an API or UI, with your own session, policy, and observability layers.

Fundamentals covered prompts, tools, and memory. This lesson focuses on assembling them into shippable features: RAG assistants, tool agents, and the production controls that keep them reliable.

Introduction

This lesson (#2 in Frameworks & Libraries) follows LangChain Fundamentals. Next in-module: LangGraph Fundamentals โ€” when you need durable stateful graphs.

Prefer the smallest architecture that meets quality: many โ€œAI appsโ€ are a retrieve | prompt | LLM chain with citations, not a full agent.

App layers

Keep LangChain inside a clear boundary so you can swap providers or migrate hard workflows to LangGraph later.

UI API orchestration LangChain backends layered architecture
UI/API โ†’ orchestration โ†’ LangChain runnables โ†’ models/stores/APIs.
  • Edge - auth, rate limits, request validation
  • Orchestration - session ids, budgets, feature flags
  • LangChain - chains/agents/tools/memory composition
  • Backends - LLM APIs, vector DB, business APIs, files
  • Obs - traces, metrics, prompt/version logs

Pattern A: RAG application

Ground answers in your corpus. LangChain helps with loaders, splitters, embeddings, retrievers, and citation-friendly prompts.

Ingest index retrieve generate cite RAG flow
Ingest offline; retrieve+generate online; always return sources.
  • Ingest - load, clean, chunk with metadata
  • Index - embed into a vector store (and refresh on change)
  • Retrieve - top-k (+ filters / rerank if needed)
  • Generate - prompt with context + user question
  • Cite - structured sources; refuse when context is empty

Pattern B: Tool-using agent app

  • Expose a small tool allowlist behind authz checks
  • Wrap agent.invoke with max iterations and wall-clock timeout
  • Persist chat memory per session key
  • Log every tool call (name, latency, success/error)
  • Fall back to a chain path when the intent is known

Choosing chain vs agent in product

  • Chain - FAQ, classifyโ†’route, RAG Q&A, format transforms
  • Agent - open-ended tasks needing 2+ unknown tools
  • Hybrid - router chain selects a specialist chain or agent
  • Measure - quality, p95 latency, and $/request before adding agents

Production checklist

Demos invoke a chain. Products pass evals, survive traffic, and fail safely.

Evals traces budgets safety and version pin checklist
Evals, traces, budgets, safety gates, pinned versions โ€” non-negotiable for ship.
  • Evals - golden questions; regression on prompt/model change
  • Observability - span every hop and tool
  • Budgets - token, cost, and latency caps per request
  • Safety - PII redaction, tool authz, prompt-injection awareness
  • Versioning - pin packages and model IDs; record prompt versions

Failure modes

  • Notebook-as-prod - secrets in code, no session isolation
  • Uncited RAG - fluent lies; require sources or abstain
  • Agent sprawl - too many tools; wrong actions
  • Silent upgrades - model/package drift breaks quality
  • No timeouts - hung tool calls block workers

Program / implementation notes

Structure repos as api/ + llm/ (runnables) + evals/. Keep business authz outside the model. Stream tokens to the UI when UX needs it.

  • Start RAG or a thin agent; add LangGraph when you need cycles/checkpoints
  • Next: LangGraph Fundamentals (Nodes, Edges, State, Routing)

Advantages and limitations

  • Advantage: fast path from prototype to integrated LLM features
  • Advantage: rich loaders/retrievers/tool wrappers
  • Advantage: clear upgrade path to LangGraph for hard control flow
  • Limitation: easy to skip evals and ship brittle demos
  • Limitation: abstraction churn โ€” pin versions deliberately
  • Limitation: not a substitute for product auth, data, and ops design

Key points

  • Apps wrap LangChain with auth, sessions, budgets, and obs.
  • RAG: ingest โ†’ index โ†’ retrieve โ†’ generate โ†’ cite.
  • Agents need small allowlists, timeouts, and tool logs.
  • Prefer chains when the path is known.
  • Ship with evals, traces, safety gates, and pinned versions.
  • Graduate to LangGraph for durable stateful workflows.

FAQs

Should every LangChain app use an agent?

No. Many production features are RAG or fixed chains. Use agents when runtime tool choice is required.

Where do I put API keys?

In server-side env/secrets managers. Never expose provider keys to the browser or embed them in prompts.

How do I keep RAG answers honest?

Retrieve first, prompt with only retrieved context, require citations, and abstain when retrieval is empty or low confidence.

When move from LangChain agents to LangGraph?

When you need cycles, durable checkpoints, complex branching, or human approval mid-flow beyond a simple agent loop.

Interview questions

  1. How do you structure a LangChain application?

    Answer Edge auth/API, app orchestration (session/budgets/logs), LangChain runnables, and backends (models, stores, tools) with observability across layers.

  2. Outline a RAG app built with LangChain.

    Answer Ingest and chunk docs, embed/index, retrieve top-k at query time, generate with context, and return citations or abstain.

  3. Chain or agent for a product feature?

    Answer Use a chain when steps are known; use an agent when the model must choose among tools at runtime โ€” measure cost and quality either way.

  4. Name production controls for LangChain apps.

    Answer Evals/golden sets, tracing, token/latency budgets, tool authz and PII gates, and pinned model/package/prompt versions.

  5. What is a common RAG failure in apps?

    Answer Generating without usable retrieved context โ€” fix with better retrieval, refusal policies, and mandatory citations.

  6. How does this relate to LangGraph?

    Answer LangChain apps handle many pipelines/agents; LangGraph is the next step for durable, stateful, human-in-the-loop graphs.

Related topics

Practice task

Complete each numbered practice step in a notebook or doc before opening the next lesson.

  1. Draw the four app layers for a โ€œdocs Q&Aโ€ product and name one component in each.
  2. Write the online RAG path (retrieveโ†’generateโ†’cite) and the offline ingest job separately.
  3. Decide chain vs agent for โ€œreset passwordโ€ vs โ€œinvestigate billing anomalyโ€ โ€” one sentence each.
  4. List your ship checklist items (evals, traces, budgets, safety, pins) with an owner.
  5. Answer interview questions 1, 2, and 4 in two sentences each.