1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
37 small wins to finish your pathNext lesson →
What a tool actually is
A tool is a Python function with a docstring. The decorator reads your function and writes the description a model needs.
from langchain_core.tools import tool
@tool
def lookup_order(order_id: str) -> str:
"""Look up the status of an order by its id."""
return f"Order {order_id} shipped on 3 March."
print("name: ", lookup_order.name)
print("description:", lookup_order.description)
print("args: ", lookup_order.args)
print("calling it: ", lookup_order.invoke({"order_id": "A17"}))Where each piece came from
| What the model sees | Where you wrote it |
|---|---|
| the name | the function name |
| the description | the docstring |
| the arguments and their types | the type hints on the parameters |
Nothing was invented. Every part of that description is something you already wrote for the benefit of the next human to read the code, and the decorator collected it.
The last line is worth noticing too. A tool is still an ordinary function and you can call it yourself. Nothing about it requires a model to be present.
Why the docstring matters more than you think
The model never sees the body of your function. It sees the name, the description and the argument list, and it decides from those three things alone whether this tool is the right one to use.
Write the docstring for the model
A docstring that says gets data will get the tool called at random, or never. Say what it does, and say when it should be used. This is the single highest value editing you can do to an agent that is misbehaving.
Try it yourself
- Delete the docstring and print the description again.
- Add a second argument with a type hint, and look at
args. - Change the type hint from
strtointand see what the argument type becomes.
Little by little, you're building something great.