LangChain Tools and Tool Calling
LangChain tools let a model do more than write text. A tool is a Python function with a name, description and input schema. With tool calling, the model decides which tool to use and with what arguments, and your code runs the function.
Creating a Tool with @tool
from langchain.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
fake = {"lahore": "34 C, sunny", "london": "12 C, rain"}
return fake.get(city.lower(), "unknown")
print(get_weather.name)
print(get_weather.description)
print(get_weather.invoke({"city": "Lahore"}))get_weather Get the current weather for a city. 34 C, sunny
The function name becomes the tool name, the docstring becomes the description, and type hints become the argument schema. Good descriptions matter: the model reads them to decide when to call the tool.
Binding Tools to a Model
from langchain_ollama import ChatOllama
llm = ChatOllama(model="llama3.2", temperature=0)
llm_with_tools = llm.bind_tools([get_weather])
msg = llm_with_tools.invoke("Is it raining in London?")
print(msg.tool_calls)Sample output:
[{'name': 'get_weather', 'args': {'city': 'London'}, 'id': 'call_1', 'type': 'tool_call'}]The model did not answer directly. It returned a tool call request. The model never runs code itself; your program does.
Completing the Tool Loop
from langchain_core.messages import HumanMessage
messages = [HumanMessage("Is it raining in London?")]
ai = llm_with_tools.invoke(messages)
messages.append(ai)
for call in ai.tool_calls:
messages.append(get_weather.invoke(call)) # returns a ToolMessage
final = llm_with_tools.invoke(messages)
print(final.content)Sample output: Yes, it's raining in London right now, about 12 C.
Invoking a tool with the whole tool call dictionary returns a ToolMessage linked by ID. Sending it back lets the model write the final answer. Agents (next lesson) automate this loop.
Tool Design Tips
- Give each tool one clear job and a specific docstring.
- Use simple argument types; add Field descriptions with an args_schema for complex inputs.
- Return short strings or JSON; long outputs waste context.
- Validate inputs and never let tools run dangerous actions without checks.
- Pick a model that supports tool calling (llama3.2, qwen3, mistral and similar on Ollama).
Frequently asked questions
Does Ollama support tool calling in LangChain?
Yes, ChatOllama supports bind_tools for models that have tool calling capability, such as llama3.1/3.2, qwen3 and mistral.
Where is the @tool decorator in LangChain 1.0?
Import it with from langchain.tools import tool. It is also available from langchain_core.tools.
Can the model call several tools at once?
Yes, many models return multiple tool_calls in one message (parallel tool calling). Run each and send back all ToolMessages.