{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to pass runtime values to tools\n", "\n", "This guide shows how to define tools that depend on dynamically defined variables. These values are provided by your program, not by the LLM.\n", "\n", "Tools can access the [config.configurable](https://langchain-ai.github.io/langgraphjs/reference/interfaces/langgraph.LangGraphRunnableConfig.html) field for values like user IDs that are known when a graph is initially executed, as well as managed values from the [store](https://langchain-ai.github.io/langgraphjs/reference/classes/checkpoint.BaseStore.html) for persistence across threads.\n", "\n", "However, it can be convenient to access intermediate runtime values which are not known ahead of time, but are progressively generated as a graph executes, such as the current graph state. This guide will cover two techniques for this: context variables and closures.\n", "\n", "## Setup\n", "\n", "Install the following to run this guide:\n", "\n", "```bash\n", "npm install @langchain/langgraph @langchain/openai @langchain/core\n", "```\n", "\n", "Next, configure your environment to connect to your model provider.\n", "\n", "```bash\n", "export OPENAI_API_KEY=your-api-key\n", "```\n", "\n", "Optionally, set your API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability.\n", "\n", "```bash\n", "export LANGCHAIN_TRACING_V2=\"true\"\n", "export LANGCHAIN_CALLBACKS_BACKGROUND=\"true\"\n", "export LANGCHAIN_API_KEY=your-api-key\n", "```\n", "\n", "## Context variables\n", "\n", "[Context variables](https://js.langchain.com/docs/how_to/tool_runtime#using-context-variables) are a powerful feature that allows you to set values at one level of your application, then access them within any child runnables (such as tools) nested within.\n", "\n", "They are convenient in that you don’t need to have a direct reference to the declared variable to access it from a child, just a string with the variable name.\n", "\n", "
Compatibility
\n", "\n",
" This functionality was added in @langchain/core>=0.3.10
. If you are using the LangSmith SDK separately in your project, we also recommend upgrading to langsmith>=0.1.65
. For help upgrading, see this guide.\n",
"
\n",
" It also requires async_hooks
support, which is supported in many popular JavaScript environments (such as Node.js, Deno, and Cloudflare Workers), but not all of them (mainly web browsers).\n",
"