What is an Agent?
An agent consists of three main components:- Agent Core: Uses an LLM to decide which action to take
- Tools: Functions the agent can call to interact with external systems
- AgentExecutor: Orchestrates the agent loop (optional, or use LangGraph)
For production agents with complex workflows, consider using LangGraph instead of the legacy AgentExecutor. LangGraph provides better control, state management, and human-in-the-loop capabilities.
Creating Tools
Tools are functions that agents can call. Define tools using the@tool decorator or by subclassing BaseTool.
Using the @tool Decorator
Structured Tool with Pydantic
For tools with complex inputs, use Pydantic models:Building an Agent with bind_tools
Bind tools to a chat model to enable tool calling:Executing Tool Calls
Handle tool calls in a loop:Agent Action Schema
LangChain defines schemas for agent actions and observations:Creating a Retriever Tool
Combine agents with retrievers for RAG-based agents:Async Agent Execution
All agent components support async execution:Best Practices
Clear tool descriptions
Write descriptive docstrings for tools. The LLM uses these to decide when to call each tool.
Use LangGraph for production
For complex agents, use LangGraph instead of manual loops.