*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to attach runtime arguments to a Runnable

On this page

How to attach runtime arguments to a Runnable
=============================================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [LangChain Expression Language (LCEL)](/v0.2/docs/concepts/#langchain-expression-language)
*   [Chaining runnables](/v0.2/docs/how_to/sequence/)
*   [Tool calling](/v0.2/docs/how_to/tool_calling/)

Sometimes we want to invoke a [`Runnable`](https://v02.api.js.langchain.com/classes/langchain_core_runnables.Runnable.html) within a [RunnableSequence](https://v02.api.js.langchain.com/classes/langchain_core_runnables.RunnableSequence.html) with constant arguments that are not part of the output of the preceding Runnable in the sequence, and which are not part of the user input. We can use the [`Runnable.bind()`](https://v02.api.js.langchain.com/classes/langchain_core_runnables.Runnable.html#bind) method to set these arguments ahead of time.

Binding stop sequences[​](#binding-stop-sequences "Direct link to Binding stop sequences")
------------------------------------------------------------------------------------------

Suppose we have a simple prompt + model chain:

tip

See [this section for general instructions on installing integration packages](/v0.2/docs/how_to/installation#installing-integration-packages).

*   npm
*   yarn
*   pnpm

    npm i @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { StringOutputParser } from "@langchain/core/output_parsers";import { ChatPromptTemplate } from "@langchain/core/prompts";import { ChatOpenAI } from "@langchain/openai";const prompt = ChatPromptTemplate.fromMessages([  [    "system",    "Write out the following equation using algebraic symbols then solve it. Use the format\n\nEQUATION:...\nSOLUTION:...\n\n",  ],  ["human", "{equation_statement}"],]);const model = new ChatOpenAI({ temperature: 0 });const runnable = prompt.pipe(model).pipe(new StringOutputParser());const res = await runnable.invoke({  equation_statement: "x raised to the third plus seven equals 12",});console.log(res);

    EQUATION: x^3 + 7 = 12SOLUTION:Subtract 7 from both sides:x^3 = 5Take the cube root of both sides:x = ∛5

and want to call the model with certain `stop` words so that we shorten the output, which is useful in certain types of prompting techniques. While we can pass some arguments into the constructor, other runtime args use the `.bind()` method as follows:

    const runnable = prompt  .pipe(model.bind({ stop: "SOLUTION" }))  .pipe(new StringOutputParser());const res = await runnable.invoke({  equation_statement: "x raised to the third plus seven equals 12",});console.log(res);

    EQUATION: x^3 + 7 = 12

What you can bind to a Runnable will depend on the extra parameters you can pass when invoking it.

Attaching OpenAI tools[​](#attaching-openai-tools "Direct link to Attaching OpenAI tools")
------------------------------------------------------------------------------------------

Another common use-case is tool calling. While you should generally use the [`.bind_tools()`](/v0.2/docs/how_to/tool_calling/) method for tool-calling models, you can also bind provider-specific args directly if you want lower level control:

    const tools = [  {    type: "function",    function: {      name: "get_current_weather",      description: "Get the current weather in a given location",      parameters: {        type: "object",        properties: {          location: {            type: "string",            description: "The city and state, e.g. San Francisco, CA",          },          unit: { type: "string", enum: ["celsius", "fahrenheit"] },        },        required: ["location"],      },    },  },];const model = new ChatOpenAI({ model: "gpt-4o" }).bind({ tools });await model.invoke("What's the weather in SF, NYC and LA?");

    AIMessage {  lc_serializable: true,  lc_kwargs: {    content: "",    tool_calls: [      {        name: "get_current_weather",        args: { location: "San Francisco, CA" },        id: "call_iDKz4zU8PKBaaIT052fJkMMF"      },      {        name: "get_current_weather",        args: { location: "New York, NY" },        id: "call_niQwZDOqO6OJTBiDBFG8FODc"      },      {        name: "get_current_weather",        args: { location: "Los Angeles, CA" },        id: "call_zLXH2cDVQy0nAVC0ViWuEP4m"      }    ],    invalid_tool_calls: [],    additional_kwargs: {      function_call: undefined,      tool_calls: [        {          id: "call_iDKz4zU8PKBaaIT052fJkMMF",          type: "function",          function: [Object]        },        {          id: "call_niQwZDOqO6OJTBiDBFG8FODc",          type: "function",          function: [Object]        },        {          id: "call_zLXH2cDVQy0nAVC0ViWuEP4m",          type: "function",          function: [Object]        }      ]    },    response_metadata: {}  },  lc_namespace: [ "langchain_core", "messages" ],  content: "",  name: undefined,  additional_kwargs: {    function_call: undefined,    tool_calls: [      {        id: "call_iDKz4zU8PKBaaIT052fJkMMF",        type: "function",        function: {          name: "get_current_weather",          arguments: '{"location": "San Francisco, CA"}'        }      },      {        id: "call_niQwZDOqO6OJTBiDBFG8FODc",        type: "function",        function: {          name: "get_current_weather",          arguments: '{"location": "New York, NY"}'        }      },      {        id: "call_zLXH2cDVQy0nAVC0ViWuEP4m",        type: "function",        function: {          name: "get_current_weather",          arguments: '{"location": "Los Angeles, CA"}'        }      }    ]  },  response_metadata: {    tokenUsage: { completionTokens: 70, promptTokens: 82, totalTokens: 152 },    finish_reason: "tool_calls"  },  tool_calls: [    {      name: "get_current_weather",      args: { location: "San Francisco, CA" },      id: "call_iDKz4zU8PKBaaIT052fJkMMF"    },    {      name: "get_current_weather",      args: { location: "New York, NY" },      id: "call_niQwZDOqO6OJTBiDBFG8FODc"    },    {      name: "get_current_weather",      args: { location: "Los Angeles, CA" },      id: "call_zLXH2cDVQy0nAVC0ViWuEP4m"    }  ],  invalid_tool_calls: []}

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You now know how to bind runtime arguments to a Runnable.

Next, you might be interested in our how-to guides on [passing data through a chain](/v0.2/docs/how_to/passthrough/).

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to add values to a chain's state

](/v0.2/docs/how_to/assign)[

Next

How to cache embedding results

](/v0.2/docs/how_to/caching_embeddings)

*   [Binding stop sequences](#binding-stop-sequences)
*   [Attaching OpenAI tools](#attaching-openai-tools)
*   [Next steps](#next-steps)