LangChain Tools: What They Are, How They Work, and How to Use Them in AI Agents
Giovanni Romerogiovanniromero.dev
Comments (0)
Views (30)

LangChain Tools: What They Are, How They Work, and How to Use Them in AI Agents

Introduction

LangChain tools are what allow a language model to go beyond text generation and become an actionable system. Instead of only responding with natural language, an LLM can use tools to fetch data, execute code, interact with APIs, update internal state, and work with persistent memory.

If you are learning LangChain seriously or building AI agents for real applications, understanding tools is essential. This guide explains what LangChain tools are, how they work internally, how agents use them, and how to design them correctly for production, including a complete practical code example.


What Are LangChain Tools?

In LangChain, a tool is a callable function that a language model can invoke using a structured schema. The model decides when to use the tool and what arguments to pass based on the conversation and the tool description.

Tools allow LLMs to:

  • Query databases
  • Execute code
  • Call external APIs
  • Search the web
  • Read or write data
  • Update agent state

Tools are the bridge between language models and real-world systems.


How LangChain Tools Work Internally

A LangChain tool is defined by:

  • A name
  • A clear description for the model
  • A strict input schema (via type hints or models)
  • A controlled output

The language model does not execute the code directly. It only requests a tool call. LangChain validates the request and runs the actual function in the backend.

This separation is what makes tools powerful and safe when designed correctly.


Agents and Tools: How They Work Together

Tools reach their full potential when used with agents.

An agent uses a language model to:

  1. Understand user intent
  2. Reason about the required steps
  3. Decide which tool to use
  4. Execute one or more tools
  5. Combine tool outputs into a final response

Unlike fixed pipelines, agents can dynamically choose different tools depending on the task.


Common Types of LangChain Tools

In real projects, tools usually fall into these categories:

Read-only tools

They only retrieve data and do not modify state.
Examples: search tools, database queries, document readers.

Execution or calculation tools

They run logic or code to produce a result.
Examples: math tools, Python execution, data processing.

Tools with side effects

They modify state or trigger real actions.
Examples: saving data, updating preferences, emitting events.

Tools with side effects require stronger validation and careful design.


Accessing Context, State, and Memory

LangChain tools can access runtime information using ToolRuntime. This allows tools to:

  • Read the agent’s current state
  • Access immutable context (user ID, session data)
  • Use persistent memory across conversations
  • Stream execution updates

This enables context-aware tools without exposing sensitive data to the model.


Complete Example: LangChain Tool with Persistent Memory

The following example shows how to create tools that store and retrieve user information using persistent memory and integrate them into an agent.

from typing import Any
from langchain_openai import ChatOpenAI
from langchain.tools import tool, ToolRuntime
from langgraph.store.memory import InMemoryStore
from langchain.agents import create_agent

# =========================
# TOOLS
# =========================

@tool
def save_user_info(
    user_id: str,
    user_info: dict[str, Any],
    runtime: ToolRuntime
) -> str:
    """Save user information to memory."""
    store = runtime.store
    store.put(("users",), user_id, user_info)
    return f"User {user_id} saved"

@tool
def get_user_info(
    user_id: str,
    runtime: ToolRuntime
) -> str:
    """Get user information from memory."""
    store = runtime.store
    data = store.get(("users",), user_id)
    return str(data.value) if data else "User not found"


# =========================
# MODEL + MEMORY
# =========================

model = ChatOpenAI(
    model="gpt-4o",
    temperature=0
)

store = InMemoryStore()

agent = create_agent(
    model=model,
    tools=[save_user_info, get_user_info],
    store=store,
    system_prompt="""
You are an assistant that manages user data.

Rules:
- When the user wants to SAVE a user, you MUST call save_user_info.
- When the user wants to GET a user, you MUST call get_user_info.

SAVE format:
User id=123, name=Anna, age=30

You must extract:
user_id = "123"
user_info = {"name": "Anna", "age": 30}
"""
)

# =========================
# TESTS
# =========================

print("\n--- SAVING USER ---")
agent.invoke({
    "messages": [
        {
            "role": "user",
            "content": "Save user id=123, name=Anna, age=30"
        }
    ]
})

print("\n--- GETTING USER ---")
response = agent.invoke({
    "messages": [
        {
            "role": "user",
            "content": "Get user information for id 123"
        }
    ]
})

print("\n--- FINAL RESPONSE ---")
print(response["messages"][-1].content)

This example demonstrates how a tool can:

  • Accept structured input
  • Access persistent memory
  • Modify state across sessions
  • Be safely orchestrated by an agent

Best Practices for LangChain Tools

When building tools for real applications:

  • Keep tools small and single-purpose
  • Write clear descriptions for the model
  • Always validate inputs before executing logic
  • Separate read and write operations
  • Add logging and auditability for critical tools
  • Design for failures and retries

Tools should be treated as production-grade code, not simple helpers.


Conclusion

LangChain tools are the foundation that turns language models into useful, interactive systems. Without tools, an LLM can only talk. With tools, it can act.

Understanding how tools work, how agents use them, and how to design them safely is essential for building reliable AI applications. Mastering tools means mastering LangChain itself.

Tags:

LangChain ToolsAI AgentsLLM

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *