Build ยท 8 min read

Agents and tools

Let the model choose which function to call, and when.


A tool is a plain function with a described signature. An agent is a loop that lets the model pick a tool, read the result, and decide whether it is finished.

Define a tool

python
from langchain_core.tools import tool

@tool
def get_order_status(order_id: str) -> str:
    """Look up the current status of a customer order."""
    return database.fetch_status(order_id)

Bind the tools to a model

python
model_with_tools = model.bind_tools([get_order_status])
response = model_with_tools.invoke("Where is order A-1183?")
print(response.tool_calls)

Keep the loop bounded

  • Cap the number of iterations so a confused agent cannot spin forever.
  • Write docstrings for humans; the model reads them as the tool spec.
  • Return structured errors so the agent can recover instead of guessing.