CTCaliberTek
Course: LangChain: Step by Step (9 Modules) · Lesson 4 of 9Log in to track your progress and earn a certificate when you finish the course.
AI & Machine Learning

LangChain Module 4: Tools and Your First Agent with create_agent

Published October 4, 2026·Updated October 29, 2026·9 min read
Like SaveLog in to like, save, and track progress.

Introduction

A tool lets a model do things: fetch data, run a calculation, call an API. An agent is a model that decides when to use tools until it can answer. In LangChain v1 you build both with a few lines. This module creates a small agent and shows exactly what happens inside it.

Prerequisites

  • Modules 1 and 2 completed

Step-by-Step Guide

Step 1: Define tools with @tool

Decorate a function with @tool. The type hints are required, because they become the tool's input schema, and the docstring becomes its description, which the model reads to decide when to use it. Write the docstring for the model.

from langchain.tools import tool

@tool
def get_word_count(text: str) -> int:
    """Count the number of words in a piece of text."""
    return len(text.split())

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    # A real tool would call a weather API here
    return "It is always sunny in " + city + "!"

Step 2: Create the agent

Pass a model, a list of tools, and a system prompt to create_agent. The system prompt argument is called system_prompt. You can pass a model object created with init_chat_model, or a provider:model string.

from langchain.agents import create_agent
from langchain.chat_models import init_chat_model

model = init_chat_model("anthropic:claude-opus-5-5", max_tokens=16000)

agent = create_agent(
    model=model,
    tools=[get_word_count, get_weather],
    system_prompt="You are a helpful assistant. Use tools when they help.",
)

Step 3: Invoke it and read the answer

Agents take a dictionary with a messages list and return a dictionary with the full message history. The last message is the final answer.

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What's the weather in Paris?"}]}
)
print(result["messages"][-1].text)

Step 4: Inspect what the agent did

Print every message to see the loop: the user's question, an AI message that requests a tool call, the tool's result, and the final AI answer. Reading this trace is the best way to debug an agent.

for message in result["messages"]:
    message.pretty_print()

Step 5: Design good tools

Give each tool one clear purpose and a descriptive snake_case name. Return concise results. Two argument names are reserved, config and runtime; use a runtime parameter of type ToolRuntime when a tool needs access to agent state or context. Prefer plain, safe operations and validate inputs, since the model chooses the arguments.

FAQs

What happened to AgentExecutor and initialize_agent?
create_agent replaces the older agent-building helpers. Legacy functionality lives in langchain-classic, but new projects should start with create_agent.
How many times will the agent loop?
Until the model answers without requesting another tool call. You can limit the number of model or tool calls with middleware, covered in Module 8.

Summary

Write tools as typed, documented functions, pass them to create_agent with a model and a system prompt, and read the message trace to understand and debug behavior.

Related tutorials

Comments

Log in or create a free account to join the discussion.

  • No comments yet. Be the first to comment.