langchain.js
    Preparing search index...

    Type Alias ToolRuntime<TState, TContext>

    ToolRuntime: RunnableConfig & {
        config: ToolRunnableConfig;
        context: TContext extends InteropZodObject
            ? InferInteropZodOutput<TContext>
            : TContext extends Record<string, unknown> ? TContext : unknown;
        state: TState extends InteropZodObject
            ? InferInteropZodOutput<TState>
            : TState extends Record<string, unknown> ? TState : unknown;
        store: BaseStore<string, unknown> | null;
        toolCall?: ToolCall;
        toolCallId: string;
        writer: ((chunk: unknown) => void) | null;
    }

    Runtime context automatically injected into tools.

    When a tool function has a parameter named tool_runtime with type hint ToolRuntime, the tool execution system will automatically inject an instance containing:

    • state: The current graph state
    • toolCallId: The ID of the current tool call
    • config: RunnableConfig for the current execution
    • context: Runtime context
    • store: BaseStore instance for persistent storage
    • writer: Stream writer for streaming output

    No Annotated wrapper is needed - just use runtime: ToolRuntime as a parameter.

    Type Parameters

    • TState = unknown
    • TContext = unknown

    Type Declaration

    import { tool, ToolRuntime } from "@langchain/core/tools";
    import { z } from "zod";

    const stateSchema = z.object({
    messages: z.array(z.any()),
    userId: z.string().optional(),
    });

    const greet = tool(
    async ({ name }, runtime) => {
    // Access state
    const messages = runtime.state.messages;

    // Access tool_call_id
    console.log(`Tool call ID: ${runtime.toolCallId}`);

    // Access config
    console.log(`Run ID: ${runtime.config.runId}`);

    // Access runtime context
    const userId = runtime.context?.userId;

    // Access store
    await runtime.store?.mset([["key", "value"]]);

    // Stream output
    runtime.writer?.("Processing...");

    return `Hello! User ID: ${runtime.state.userId || "unknown"} ${name}`;
    },
    {
    name: "greet",
    description: "Use this to greet the user once you found their info.",
    schema: z.object({ name: z.string() }),
    stateSchema,
    }
    );

    The type of the state schema (inferred from stateSchema)

    The type of the context schema (inferred from contextSchema)