Questions
10 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?
10 / 24

How do you validate and sanitize tool output before it is passed back to the LLM?

Validate and sanitize tool output by applying deterministic sanitization (e.g., PII redaction), structured output validation using schemas and retry mechanisms, and safety filtering through middleware to prevent harmful or malformed responses from reaching the LLM.

LangChain provides built-in middleware and custom hooks to intercept and sanitize tool results before they are sent back to the LLM. This is the most structured approach for compliance and safety tasks.

Sanitizing Tool Output with PII Redaction Middleware

For custom validation, you can create middleware that runs after a tool executes. This allows you to inspect, modify, or block tool outputs before they are added to the conversation state.

Custom Tool Output Validation Middleware

The most reliable way to ensure tool output is safe and usable is to enforce a strict schema. Use the LLM's native structured output capabilities (e.g., JSON mode or tool calling) to guarantee that the tool’s response conforms to a predictable format.

Enforcing Structured Tool Output with Zod Schema

When a tool returns malformed output that fails validation, implement a fallback using LangChain's RetryOutputParser or a custom retry loop. This allows the system to clean the output or ask for a retry rather than crashing the agent loop.

Implementing a Retry Mechanism for Failed Parsing

Tools can return excessively long outputs that exceed the LLM's context window. To prevent crashes and reduce costs, you should either truncate the output or implement summarizing middleware that compresses the tool result before passing it back.

Implementing Truncation Logic

Tool outputs must be sanitized to prevent injection attacks. The official LangChain documentation warns against the use of eval() on untrusted output, which can lead to Remote Code Execution (RCE) vulnerabilities. Always use safe parsers and validate the structure of the output instead of directly executing it.

Example: Refactoring Insecure Code

While we are validating output, it is also important to ensure the tool receives the correct input. Define a Pydantic args_schema for the tool. This not only validates the LLM-generated arguments but also sanitizes them before they reach your business logic.

Defining a Robust Input Schema