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
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.
- 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 - 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 - 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
- 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.
- 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.
- 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.
- 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.
- What is a common RAG failure in apps?
Answer Generating without usable retrieved context โ fix with better retrieval, refusal policies, and mandatory citations.
- 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.
- Draw the four app layers for a โdocs Q&Aโ product and name one component in each.
- Write the online RAG path (retrieveโgenerateโcite) and the offline ingest job separately.
- Decide chain vs agent for โreset passwordโ vs โinvestigate billing anomalyโ โ one sentence each.
- List your ship checklist items (evals, traces, budgets, safety, pins) with an owner.
- Answer interview questions 1, 2, and 4 in two sentences each.