Questions
7 of 24
1What is a Tool in LangChain and how does it differ from a plain function or API call?
2What is the difference between the tool() helper, DynamicTool, and StructuredTool class?
3How does an LLM decide which tool to call — what role does the tool description play?
4What is the role of Zod schema in tool definitions and how does it map to OpenAI's function calling spec?
5What is a ToolNode in LangGraph and how does it differ from calling a tool manually inside a graph node?
6How do you wrap a REST API call with auth headers inside a Tool in TypeScript?
7How do you handle async errors and retries inside a Tool without crashing the agent loop?
8How do you pass runtime context (userId, authToken, DB connection) into a Tool using RunnableConfig?
9How do you build a Toolkit (grouped set of related tools) using BaseToolkit?
10How do you validate and sanitize tool output before it is passed back to the LLM?
11How do you stream tool call results back to the client in real time?
12How do you implement tool-level authorization — allowing certain tools only for certain users?
13How do you build stateful tools that read/write to a database across multiple agent turns?
14How do you prevent tool abuse or infinite loops where an agent keeps calling the same tool repeatedly?
15How do you implement parallel tool calling — when the LLM decides to call multiple tools simultaneously?
16How do you create a human-in-the-loop tool that pauses the agent and waits for user approval before executing?
17How do you unit test and mock tools in isolation without invoking the LLM?
18How do you implement tool call caching to avoid redundant API calls for identical inputs?
19How do you design a multi-agent system where one agent's tool is actually another agent (agent-as-tool pattern)?
20How does LangGraph's ToolNode handle tool call errors and surface them back into the message state?
21What is the difference between tool_choice: "auto", "required", and "none" when binding tools to an LLM?
22How do you implement dynamic tool loading — where the set of available tools changes based on user role or session state?
23How do you trace and observe tool call latency in production using LangSmith?
24What are the token cost implications of registering too many tools and how do you mitigate it?
07 / 24

How do you handle async errors and retries inside a Tool without crashing the agent loop?

Handle async errors and retries inside a Tool by using structured error returns, implementing retry logic with exponential backoff, classifying error types, and leveraging LangChain's built-in retry utilities and middleware to keep the agent loop running and allow the LLM to recover.

To prevent a tool error from crashing the entire agent loop, you must treat errors as recoverable events rather than fatal exceptions. The core pattern is to return structured error information to the agent instead of throwing exceptions. This allows the LLM to see what went wrong and attempt to correct its approach. You can then layer on retry logic for transient failures, ensuring that the tool execution is resilient without breaking the agent's reasoning flow .

Basic Pattern: Return Structured Errors vs. Throw

For transient failures like network timeouts or rate limits, you can implement retry logic directly inside the tool function. Use exponential backoff to avoid overwhelming the failing service. This approach keeps the retry logic encapsulated within the tool, so the agent loop sees only the final success or a well-formatted error after retries are exhausted .

Tool with Built-in Exponential Backoff

LangChain provides built-in mechanisms for adding retry logic to any Runnable, including tools. The .with_retry() method can be applied to a tool instance, allowing you to specify which exceptions should trigger a retry, the maximum number of attempts, and exponential backoff with jitter. This approach keeps your tool implementation clean while still providing resilience .

Using .with_retry() on a Tool

For more sophisticated recovery scenarios, you can use the ToolRecoverMiddleware from the langchain-tool-recover package. This middleware classifies errors into categories (timeout, rate_limit, validation_error, empty_result, etc.) and applies configurable recovery actions. When a tool fails or returns an empty result, the middleware intercepts the error and returns a structured JSON message to the agent, allowing the LLM to reason about the failure and adjust its strategy .

Using ToolRecoverMiddleware

In some cases, a tool may need to determine that the agent loop should end early, even if the agent hasn't reached its final answer. For example, after a successful verification step, you might want to skip further tool calls and terminate. This can be achieved by having the tool return a Command object with a jump_to instruction. This feature is currently experimental and discussed in LangChain's feature requests, but it provides a powerful way to dynamically control the agent's execution flow .

Early Termination from a Tool (Experimental)

Production-ready error handling requires classifying errors into appropriate categories and applying suitable recovery strategies. Classification should be deterministic and conservative. Tools like ToolRecoverMiddleware implement this classification automatically, but you can also implement your own logic. Common error classes include timeout, rate_limit, validation_error, auth_error, empty_result, and unknown_error. Each class should map to a specific recovery action: retry with backoff for transient errors, return to agent for validation issues, or fail fast for authentication errors .

Error Classification and Recovery Matrix
  1. 1

    timeout or rate_limit: Retry with exponential backoff — These are transient and often succeed on retry

  2. 2

    validation_error: Return to agent with structured message — The agent may correct its input format

  3. 3

    empty_result: Return to agent with suggestion — The agent may adjust its query

  4. 4

    auth_error: Fail fast (do not retry) — Retrying won't fix authentication issues

  5. 5

    unknown_error: Log and fail fast, or return to agent for recovery — Depends on error criticality

The LangGraph team is also exploring built-in reliability features for create_react_agent, including error_handling configuration that would automatically classify errors and apply appropriate retry strategies. This would further simplify production deployments by providing battle-tested defaults for common failure modes .