Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

LangChain blueprint · Tech & SaaS

Tool-calling agent loop

An agent built with create_agent reasons, calls a tool, reads the result and repeats until it can answer.

Steps
7
Subjective questions
32
Objective questions
32

The problem and the outcome

Problem: A plain chatbot cannot look anything up or take an action.

Outcome: An assistant that searches, queries and calls APIs until the job is done.

Architecture

User question01Agentmodel + tool schemas02Tool call?03Toolssearch · SQL · API04Final answer06Memorychat history07yes5 · observationnoread / write

How it works, step by step

  1. The question goes to an agent created with create_agent and a list of tools.
  2. The model sees every tool's name, description and argument schema.
  3. It either answers directly or emits a structured tool call.
  4. The runtime runs the tool with the arguments the model chose.
  5. The result comes back as an observation and the model reasons again; the loop repeats.
  6. When no tool call is needed, the agent returns its final answer.
  7. Memory keeps the conversation, so follow-up questions have context.

Tech stack

  • LangChain create_agent
  • Tool definitions with typed arguments
  • Chat model with tool calling
  • Middleware (guardrails, summarisation, human approval)
  • LangSmith tracing

Key design decisions

  • Give the agent a small set of well-described tools with clear argument schemas; the descriptions are part of the prompt.
  • Cap the number of steps and add timeouts, so a confused agent cannot loop forever.
  • Keep tools read-only by default and require approval for anything that changes data or spends money.

Common mistakes

  • Twenty overlapping tools with vague names, so the model picks the wrong one.
  • Returning huge raw tool outputs that flood the context window.
  • Letting the model build raw SQL or shell commands with no validation.

What to measure

  • Task success rate
  • Average steps and tool calls per task
  • Tool error rate
  • Cost and latency per task

Most-asked interview questions

Subjective questions 32

1Explain the ReAct loop behind a tool-calling agent.Asked almost every time

The model reasons about the task and either answers or emits a tool call with arguments. The runtime executes the tool and feeds the result back as a message. The model reads it and decides the next step. This repeats until the model produces a final answer or a step limit is reached.

2When would you use an agent instead of a fixed chain?Asked almost every time

Use a chain when the steps are known and always the same: it is cheaper, faster and more predictable. Use an agent when the steps depend on what is found along the way, such as research or troubleshooting. Many production systems are mostly fixed workflows with an agent only where flexibility is needed.

3How do you build a tool-calling agent with LangChain v1?Asked almost every time

Define tools with the @tool decorator (clear names, descriptions and typed arguments), choose a chat model that supports tool calling, and call create_agent(model, tools=[...], system_prompt=...). Invoke it with messages; it loops model call, tool execution and observation until the model answers. Add middleware for guardrails, summarisation or human approval.

4How do you write tool descriptions the model can use well?Asked almost every time

Name tools by action (search_orders), explain when to use and not use them, document each argument with type, format and examples, keep tools focused on one job, and return concise, structured results with clear error messages the model can act on.

5How do you limit cost and loops in an agent?Asked almost every time

Set a maximum number of iterations or a recursion limit, cap tokens per call, trim or summarise history, return compact tool outputs, use cheaper models for simple steps, and stop early when the answer is found.

6How do you make tool calling reliable?Very often asked

Clear tool names and descriptions, strict argument schemas, few tools per agent, validation and helpful error messages returned to the model so it can correct itself, and examples in the prompt. Then trace runs and evaluate tool choice on a dataset.

7What is agent middleware in LangChain v1?Very often asked

Hooks that run around model and tool calls in create_agent: before and after the model, wrapping tool calls, and modifying requests. Built-in middleware covers summarisation, human-in-the-loop approval, PII redaction and call limits; custom middleware adds your own policies.

8How do you get structured final output from an agent?Very often asked

Pass response_format (a Pydantic model or schema) to create_agent, so the agent's final answer is validated structured data, using provider-native structured output or a tool-based strategy.

9How do you handle tool errors?Very often asked

Catch exceptions in tools or with middleware and return a readable error message as the tool result so the model can retry with corrected arguments; retry transient failures in code; stop after repeated failures.

10How do you give an agent memory across turns?Very often asked

Pass a checkpointer and a thread_id so conversation state persists between invocations; for long conversations add summarisation middleware, and use a store for long-term facts across threads.

11How do you stream an agent's progress?Very often asked

Use agent.stream(..., stream_mode='updates') for step-by-step updates (model calls, tool calls, results) or 'messages' for tokens, so the UI can show what the agent is doing as it works.

12How do you test an agent?Very often asked

Unit-test tools directly, test the agent with a fake or recorded model for deterministic behaviour, and evaluate on a dataset of tasks in LangSmith checking final answers and tool-call trajectories, running each task several times.

13How many tools should an agent have?Very often asked

As few as needed: many similar tools confuse models and increase prompt size. Group related actions, use sub-agents or dynamic tool selection for large toolsets, and measure tool-choice accuracy as you add tools.

14Which built-in middleware would you use to make an agent resilient to provider outages and flaky tools?Very often asked

LangChain v1 ships provider-agnostic middleware for this: model retry and tool retry retry failed calls with exponential backoff, and model fallback switches to alternative models when the primary fails. Combine them with model call and tool call limits so retries cannot run up unbounded cost.

Sourced from Docs by LangChain (MCP)

15How do you keep an agent's conversation within the context window over a long session?Very often asked

Use the summarization middleware, which automatically summarises older conversation history when the token count approaches the limit, keeping recent messages verbatim. Persist the thread with a checkpointer so the summarised history carries across turns.

Sourced from Docs by LangChain (MCP)

16How do you run tools in parallel?Often asked

Models can return several tool calls in one response; the agent executes them (often concurrently) and returns all results before the next model call, speeding up independent lookups.

17How do you protect against prompt injection through tool results?Often asked

Treat tool outputs as untrusted data, keep instructions in the system prompt, restrict which tools can be called after reading external content, require approval for sensitive actions and filter outputs.

18How do you choose the model for an agent?Often asked

Pick one with reliable tool calling and enough context for your tools and history; test candidates on your task set for success rate, cost and latency, and consider routing easy requests to smaller models.

19What does the system prompt of an agent contain?Often asked

The agent's role, goals and constraints, when to use which tools, how to handle missing information, output style, and safety rules, kept stable for caching.

20How do you pass runtime context such as user ID to tools?Often asked

Use the runtime context or config passed at invocation, which tools can read, rather than asking the model to supply sensitive values as arguments.

21When would you switch from create_agent to a custom LangGraph graph?Often asked

When you need explicit branches, parallel steps, deterministic sub-workflows, complex human review or multi-agent coordination that the standard agent loop does not express well.

22How do you trace and debug an agent?Often asked

Enable LangSmith tracing to see each model call, tool call, arguments, results, tokens and latency in a run tree, then reproduce failures in the playground.

23What is dynamic model or tool selection?Often asked

Middleware can change the model or available tools per step based on state, for example switching to a stronger model for hard requests or exposing tools only after authentication.

24How do you stop an agent from leaking personal data to the model or in its output?Often asked

Add the PII detection middleware, which detects personally identifiable information and handles it with a chosen strategy such as redacting or masking it. Keep secrets like user IDs out of the prompt entirely by passing them through runtime context to tools.

Sourced from Docs by LangChain (MCP)

25When an agent has dozens of tools, how do you help the model pick the right ones?Often asked

Use the LLM tool selector middleware, which uses a model to select the relevant tools before the main model call, so the main call sees a short, focused list. Group related operations into fewer, well-described tools as well.

Sourced from Docs by LangChain (MCP)

26How can one agent change its behaviour across the steps of a support conversation?Often asked

Keep a step field in agent state, have tools update it by returning a Command with a state update, and write middleware that wraps each model call to swap the system prompt and the available tools based on the current step. This is the single-agent-with-middleware handoff pattern.

Sourced from Docs by LangChain (MCP)

27How does an agent use tools exposed by an MCP server?Often asked

Connect to the MCP server with the LangChain MCP adapters library, which discovers the server's tools and adapts them into LangChain tools that you pass to the agent like any other tool. Authentication and transport are configured on the client.

Sourced from Docs by LangChain (MCP)

28How do you return files or rich data from tools?Sometimes asked

Return references (URLs, IDs) or small structured summaries rather than large raw content, and let the application fetch full data for display.

29How do you deploy a LangChain agent?Sometimes asked

Serve it behind an API (FastAPI or LangGraph Platform/LangSmith Deployment), with persistent checkpointing, authentication, rate limits, tracing and evaluation in CI.

30What are the main metrics for an agent?Sometimes asked

Task success rate, tool-call accuracy, steps per task, latency, tokens and cost per task, escalation rate and user feedback.

31How do you keep agents safe in production?Sometimes asked

Least-privilege tools, approval for irreversible actions, input and output guardrails, iteration limits, monitoring and a kill switch.

32How would you give an agent a planning step for multi-part tasks?Sometimes asked

Add the to-do list middleware, which equips the agent with task planning and tracking: it writes a list of steps, works through them and updates their status, which keeps long tasks on track and makes progress visible to users.

Sourced from Docs by LangChain (MCP)

Objective questions 32

1In a tool-calling agent, who actually executes the tool?Asked almost every time
  1. AThe language model itself
  2. BYour application runtimeCorrect answer
  3. CThe vector store
  4. DThe user's browser always

Why: The model only outputs the tool name and arguments; your code runs the tool and returns the result.

2Which function creates an agent in LangChain v1?Asked almost every time
  1. Ainitialize_agent
  2. BAgentExecutor.run
  3. Cmake_agent
  4. Dcreate_agentCorrect answer

Why: It builds a tool-calling loop on LangGraph.

3What should a tool description explain?Asked almost every time
  1. AThe developer's name
  2. BThe server IP
  3. CNothing
  4. DWhat it does, when to use it and its argumentsCorrect answer

Why: The model chooses tools from it.

4Which parameter gives an agent structured final output?Asked almost every time
  1. Aresponse_formatCorrect answer
  2. Boutput_parser
  3. Cschema_mode
  4. Djson=True

Why: Validated against a schema.

5What stops an agent from looping forever?Very often asked
  1. AMore tools
  2. BA longer system prompt
  3. CA higher temperature
  4. DA maximum number of steps or a recursion limitCorrect answer

Why: A step or recursion limit ends the run if the agent has not finished.

6What persists an agent's conversation between calls?Very often asked
  1. AA bigger model
  2. BA checkpointer with a thread_idCorrect answer
  3. CA global variable
  4. DThe system prompt

Why: State is saved per thread.

7What should a failing tool return to the model?Very often asked
  1. ANothing
  2. BA crash
  3. CRandom data
  4. DA readable error messageCorrect answer

Why: The model can correct its arguments.

8Which stream mode shows each agent step?Very often asked
  1. Adebug only
  2. Bnone
  3. CupdatesCorrect answer
  4. Dvalues only

Why: Model calls, tool calls and results.

9Why limit the number of tools?Very often asked
  1. ATools are expensive to define
  2. BLangChain caps tools at three
  3. CIt is required by OpenAI
  4. DMany similar tools confuse the modelCorrect answer

Why: Measure tool-choice accuracy.

10How should sensitive values like user ID reach tools?Very often asked
  1. AIn the system prompt
  2. BHard-coded
  3. CThrough runtime context or configCorrect answer
  4. DAs model-generated arguments

Why: The model should not supply them.

11What does summarisation middleware do?Very often asked
  1. AShrinks long history to fit contextCorrect answer
  2. BSummarises tool code
  3. CWrites documentation
  4. DCompresses images

Why: Keeps long conversations within limits.

12Which middleware switches to another model when the primary fails?Very often asked
  1. AModel fallbackCorrect answer
  2. BSummarization
  3. CPII detection
  4. DTo-do list

Why: It lists alternative models to try in order.

Sourced from Docs by LangChain (MCP)

13Which middleware retries failed tool calls with exponential backoff?Very often asked
  1. ATool retryCorrect answer
  2. BTool call limit
  3. CLLM tool selector
  4. DHuman-in-the-loop

Why: Retries with backoff absorb transient tool failures.

Sourced from Docs by LangChain (MCP)

14What does the model read to choose a tool?Often asked
  1. AOnly the tool's source code
  2. BThe tool's name, description and argument schemaCorrect answer
  3. CThe tool's file path
  4. DNothing; it picks at random

Why: Tool metadata is sent to the model, so good descriptions directly improve tool choice.

15Tool outputs should be treated as…Often asked
  1. AAlways correct
  2. BSecrets
  3. CUntrusted dataCorrect answer
  4. DSystem instructions

Why: They may contain injections.

16When is a custom LangGraph graph better than create_agent?Often asked
  1. AFor every agent
  2. BNever
  3. CFor one tool only
  4. DWhen you need explicit branches and parallel stepsCorrect answer

Why: More control over flow.

17How are several independent tool calls handled?Often asked
  1. ASent to a human
  2. BExecuted together before the next model callCorrect answer
  3. COne per conversation
  4. DIgnored

Why: Parallel tool calls save time.

18Which tool shows an agent's run tree?Often asked
  1. APostman
  2. BFigma
  3. CLangSmithCorrect answer
  4. DWebpack

Why: Model and tool calls with timing.

19How do you test agents deterministically?Often asked
  1. AWith a fake or recorded modelCorrect answer
  2. BOnly in production
  3. CBy reading code
  4. DWith higher temperature

Why: Then evaluate with real models.

20Dynamic tool selection can…Often asked
  1. ADisable tracing
  2. BSkip the system prompt
  3. CExpose tools only after authenticationCorrect answer
  4. DRemove the model

Why: Middleware changes tools per step.

21What should tools return for large files?Often asked
  1. ANothing
  2. BReferences or small summariesCorrect answer
  3. CEntire raw content
  4. DBase64 blobs

Why: References save context space.

22Which metric reflects agent quality best?Often asked
  1. ALines of code
  2. BTask success rateCorrect answer
  3. CPrompt length
  4. DNumber of tools

Why: Pair with cost per task.

23What does the summarization middleware do near the token limit?Often asked
  1. ADeletes the system prompt
  2. BStops the agent
  3. CSwitches to a larger model
  4. DSummarises older conversation historyCorrect answer

Why: Recent messages stay; older ones are condensed.

Sourced from Docs by LangChain (MCP)

24The LLM tool selector middleware…Often asked
  1. ADisables tools
  2. BRetries tools
  3. CUses a model to pick relevant tools before the main callCorrect answer
  4. DWrites new tools

Why: The main call then sees a shorter tool list.

Sourced from Docs by LangChain (MCP)

25Which library turns MCP server tools into LangChain tools?Often asked
  1. ALangflow
  2. BThe checkpointer
  3. CThe LangChain MCP adaptersCorrect answer
  4. DLangSmith

Why: Adapters discover and wrap the server's tools.

Sourced from Docs by LangChain (MCP)

26In the single-agent handoff pattern, a tool triggers a step change by…Often asked
  1. ARaising an exception
  2. BCalling another agent's API
  3. CEditing the system prompt file
  4. DReturning a Command that updates stateCorrect answer

Why: Middleware then reads the new step and swaps prompt and tools.

Sourced from Docs by LangChain (MCP)

27Which middleware asks a person before sensitive actions?Sometimes asked
  1. ASummarisation
  2. BRetry
  3. CLogging
  4. DHuman-in-the-loop middlewareCorrect answer

Why: Approval before execution.

28A runaway agent loop is best capped by…Sometimes asked
  1. AA higher temperature
  2. BMore tools
  3. CA recursion or iteration limitCorrect answer
  4. DA longer prompt

Why: Hard caps bound the number of steps.

29Which decorator defines a tool?Sometimes asked
  1. A@function
  2. B@toolCorrect answer
  3. C@agent
  4. D@chain

Why: Docstring becomes the description.

30Where can an agent be served?Sometimes asked
  1. AIn a spreadsheet
  2. BBehind an API with checkpointing and authCorrect answer
  3. COnly in a notebook
  4. DOnly in the browser

Why: Production needs persistence.

31Routing easy requests to smaller models…Sometimes asked
  1. AIs impossible
  2. BDisables tracing
  3. CCuts cost and latencyCorrect answer
  4. DBreaks tool calling

Why: Keep strong models for hard tasks.

32Which middleware gives an agent planning and progress tracking?Sometimes asked
  1. AModel retry
  2. BTo-do listCorrect answer
  3. CModel call limit
  4. DPII detection

Why: It maintains a list of steps and their status.

Sourced from Docs by LangChain (MCP)

Track your progress through every blueprint in your dashboard.

Start learning