THN Interview Prep

LangChain for agents

LangChain supplies the primitives: message types, model wrappers, runnables, retrievers, and structured tool schemas (@tool). You compose directed pipelines for preprocessing, deterministic routing, context packing, single-hop tool calls, or nodes that feed into LangGraph for loops.

For durable loops and branching, evolve into LangGraph for agents.


Process — compose then invoke

Loading diagram…

LCEL intuition: A | B | C means “output schema of (A) must satisfy input expectations of (B)”. Fail fast with typed contracts.


Tool binding pipeline

Loading diagram…

Docstrings drive few-shot ergonomics: keep them factual—models read them verbatim.


Chain vs agent decision

Loading diagram…

Easy rule: LangChain is excellent for repeatable typed steps. Move to a graph when the next step depends on prior observations or when you need checkpoints and human interrupts.


Minimal example — bind tools without a manual loop

Below is illustrative of composition around a chat model. In production loops, prefer LangGraph patterns (e.g. create_react_agent in the LangGraph tool tutorial); chains still excel for deterministic pre-/post-steps.

# pip install langchain-core langchain-openai
from langchain_core.messages import HumanMessage
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI


@tool
def unit_convert_c_to_f(celsius: float) -> float:
    """Convert Celsius to Fahrenheit."""
    return celsius * 9 / 5 + 32


llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
bound = llm.bind_tools([unit_convert_c_to_f])
msg = bound.invoke([HumanMessage("Convert 37C to Fahrenheit using the tool")])
print(msg.tool_calls or msg.content)

Senior bar: cite streaming chunks, cancellation tokens, retries on transport layers (distinct from semantic repair).


Tool design standards

StandardExample
Small surfaceget_order_status(order_id) beats query_database(sql).
Typed argumentsPrefer enums, ids, bounded strings, and numeric ranges.
Server-side authorizationTool receives user/tenant context and checks access before work.
No hidden writesRetrieval tools should not mutate production state.
IdempotencyWrite tools require a request id or deterministic operation key.
Clear observationReturn concise facts, ids, and status codes; do not return secrets or raw stack traces.

TypeScript tangent

LangChain.js mirrors the same primitives (bindTools, DynamicStructuredTool). Use JS when deploying on Node-heavy stacks; diagrams above still apply.


When not to use LangChain

Avoid adding LangChain when a plain function call or small typed wrapper is clearer. The library helps when composition, provider abstraction, tools, retrievers, parsers, tracing, or later graph migration matter.

Do not use an agent loop for a fixed path just because the framework makes it easy.


Example design decision

Scenario: Build a support answer endpoint that classifies intent, retrieves context, produces a JSON answer, and logs traces.

Good LangChain use:

  1. Classifier runnable for intent/risk.
  2. Retriever runnable with ACL and dedupe.
  3. Context-packing runnable.
  4. Model call with structured output.
  5. Parser/validator runnable.

Move to LangGraph when the workflow may loop: missing evidence -> retrieve again, tool call -> observe -> decide, high risk -> human approval.

Bad design: using an "agent" for a fixed extract-transform-answer path. It adds nondeterminism without adding capability.


Interview questions — LangChain/LCEL

1. What is a Runnable in LangChain terms?

  • Object with invoke / ainvoke, stream, batch and composable | with schema expectations.

Follow-up: Why does this matter in production?

  • Each runnable boundary gives a place to validate, trace, retry, batch, or swap implementation.

2. Why gate tool descriptions?

  • They become part of prompt surface—overlong catalogs waste tokens & invite wrong routing.

3. Difference between PromptTemplate chaining vs Runnable passthrough.

  • RunnablePassthrough helps carry multimodal keys (context + question) merges; PromptTemplate binds variables only.

4. Where would retrieval live?

  • Dedicated retriever runnable before model call—or inside graph node LangGraph—but keep ACL + dedupe at retriever boundary.

Follow-up: What should not live in the model prompt?

  • Authorization, tenancy, source freshness, and write permissions. Those belong in application code and tool gateways.

5. How avoid silent JSON drift on outputs?

  • Prefer structured output / pydantic parsers with repair loop capped—see Gen AI structured outputs narrative on /gen-ai.

6. Why avoid a huge tool catalog?

  • It increases token cost and wrong-tool routing. Expose only the tools relevant to the current task and user permissions.

7. Where do retries belong?

  • Transport retries belong around providers/tools. Semantic retries belong in a capped repair path with trace visibility.

Interview answer template

For "When would you use LangChain vs LangGraph?", answer:

  1. Use LangChain/runnables for fixed, typed pipelines.
  2. Use LangGraph when the next step depends on state or observations.
  3. Keep tools narrow and typed in both cases.
  4. Put ACL, schema validation, retries, and tracing outside the model.
  5. Evaluate each boundary: retrieval quality, tool routing, output validity, latency, and cost.

Common bad answers

Bad answerWhy it is weak
"LangChain is the agent."LangChain provides primitives; architecture, tools, policy, and evals are still your responsibility.
"Expose all tools and let the model choose."Huge tool catalogs increase wrong routing, prompt surface, and token cost.
"Retries are just retrying the model."Transport retries and semantic repair are different and need separate limits.

Self-check

You are ready if you can explain:

  • Runnable composition and why boundaries matter.
  • When a fixed chain is better than a graph.
  • How tool docstrings affect routing.
  • Where retrieval and ACL checks belong.
  • When to move from LangChain to LangGraph.

Evolving to LangGraph · Observe with LangSmith

Mark this page when you finish learning it.

Spotted something unclear or wrong on this page?

On this page