Explains LangChain tools that connect LLMs to external APIs and services, built-in and custom tool creation, usage patterns, security practices, and organizing tools into reusable toolkits
In this lesson we cover tools — a core LangChain concept that extends a large language model’s capabilities by connecting it to external functions, services, and APIs. Tools let your LLM access real-world data sources (APIs, databases, internal services) and perform actions (I/O, computation, side effects), enabling richer, production-ready AI workflows.
LangChain ships with many ready-made tools for popular services such as Wikipedia, YouTube, and Google Search. Use these built-ins to quickly add search and knowledge retrieval to your agents without building integrations from scratch.
For private systems or custom workflows, implement a custom tool. A typical tool:
Accepts text or structured arguments,
Calls an external API, queries a database, or runs application logic,
Returns text or structured data (JSON, lists, etc.) that the LLM can consume.
Below is a minimal example showing the decorator-based pattern for creating a simple custom tool in Python. This pattern wraps your function so it can be invoked by LangChain agents and pipelines:
# Example: simple custom tool using LangChain's decorator patternfrom langchain.tools import tool@tooldef get_internal_user_profile(user_id: str) -> str: """Fetch a user profile from an internal API and return a summary string.""" # Replace this with your HTTP/db call or application logic profile = call_internal_api(user_id) # implement call_internal_api(...) return f"User {profile['id']}: {profile['name']} — {profile['role']}"
Tool behavior and usage notes
Aspect
What it means
Best practice
Inputs & outputs
Tools accept text prompts or structured arguments and return text, JSON, or other structured data
Normalize I/O formats and document the tool’s contract (input types, expected outputs)
Integration points
Tools are invoked inside pipelines, agents, or higher-level workflows to augment the LLM with live data or actions
Keep tool responsibilities focused and side effects explicit
Toolkits
Collections of related tools grouped together for a single purpose (e.g., search, user management)
Package related tools into toolkits for easier reuse and permissioning
Extensibility
LangChain provides many built-in toolkits; you can also create custom tools for private systems
Prefer built-ins when they meet requirements; add custom tools only when needed
Use built-in tools for common services (e.g., Wikipedia, YouTube, Google Search). For proprietary data or specialized workflows, create a custom tool and publish it in a toolkit so multiple pipelines and agents can reuse it.
When tools perform actions (modify data, call external APIs, or trigger side effects), validate and sanitize all inputs and outputs. Apply least-privilege access, input validation, and rate-limiting to reduce security and stability risks.
SummaryTools are the mechanism by which LangChain connects LLMs to external data and capabilities. They range from simple adapters for well-known services to fully custom integrations for private systems. By grouping tools into toolkits and using them within pipelines and agents, you can assemble modular, maintainable, and secure AI workflows.Further reading and references: