Home / Class/ LLMToolEmulator Class — langchain Architecture

LLMToolEmulator Class — langchain Architecture

Architecture documentation for the LLMToolEmulator class in tool_emulator.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a4266996_914c_18fd_7063_10ef49e72ec1["LLMToolEmulator"]
  48aa29b8_65e7_522f_a445_a441eeb6baff["BaseChatModel"]
  a4266996_914c_18fd_7063_10ef49e72ec1 -->|extends| 48aa29b8_65e7_522f_a445_a441eeb6baff
  9d440466_e21c_2c68_e5d1_992787a1d0d9["tool_emulator.py"]
  a4266996_914c_18fd_7063_10ef49e72ec1 -->|defined in| 9d440466_e21c_2c68_e5d1_992787a1d0d9
  68fec0a7_58ea_3669_5538_184cad824113["__init__()"]
  a4266996_914c_18fd_7063_10ef49e72ec1 -->|method| 68fec0a7_58ea_3669_5538_184cad824113
  4044e8b7_30b9_dc69_5caa_63ae699529ab["wrap_tool_call()"]
  a4266996_914c_18fd_7063_10ef49e72ec1 -->|method| 4044e8b7_30b9_dc69_5caa_63ae699529ab
  ab9fc9f6_33ca_e973_875f_7e92eb02be7a["awrap_tool_call()"]
  a4266996_914c_18fd_7063_10ef49e72ec1 -->|method| ab9fc9f6_33ca_e973_875f_7e92eb02be7a

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/tool_emulator.py lines 22–209

class LLMToolEmulator(AgentMiddleware[AgentState[Any], ContextT], Generic[ContextT]):
    """Emulates specified tools using an LLM instead of executing them.

    This middleware allows selective emulation of tools for testing purposes.

    By default (when `tools=None`), all tools are emulated. You can specify which
    tools to emulate by passing a list of tool names or `BaseTool` instances.

    Examples:
        !!! example "Emulate all tools (default behavior)"

            ```python
            from langchain.agents.middleware import LLMToolEmulator

            middleware = LLMToolEmulator()

            agent = create_agent(
                model="openai:gpt-4o",
                tools=[get_weather, get_user_location, calculator],
                middleware=[middleware],
            )
            ```

        !!! example "Emulate specific tools by name"

            ```python
            middleware = LLMToolEmulator(tools=["get_weather", "get_user_location"])
            ```

        !!! example "Use a custom model for emulation"

            ```python
            middleware = LLMToolEmulator(
                tools=["get_weather"], model="anthropic:claude-sonnet-4-5-20250929"
            )
            ```

        !!! example "Emulate specific tools by passing tool instances"

            ```python
            middleware = LLMToolEmulator(tools=[get_weather, get_user_location])
            ```
    """

    def __init__(
        self,
        *,
        tools: list[str | BaseTool] | None = None,
        model: str | BaseChatModel | None = None,
    ) -> None:
        """Initialize the tool emulator.

        Args:
            tools: List of tool names (`str`) or `BaseTool` instances to emulate.

                If `None`, ALL tools will be emulated.

                If empty list, no tools will be emulated.
            model: Model to use for emulation.

                Defaults to `'anthropic:claude-sonnet-4-5-20250929'`.

                Can be a model identifier string or `BaseChatModel` instance.
        """
        super().__init__()

        # Extract tool names from tools
        # None means emulate all tools
        self.emulate_all = tools is None
        self.tools_to_emulate: set[str] = set()

        if not self.emulate_all and tools is not None:
            for tool in tools:
                if isinstance(tool, str):
                    self.tools_to_emulate.add(tool)
                else:
                    # Assume BaseTool with .name attribute
                    self.tools_to_emulate.add(tool.name)

        # Initialize emulator model
        if model is None:

Extends

Frequently Asked Questions

What is the LLMToolEmulator class?
LLMToolEmulator is a class in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/tool_emulator.py.
Where is LLMToolEmulator defined?
LLMToolEmulator is defined in libs/langchain_v1/langchain/agents/middleware/tool_emulator.py at line 22.
What does LLMToolEmulator extend?
LLMToolEmulator extends BaseChatModel.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free