AI Hints
Home›LangChain Tutorial›Tool Calls by Hand
LangChain · Lesson 40 of 120

Binding Tools

Bind_tools.

⏱ 2 min read▶ Video at 43:46🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

To let a model use tools, you bind them to it. Bind tools returns a version of the model that knows the tools exist, and can choose to call them.

Binding Tools
From the video: Binding Tools
  • Bind the tools
  • The model knows they exist
  • It may call them

A model with tools

We bind the check stock tool to the model. Then we ask two questions. One needs the tool, and one doesn't. Run it. For the stock question, the reply text is empty, and there's a tool call instead. For the greeting, there's normal text, and no tool calls. The model decides, every time.

bind.pypython
from langchain.tools import tool
from langchain.chat_models import init_chat_model

@tool
def check_stock(title: str) -> int:
    """Return how many copies of a book Riverside Books has in stock."""
    return {"Dune": 3}.get(title, 0)

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
model_with_tools = model.bind_tools([check_stock])

for q in ["How many copies of Dune do you have?", "Say hi in three words."]:
    reply = model_with_tools.invoke(q)
    print(f"text={reply.text!r}  tool_calls={len(reply.tool_calls)}")
Output
text=''  tool_calls=1
text='Hello, friend. How are you?'  tool_calls=0

Controlling tool choice

Usually the model decides freely. But bind tools has a tool choice option. You can force it to call a particular tool, or require it to call some tool. This is useful when you know a tool must be used, like always looking up the order before answering.

keyvalue
Defaultmodel decides
tool_choice='check_stock'force one tool
tool_choice='any'must call some tool
Use whena lookup is required

Summary

Bind tools gives a model access to tools, and it decides when to call them. Tool choice can force a call. Next, reading what the model asked for.

Key takeaways
  • model.bind_tools([...])
  • Model decides
  • tool_choice to force
Next up: Reading Tool Calls — what the model asked for