RunnableConfig for the current execution.
Runtime context (from langgraph Runtime).
The current graph state.
BaseStore instance for persistent storage (from langgraph Runtime).
OptionaltoolCall?: ToolCallThe current tool call.
The ID of the current tool call.
Stream writer for streaming output (from langgraph Runtime).
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,
}
);
Runtime context automatically injected into tools.
When a tool function has a parameter named
tool_runtimewith type hintToolRuntime, the tool execution system will automatically inject an instance containing:state: The current graph statetoolCallId: The ID of the current tool callconfig:RunnableConfigfor the current executioncontext: Runtime contextstore:BaseStoreinstance for persistent storagewriter: Stream writer for streaming outputNo
Annotatedwrapper is needed - just useruntime: ToolRuntimeas a parameter.