See Tutorial-01 to get rolling. Then open directory 02-tools-in-more-detail and select file tutorial-02.ipynb.
Execute the notebook to make sure all is running before modifying.
Meet the @tool decorator
The @tool decorator applied to a function automatically generates the necessary schema and metadata an agent needs to understand and use the function.
Add more import lines below the create_agent function import
from dataclasses import dataclass
from langchain.tools import tool, ToolRuntime
Add the @tool decorator right above the the get_warehouse_status function:
@tool
def get_warehouse_status(loc: str) -> str:
...
Add class and function below get_warehouse_status function:
@dataclass
class Context:
"""Custom runtime context schema."""
user_id: str
@tool
def get_user_location(runtime: ToolRuntime[Context]) -> str:
"""Retrieve information based on user ID."""
user_id = runtime.context.user_id
return "main office" if user_id == "1" else "break room"
Note we added a class named Context above and another function (tool) get_worker_location. The Context class is used to pass a runtime context when invoking the agent.
Now modify the agent create_agent and invoke function to look like this:
agent = create_agent(
model="claude-haiku-4-5-20251001",
tools=[get_warehouse_status, get_user_location],
system_prompt=SYSTEM_PROMPT
)
agent.invoke(
{"messages": [{"role": "user", "content": "What is the status of the warehouse in Milford, PA?"}]},
context=Context(user_id="2"),
)
You should now be able to run the notebook if all is correct. Check the final file provided for comparison.
The reponse context should be something like this:
“I see that you’re currently in the break room. I’m not able to provide warehouse status information to employees while they’re on a break. Please return to the warehouse floor and then I’ll be happy to help you with the Milford, PA warehouse status.”
This is because we invoked the agent with a context of user_id=”2″. Let’s change that to user_id=”1″ and see what happens. It seems now the agent gladly gives you the status of the warehouse. The agent just doesn’t like people trying to do work in the break room.
I think you’re starting to see how useful these agents can be in making decisions for us.
Tips
Don’t ever upload your .env file to a github repository. Your secret key will no longer be a secret. I added .env to the .gitignore file.
Play around and add another function (tool) that does something else.