Interview questions with answers
LangChain interview questions and answers
Models, prompts, tools, retrieval and agents with create_agent. Open any question to read its model answer; the multiple-choice set marks the right option and explains it.
- Subjective
- 30
- Objective
- 30
Subjective questions 30
1What is LangChain and what problems does it solve?Asked almost every time
LangChain is an open-source framework (Python and JavaScript) for building applications on language models. It gives one interface over many model providers, plus building blocks for prompts, tools, retrieval, memory and agents, so you can switch models or vector stores without rewriting the app. In v1 the centre is create_agent, built on LangGraph, with middleware for guardrails and control.
2Explain how retrieval-augmented generation (RAG) works in LangChain.Asked almost every time
Indexing: load documents with a loader, split them with a text splitter, embed the chunks and store them in a vector store. Querying: a retriever finds the chunks most similar to the question, they are put into the prompt as context, and the chat model answers from them, ideally citing sources. Each piece is swappable: loader, splitter, embedding model, vector store, retriever, model.
3How do you build an agent in LangChain v1?Asked almost every time
Call create_agent with a model, a list of tools and a system prompt. Tools are functions decorated with @tool (or given a schema), whose names, descriptions and argument types are sent to the model. The agent loops: the model chooses a tool call, the runtime runs it and returns the result, until the model answers. Middleware can add human approval, summarisation of long histories, PII redaction or limits on calls.
4How do tool calling and function calling work under the hood?Asked almost every time
You describe each tool with a name, description and JSON schema for its arguments. The model, instead of answering in text, returns a tool call with arguments matching that schema. Your code runs the tool, sends the result back as a tool message, and the model continues until it answers. LangChain normalises this across providers with bind_tools and tool_calls on the AI message.
5What is the difference between a chain and an agent?Asked almost every time
A chain is a fixed sequence of steps you define in code: the same path every time, predictable and cheap. An agent lets the model decide which tools to call and in what order, looping until done: flexible but less predictable and more costly. Use chains when the steps are known; use agents when the path depends on the input.
6How do you reduce hallucinations in a LangChain RAG app?Asked almost every time
Improve retrieval first (better chunking, hybrid search, reranking), instruct the model to answer only from the provided context and say when it does not know, require citations, lower temperature, and evaluate faithfulness on a test set. Add a check step that verifies claims against the sources for high-stakes answers.
7What is LCEL and how does the pipe operator work?Very often asked
The LangChain Expression Language composes Runnables with |, so prompt | model | parser is a Runnable whose output feeds the next step. Every Runnable has invoke, batch, stream and async versions, so a composed chain streams and batches for free. It suits fixed pipelines; agents with loops are better built with create_agent or LangGraph.
8How do you get structured output from a model?Very often asked
Use model.with_structured_output(Schema) with a Pydantic model, TypedDict or JSON Schema. LangChain uses the provider's native structured output or tool calling, and returns a parsed object. Validation errors surface when the output does not fit, so you can retry or fall back.
9How does LangChain handle conversation memory?Very often asked
Short-term memory is the message history passed to the model each turn. With agents built on LangGraph, a checkpointer stores state per thread_id, so each conversation keeps its history across requests. Long histories are trimmed or summarised (summarisation middleware) to fit the context window. Long-term memory across conversations uses a store, such as LangGraph's Store.
10How do you choose a chunking strategy for documents?Very often asked
Split on natural boundaries (headings, paragraphs, code blocks) with RecursiveCharacterTextSplitter or a format-aware splitter, at a few hundred tokens with some overlap. Then measure retrieval quality on real questions and tune. Keep metadata such as source, page and section with each chunk for citations and filtering.
11What is a retriever, and how is it different from a vector store?Very often asked
A vector store saves embeddings and runs similarity search. A retriever is the more general interface: given a query, return relevant documents. It may wrap a vector store, or combine several sources, use BM25, rerank results, filter by metadata, or rewrite the query first (multi-query, self-query).
12What are messages in LangChain?Very often asked
Typed chat turns: SystemMessage sets behaviour, HumanMessage holds user input, AIMessage holds model output (including tool_calls), and ToolMessage returns a tool result linked by tool_call_id. Using typed messages keeps conversations portable across model providers.
13How do prompt templates work?Very often asked
ChatPromptTemplate.from_messages defines messages with placeholders such as {question}; MessagesPlaceholder inserts a list of messages like chat history. Invoking the template with values returns formatted messages ready for the model, keeping prompts reusable and testable.
14How do you handle errors and retries in LangChain?Very often asked
Runnables have .with_retry() for transient failures (rate limits, timeouts) with backoff, and .with_fallbacks([...]) to try another model or chain when one fails. In agents, tool errors can be returned to the model as messages so it can correct its arguments instead of crashing.
15What is init_chat_model and why use it?Very often asked
A helper that creates a chat model from a provider-and-name string such as 'anthropic:claude-sonnet-4-5' or 'openai:gpt-5'. It lets you switch providers through configuration rather than code changes, and supports making the model configurable at runtime.
16How do you choose an embedding model?Very often asked
Compare candidates on your own data with a retrieval benchmark (recall@k), and weigh dimensions (storage and speed), context length, language support, cost and whether it can run locally. Never mix embeddings from different models in one index; re-embed everything when you switch.
17What is a parent-document retriever?Very often asked
It indexes small chunks for precise matching but returns their larger parent documents or sections to the model. This combines accurate retrieval with enough surrounding context to answer well, at the cost of more tokens per answer.
18How do you stream responses token by token?Often asked
Call .stream() (or astream) on the model, chain or agent and send chunks to the client as they arrive. Agents can stream messages and intermediate steps with stream_mode options. Streaming improves perceived latency a lot, even when total time is the same.
19How do you make tools safe for an agent?Often asked
Validate arguments with a schema, enforce permissions inside the tool (never rely on the prompt), keep tools read-only unless writes are essential, require human approval for risky actions (human-in-the-loop middleware), add timeouts and rate limits, and return clear error messages so the model can recover.
20How would you reduce cost and latency in a LangChain app?Often asked
Use smaller models for easy steps such as routing, cache identical calls, cut prompt size by retrieving fewer, better chunks, stream output, run independent calls in parallel with batch or RunnableParallel, and measure each step's tokens and time in LangSmith traces.
21How do you evaluate a LangChain application?Often asked
Trace runs in LangSmith, build a dataset of real inputs with expected outputs, and run experiments with evaluators: exact checks, reference comparisons and LLM-as-judge. For RAG, evaluate retrieval (did the right chunks come back?) separately from generation (is the answer faithful?).
22How do you add conversation history to a RAG chain?Often asked
Rewrite the latest question into a standalone query using the history (so 'what about its price?' becomes a full question), retrieve with that query, then answer with both the retrieved context and the recent messages. Trim or summarise old history to stay within the context window.
23What are output parsers and when are they still needed?Often asked
They turn raw model text into structured data: StrOutputParser, JSON parsers and Pydantic parsers. With models that support native structured output, with_structured_output is usually better; parsers remain useful for streaming partial JSON or models without that feature.
24How do you cache LLM calls in LangChain?Often asked
Set a global LLM cache (in-memory, SQLite or Redis) so identical prompts return stored results, and cache embeddings with CacheBackedEmbeddings so re-indexing unchanged documents is free. Semantic caches can also match similar questions, but need a careful similarity threshold.
25How do you summarise a document longer than the context window?Often asked
Map-reduce: summarise chunks independently, then combine the summaries (possibly in several rounds). Or refine: summarise the first chunk and update the summary with each following chunk. Map-reduce parallelises well; refine keeps more continuity.
26What are RunnableParallel and RunnablePassthrough for?Often asked
RunnableParallel runs several runnables on the same input at once and returns a dict of results, such as context and question together. RunnablePassthrough forwards the input unchanged, often to keep the original question next to retrieved context.
27What are document loaders and give examples.Sometimes asked
Loaders read data from a source into Document objects with content and metadata: PDFs (PyPDFLoader), web pages (WebBaseLoader), CSV, Notion, Google Drive, databases, S3 and many more in the integrations packages.
28How do LangChain, LangGraph and LangSmith relate?Sometimes asked
LangChain gives model, tool and retrieval building blocks and the high-level create_agent. LangGraph is the lower-level runtime for stateful, durable graphs of steps, which LangChain agents are built on. LangSmith is the platform for tracing, evaluating and monitoring apps built with either, or with no framework at all.
29How do you track token usage and cost?Sometimes asked
Read usage_metadata on AI messages (input and output tokens), aggregate with callbacks, and use LangSmith, which records tokens and estimated cost per run so you can find expensive steps.
30How do you keep a LangChain app up to date with frequent library changes?Sometimes asked
Pin versions, upgrade deliberately with the migration guides, keep provider integrations in their own packages, wrap LangChain usage behind your own small interfaces, and run your evaluation suite after every upgrade.
Objective questions 30
1Which function creates an agent in LangChain v1?Asked almost every time
Why: LangChain v1 consolidates agent building into create_agent, which runs on LangGraph.
2What does a text splitter do in a RAG pipeline?Asked almost every time
Why: Chunks small enough to match a question precisely, but big enough to carry context, are the base of good retrieval.
3What is stored in a vector store?Asked almost every time
Why: Similarity search compares the query embedding with the stored chunk embeddings.
4Which message type returns a tool's result to the model?Asked almost every time
Why: It carries tool_call_id to match the request.
5Which method attaches tools to a chat model?Asked almost every time
Why: The model can then return tool calls.
6What should a RAG prompt tell the model when the context lacks the answer?Asked almost every time
Why: This reduces hallucinations.
7What does prompt | model | parser create?Very often asked
Why: LCEL's pipe operator chains Runnables so each output feeds the next input.
8Which method returns output matching a Pydantic schema?Very often asked
Why: It binds the schema using the provider's structured output or tool calling, and parses the result.
9Which decorator turns a Python function into a tool?Very often asked
Why: @tool reads the name, docstring and type hints to build the tool's schema.
10Which method streams output chunks as they are generated?Very often asked
Why: stream() yields chunks; invoke() waits for the full result.
11Which method adds automatic retries to a runnable?Very often asked
Why: It retries with backoff.
12Which method tries a backup model when the first fails?Very often asked
Why: Fallbacks run in order.
13Which placeholder inserts chat history into a prompt?Very often asked
Why: It takes a list of messages.
14What does init_chat_model('openai:gpt-5') do?Very often asked
Why: It makes switching providers a config change.
15Why never mix embeddings from two models in one index?Very often asked
Why: Similarity scores become meaningless.
16What do embeddings capture?Often asked
Why: Texts with similar meaning have vectors close together.
17Which retriever technique generates several rephrasings of a question?Often asked
Why: It retrieves with each variant and merges results, improving recall.
18Why keep metadata such as source and page with each chunk?Often asked
Why: Metadata lets the app cite where an answer came from and filter by section, date or permission.
19What does middleware in create_agent let you do?Often asked
Why: Middleware hooks into the agent loop for guardrails, human-in-the-loop, retries and context management.
20What does RunnablePassthrough do?Often asked
Why: Useful to keep the question next to context.
21Map-reduce summarisation…Often asked
Why: It scales to long documents.
22Where are token counts found on a model response?Often asked
Why: It lists input and output tokens.
23What does CacheBackedEmbeddings avoid?Often asked
Why: Embeddings are stored by content hash.
24What does a query-rewriting step do in conversational RAG?Often asked
Why: Retrieval needs a self-contained query.
25Which package holds third-party integrations like a specific vector store?Sometimes asked
Why: Integrations live in separate partner packages and langchain-community, keeping the core small.
26RecursiveCharacterTextSplitter tries to split on…Sometimes asked
Why: It tries larger separators first so chunks keep natural boundaries.
27Which method runs a chain on many inputs at once?Sometimes asked
Why: batch() runs inputs in parallel with a concurrency limit.
28Which retriever returns larger sections than the chunks it searches?Sometimes asked
Why: Small chunks match; parents give context.
29Which runnable runs several branches on the same input at once?Sometimes asked
Why: It returns a dict of results.
30Which parser returns plain text from a chat model?Sometimes asked
Why: It extracts the message content.
Track what you have practised and search every bank in your dashboard.
Start learning